how to return value from ajax

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
shuchitaumehta
Forum Newbie
Posts: 15
Joined: Tue Jul 01, 2008 2:10 am

how to return value from ajax

Post by shuchitaumehta »

hello

i want 2 return true/false return from Ajax function

function valid(){
if(document.frm1.name.value==""){
document.frm1.name .focus();
alert("Please enter name");
return false;
}

check_present_user(url,"GET","message_div");

/* how to get return value & check*/


if(document.frm1.address.value==""){
document.frm1.address.focus();
alert("Please enter address");
return false;
}
return true;
}


function submitForm(){
if(valid()==true){
document.frm1.submit();
}
}



pls help me

thank you
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to return value from ajax

Post by Eran »

There is no ajax call in the code you provided.

Start by reading this tutorial by Mozilla - http://developer.mozilla.org/en/docs/AJ ... ng_Started

You may want to use a javascript library to handle your ajax requests, such as jQuery - http://jquery.com
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to return value from ajax

Post by Eran »

thank u for replay

i tried with javascript variable but its not secure
it shows all values in view source

i want to check user already present/ not before submitting form

if u have such example with jquery or any thing else
can u please help me

thank you
When you say user already present, do you mean to check if the user is logged in?
shuchitaumehta
Forum Newbie
Posts: 15
Joined: Tue Jul 01, 2008 2:10 am

Re: how to return value from ajax

Post by shuchitaumehta »

pytrin wrote:
thank u for replay

i tried with javascript variable but its not secure
it shows all values in view source

i want to check user already present/ not before submitting form

if u have such example with jquery or any thing else
can u please help me

thank you
When you say user already present, do you mean to check if the user is logged in?
hi,

no

its register form
user name must be unique
so when new user enter user name i m checking this in db
if its present in db then it says user already present please enter another user name

in such a case i want whole db data
if i take this in javascript variable / cookie

user can see whole data its not secure way

so what should i do..?

thank you
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to return value from ajax

Post by Eran »

Ok, an example with jQuery:

Suppose you have this simplified register form with a user-name input -

Code: Select all

 
<form action="register.php" method="post" id="registerForm">
      User name: <input type="text" name="username" /> <br />
      <input type="submit" value="Register!" />
</form>
 
You want to check if the user name is taken when the user submits the form. First you intercept the form submit event:

Code: Select all

 
<script type="text/javascript">
$(document).ready(function() { //This will make sure the code inside runs only when the DOM is ready
       $('form#registerForm').submit(function(event){  //Intercept the submit event for our form
             var form = this; 
             event.preventDefault(); //Prevent the form from submitting while we check the user name
             var username = $("input[name='username']",this).val(); //Extract the user-name value
             $.ajax({
                  url: 'check_username.php',   //This is the location of the script that will check if the user name exists in the database
                  data: 'username=' + username, 
                  success: function(msg) {  //This callback will handle the results of the check
                         if(msg == 1) { //We'll define a return value of 1 to confirm the user name is available
                                form.submit(); //Submit the form
                         } else {
                             // show an error message
                         }
                  }
             });
       });
});
</script>
 
You will need to also create the script that checks the user name against the database (in my example I named it check_username.php). It will recieve the username via the $_GET superglobal. Something like:

check_username.php

Code: Select all

 
if($_SERVER['REQUEST_METHOD'] == 'GET') {
      $username = $_GET['username'];
      // Check if exists in the database, output '1' if available
}
 
shuchitaumehta
Forum Newbie
Posts: 15
Joined: Tue Jul 01, 2008 2:10 am

Re: how to return value from ajax

Post by shuchitaumehta »

pytrin wrote:Ok, an example with jQuery:

Suppose you have this simplified register form with a user-name input -

Code: Select all

 
<form action="register.php" method="post" id="registerForm">
      User name: <input type="text" name="username" /> <br />
      <input type="submit" value="Register!" />
</form>
 
You want to check if the user name is taken when the user submits the form. First you intercept the form submit event:

Code: Select all

 
<script type="text/javascript">
$(document).ready(function() { //This will make sure the code inside runs only when the DOM is ready
       $('form#registerForm').submit(function(event){  //Intercept the submit event for our form
             var form = this; 
             event.preventDefault(); //Prevent the form from submitting while we check the user name
             var username = $("input[name='username']",this).val(); //Extract the user-name value
             $.ajax({
                  url: 'check_username.php',   //This is the location of the script that will check if the user name exists in the database
                  data: 'username=' + username, 
                  success: function(msg) {  //This callback will handle the results of the check
                         if(msg == 1) { //We'll define a return value of 1 to confirm the user name is available
                                form.submit(); //Submit the form
                         } else {
                             // show an error message
                         }
                  }
             });
       });
});
</script>
 
You will need to also create the script that checks the user name against the database (in my example I named it check_username.php). It will recieve the username via the $_GET superglobal. Something like:

check_username.php

Code: Select all

 
if($_SERVER['REQUEST_METHOD'] == 'GET') {
      $username = $_GET['username'];
      // Check if exists in the database, output '1' if available
}
 
thanks a lot
Post Reply