Causing an Angular form to POST - forms

When a form is valid, I want it to POST the value to a web site.
I get the alert to fire nicely when the form is not valid, but I can't set the action and cause the form to submit when the form is valid.
So, the result should be that the form posts and the browser goes to the next page like a traditional html form.
<form name="userForm" ng-submit="submitForm(userForm)" novalidate>
......
validationApp.controller('mainController', function($scope) {
// function to submit the form after all validation has occurred
$scope.submitForm = function(form) {
if(form.$invalid){
alert("form invalid");
}
else
{
form.action="http://microsoft.com";
form.submit();
}
};
});

Angular doesn't works that way. Angular mostly expects a RESTful web service to answer its calls. Yes, you can get it working with action somehow. However, for redirecting you would have to make use of $location web service. Through $location you can redirect your page to next page.
$scope.submitForm=function(form){
if(form.$invalid){
alert("form invalid");
}
else
{
$http.post(url:'yourURL', yourData));
$location.path='newURL';
}

From the documentation on ngSubmit:
Additionally it prevents the default action (which for form means sending the request to the server and reloading the current page), but only if the form does not contain action, data-action, or x-action attributes.
Otherwise, I think you can pass $event to the function and trigger $event.srcElement.submit(). The object you're passing to the function isn't the form element itself, which is why it won't submit. If you want, you can pass both by doing ng-submit="submitForm($event, userForm)". Hope that helps!

Related

Conflict between Featherlight.js and Contact Form 7?

I'm trying to get a Contact Form 7 form to work in a Featherlight.js lightbox. I've created a page at mydomain.com/contact and set the link to open mydomain.com/contact #main article.
Featherlight does open the form, but when I submit the form, the lightbox closes and the url resolves to mydomain.com/contact/#wpcf7-f262-p11-o1. It doesn't matter if it's successfully submitted or there are validation errors, the lightbox still closes (to be clear, the form does actually work—I receive the email).
If I open the entire page (mydomain.com/contact/), the lightbox doesn't close on submission, which leads me to believe that perhaps there's an AJAX conflict.
That said, I don't receive any errors in the console.
Any help in resolving the issue would be appreciated!
Thanks.
I've got it working, thanks to the second part of the accepted answer here (the Documentation example from the jQuery website).
jQuery's submit() function didn't work for me—I'm guessing it's a version issue? In any case, this is my final code:
/* attach a submit handler to the form */
$( "body" ).on( "submit", ".wpcf7-form", function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
url = $form.attr( 'action' );
/* Send the data using post */
var posting = $.post( url, $form.serialize() );
/* Put the results in a div */
posting.done(function(data) {
var content = $(data).find('.wpcf7-form');
$('.featherlight .wpcf7').empty().append(content);
});
});

Facebook PHP SDK Capture Like Event

I need to record to the database if a user likes an iframe page tab using the official Facebook 'like' button at the top. I did a search here at stackoverflow and found a javascript snippet ...
FB.Event.subscribe('edge.create',
function(response) {
alert('You liked the URL: ' + response);
}
);
But it doesn't fire for the official button (only like buttons added to the page)? I declare it after FB.init and before FB.Canvas resize inside the asynchronous call. Is there a way in PHP to capture this - possible on refresh? Either JS or PHP is okay (since JS can simply ajax a php file). The signed request contains whether they like the page or not but I need to capture it as it happens (or just happened).
Any help greatly appreciated :-)
Try :
window.fbAsyncInit = function () {
FB.Event.subscribe('edge.create', function (targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});
};
I think you just need to setup the subscript inside the fbAsyncInit.
Did it a purely PHP way in the end.
1: Get the signed request from facebook and extract the liked stats
2: Create a variable $liked and set to "yes" or "no" based on signed request value
3: Manipulate a session - use a session because the page gets refreshed when you "like" but the session remains (but the request value will have changed)
if(isset($_SESSION['likestatus'])){
if($_SESSION['likestatus'] == "no" && $liked == "yes"){
// do mysql updatestuff cause has liked page since session started
$_SESSION['likestatus'] = $liked;
}
else{
$_SESSION['likestatus'] = $liked;
}
}
else {
$_SESSION['likestatus'] = $liked;
}

Passing Parameters from e-mail link to jQuery mobile web app

I created a web app using jquery mobile 1.1.1
As part of my app I built password retrieval functionality. If a user needs to reset their password, they fill out a form and receive an e-mail with a link that includes the address of the password reset page and two other parameters as such:
www.mywebapp.com/demo.html#resetPassword?x=123&y=123
The Initial Problem:
When the user clicks on the link, they see the home page of the web app even though the URL in the address bar says: www.mywebapp.com/demo.html#resetPassword?x=123&y=123 I understand that jQuery mobile does not support passing parameters after the hash, so I came up with the following solution.
A Solution with a small inconvenience:
I put together the following code, which reads the URL, captures my two parameters and redirects the user to the password reset page:
$( document ).bind( "pagebeforeshow", function() {
//cpe("parameter") will check whether the specified URL parameter exists
if(cpe("x") && cpe("y")){
//gpv("parameter") captures the value of the specified URL parameter
recovery.username=gpv("x");
recovery.token=gpv("y");
$.mobile.changePage("#resetPassword");
}
})
The Inconvenience, and thus my current problem:
When the user clicks on the link in the e-mail the browser fires up and opens the main page of the app, and then it quickly displays the #resetPassword page. I understand that this happens because I'm changing the page
$.mobile.changePage("#resetPassword");
But, how do I modify the above code so that the user won't see the main page at all, and go straight to the #resetPassword page?
Use an empty initial page with no content. By default do a changePage to what was your initial page, but in other cases, like the resetPassword case, you changePage to that instead.
I followed Raymond Camden's suggestion and added the following to my html:
<pre>
<!--Start of blank initial page: #initPage-->
<div data-role="page" id="initPage">
<div data-role="content"></div>
</div>
<!-- /page -->
</pre>
I also added the following to my javascript:
//init page -> path control hub
$( document ).bind( "pagebeforeshow", function() {
var pageid=$.mobile.activePage.attr('id');
if(pageid=="initPage"){
if(cpe("x") && cpe("y")){
recovery.username=gpv("x");
recovery.token=gpv("y");
$.mobile.changePage("#resetPassword");
}else{
$.mobile.changePage("#info");
}
}
})
It's working now.

Facebook Register Plugin - Form submitting even though validation fails?

I'm using the Facebook Registration Plugin (http://developers.facebook.com/docs/plugins/registration/) to register users for our site. The problem is I can't seem to get custom validation working and was wondering whether it's a mistake on my part or something wrong with Facebook.
This is the XFBML code that I'm using:
<fb:registration
fields="[{'name':'name'},
{'name':'email'},
{'name':'password','description':'Enter a password','type':'text'}]"
redirect-uri="http://local.dev"
onvalidate="validateFacebookRegistrationForm">
</fb:registration>
and I have a global function called validateFacebookRegistrationForm which has the following code in it:
function validateFacebookRegistrationForm(form) {
errors = {};
if (form.password == "") {
errors.password = "No Password Entered";
}
return errors;
}
I would expect hitting the register button on the form would do nothing and the validation message would show up... instead I get a popup like this:
http://i.imgur.com/ERxw3.jpg
Once the popup is closed, the form is validated ... and the error message shows. Clicking register again will not submit the form until the errors have been fixed - which is the how the form should behave in the first instance!
The Facebook Registration plugin does not client-side validate until AFTER the user has pressed "Register" and then in the popup pressed "Continue". Then, it will do the "client-side" validation... it really doesn't make sense to go in that order but that's how it seems to be.
Your code is fine.

Facebook fan page tab and user id

As per the documentation in the following link, we can get the user id if the uer will interact with the form..
http://wiki.developers.facebook.com/ind … d_Policies
"If a viewing user interacts with the tab (like submits a form, takes an action that causes an AJAX load of new content, or follows a relative URL that loads on the tab), that user's UID is sent to the application as the fb_sig_user parameter, the profile owner's user ID is sent as the fb_sig_profile_user parameter. The viewing user's session key is key is sent only if the user authorized the application. "
In my fan page tab am I have an AJAX form which the user can submit with some value.. now I need the users id also.. how can I get this..
I tried to get the value in my AJAX submit page using $_POST['fb_sig_user'] with no success.. can anyone help me with this please..
You won't be able to get the id of the user using $_POST['fb_sig_user'] unless you authenticate the user by having this in the facebook's ajax function:
ajax.requireLogin = true;
For example, I'm retrieving it fine with this:
function do_ajax(url, div_id)
{
document.getElementById('poller_waitMessage').setStyle('display', 'block');
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.onerror = function(error)
{
new Dialog().showMessage("Error:", "Some communication error occured, Please reload the page.","Ok");
};
ajax.ondone = function(data)
{
document.getElementById('poller_waitMessage').setStyle('display', 'none');
document.getElementById(div_id).setInnerFBML(data);
}
ajax.requireLogin = true; // <----- this is important
ajax.post(url);
}
I've been happily using the form variable fb_sig_profile_user for previous apps, and when developing a new app last week, the variable was no where to be found.
Searched for several days, I was about to give up, and then the found answer:
ajax.requireLogin = true;
I understand FB cares about privacy and all, but they really need to announce these kinds of changes before just taking it away.
Million Thanks!