[jQuery] Re: on form submission, how to decide on submit: return true / false
HI there, This is the Code which You need , have fun Var Mydata{ Username: $('#username').val(), Password: $('#password').val() } $.ajax({ url: 'check.php', type: 'POST', data:Mydata // id of the tree dataType: 'html', cache: false, beforeSend: function() {}, error: function(){ // if the datas has not been send }, success: function (txt){ }// end of the success });// end of the .ajax -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of freech Sent: Wednesday, February 20, 2008 5:35 PM To: jQuery (English) Subject: [jQuery] on form submission, how to decide on submit: return true / false Hi there, I got helps and also debugging on self for days on trying to implement a form submission with jQuery, now I was stunk on getting the results from call back .Post or .ajax to pass true false to the form submission. the code is pretty simple here: $(document).ready(function() { $(#regForm).submit(function() { $.post(check.php, { username: $('#username').val(),password: $ ('#password').val() }, function(data) { $('#errorMessage').html(data); if (data == pass ) return true; else return false; //check.php will output { pass, user name already token, input username password} //problem here, the true / false value doesn't work. //even if I commented this line and add return false, the form will still be submitted. //I got kind suggestions should try replace $.Post with $.ajax to call here, but don't know how to. }); }); }); the full code: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd html xmlns=http://www.w3.org/1999/xhtml; head meta http-equiv=Content-Type content=text/html; charset=utf-8 / titleRegister/title script type=text/javascript src=jquery.js/script script type=text/javascript $(document).ready(function() { $(#regForm).submit(function() { $.post(check.php, { username: $('#username').val(),password: $ ('#password').val() }, function(data) { $('#errorMessage').html(data); if (data == pass ) return true; else return false; }); }); }); /script /head body form TARGET=?php echo $PHP_SELF; ? method=POST id=regForm pUsername:input type=text name=username id=username / pPassword:input type=password name=password id=password / INPUT TYPE=HIDDEN NAME=stage VALUE=go p id=errorMessage style='color:#f00'/p input type=submit name=submit id=send value=Sign Up! / /form ?php if ($_POST['stage']=='go') { // do registration insert user data echo form submitted; } //print_r($_POST); ? /body /html
[jQuery] Re: on form submission, how to decide on submit: return true / false
Hi Andrea, Thank you, you got my flow right and the explanation is very clear, so I should put all the actions rely on those data into the callback function of $.post. for e.g. $.post('check.php', {username: $('#username').val(),password: $ ('#password').val()}, function(data){ if (data == 'true') return true; else return false; }); I will try to figure it. I have 2 questions on following: 1. I handle the check.php file like: ?php if (!isset($_POST['username'])) echo please input a username; ... if $username already exists, echo the name you try to register is already taken; so, back to the callback function, $.post('check.php', {username: $('#username').val(),password: $ ('#password').val()}, function(data){ if (data == 'true') return true; else return false; }); the data will be something looks like: please input a username, the name you try to register is already taken, as the check.php file outputs. instead of { data == 'ture' }. I feel to get data from check.php to decide return ture or false is not workable if I design the check.php file like above. Any good ideas? 2. I used form TARGET=?php echo $PHP_SELF; ? to write the register form data insert in same file: register.php, is there any way to combine the functions in check.php into register.php, which means all of the actions will be executed in 1 file? looks like $.post('register.php'), (call itself), are there any good solutions? On Feb 15, 6:00 am, andrea varnier [EMAIL PROTECTED] wrote: On 14 Feb, 15:57, freech [EMAIL PROTECTED] wrote: //I have problem on here: if ( !$(.sdComt).response ) { return false; } else { return true; } ok, if I got it right, you're using a $.post call to check if the user's input is correct or somehow acceptable, and then you append to the form an hidden input to be checked on submit. ok, assuming that check.php returns the string 'true' or 'false', one way of doing that is adding a listener for the submit action of your form. $('#sdComt').submit(function(){ //code here }); inside this event handler you will put your ajax call. $('#sdComt').submit(function(){ $.post('check.php', {. }); in order to decide whether the form is to be passed or not, you will have to wait for the response. so all the actions that rely on those data, have to be put in the callback function of $.post $('#sdComt').submit(function(){ $.post('check.php', {username: $('#username').val(),password: $ ('#password').val()}, function(data){ if (data == 'true') return true; else return false; }); }); hope this helps... but I'm not sure I got the flow right :)
[jQuery] Re: on form submission, how to decide on submit: return true / false
On 15 Feb, 06:20, freech [EMAIL PROTECTED] wrote: 1. I handle the check.php file like: ?php if (!isset($_POST['username'])) echo please input a username; ... if $username already exists, echo the name you try to register is already taken; I think you could insert in your document a div with an id=errorMessage (or whatever) and then change the callback function of $.post so that it puts the response message in the div. for example $.post('check.php', { username: $('#username').val(), password: $('#password').val() }, function(data){ $('#errorMessage').html(data); // but you still need some check to see if the form is ok // what does 'check.php' send back in case the form is ok? // I assume nothing, so: if (data != ) return false; else return true; }); now I'm just thinking that maybe doing as I suggested wil not work, because $.post is an asynchronous request. you may want to lock the page somehow, and get a synchronous request, and have the form submit waiting for the response... not sure (I'm not that expert I fear), so have a look here http://docs.jquery.com/Ajax/jQuery.ajax#options :) 2. I used form TARGET=?php echo $PHP_SELF; ? to write the register form data insert in same file: register.php, is there any way to combine the functions in check.php into register.php, which means all of the actions will be executed in 1 file? looks like $.post('register.php'), (call itself), are there any good solutions? actually I don't know how that would work. but you can try and let us know :) good luck
[jQuery] Re: on form submission, how to decide on submit: return true / false
On 14 Feb, 15:57, freech [EMAIL PROTECTED] wrote: //I have problem on here: if ( !$(.sdComt).response ) { return false; } else { return true; } ok, if I got it right, you're using a $.post call to check if the user's input is correct or somehow acceptable, and then you append to the form an hidden input to be checked on submit. ok, assuming that check.php returns the string 'true' or 'false', one way of doing that is adding a listener for the submit action of your form. $('#sdComt').submit(function(){ //code here }); inside this event handler you will put your ajax call. $('#sdComt').submit(function(){ $.post('check.php', {. }); in order to decide whether the form is to be passed or not, you will have to wait for the response. so all the actions that rely on those data, have to be put in the callback function of $.post $('#sdComt').submit(function(){ $.post('check.php', {username: $('#username').val(),password: $ ('#password').val()}, function(data){ if (data == 'true') return true; else return false; }); }); hope this helps... but I'm not sure I got the flow right :)