Oliver K
Oliver K

Reputation: 15

How can i retrieve multiple values into my jquery form from php?

Basically i want a form that retrieves the value 1 or 0 to see whether an email exists within the form, and i also want to retrieve the name of the 'username' column which is on the same row as the email.

Here is the javascript/php script ( i start here ) : http://pastebin.com/zB0Umyyb

<!DOCTYPE html>
<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

  <script>
  $(document).ready(function() {
    $("#tabs").tabs();
  });
  </script>
<script type="text/javascript">


  $(document).ready(function() {


                //the min chars for username
                var min_chars = 3;

                //result texts
                var characters_error = 'Minimum amount of chars is 3';
                var checking_html = 'Checking...';

                //when button is clicked
                $('#check_email_availability').click(function(){
                        //run the character number check
                        if($('#email').val().length < min_chars){
                                //if it's bellow the minimum show characters_error text
                                $('#email_availability_result').html(characters_error);
                        $(checking_html).hide();
                        }else{                 
                                //else show the cheking_text and run the function to check
                                $('#email_availability_result').html(checking_html);
                                check_availability();
                                $(checking_html).hide();
                        }
                });


  });

//function to check username availability      
function check_availability(){

                //get the username
                var email = $('#email').val();
                //use ajax to run the check
                $.post("check_email.php", { email: email },
                        function(result){
                                //if the result is 1
                                if(result == 1){
                                        //show that the username is available
                                        $('#email_availability_result').html('<span class="is_available"><b>' +email + '</b> is Available</span>');
                                }else{
                                        //show that the username is NOT available
                                        $('#email_availability_result').html('<span class="is_not_available"><b>' +email + '</b> has already been refered</span>');
                                }
                });

}  
</script>
</head>
<body style="font-size:62.5%;">
<?
require ("config/config.php");
$result = mysql_query('select * from refer where email = "[email protected]" AND referred = "true"');
$row = mysql_fetch_array($result);
$usercheck = $row['username'];
?>
<div id="tabs">
    <ul>
        <li><a href="#fragment-1"><span>Refer</span></a></li>
        <li><a href="#fragment-2"><span>Referal History</span></a></li>
        <li><a href="#fragment-3"><span>Rewards</span></a></li>
    </ul>
    <div id="fragment-1">
<form>
  <p>Minecraft User:
  <input type='text' id='username'>
    </p>
  <p>Email Address:
  <input type='text' id='email'> <input type='button' id='check_email_availability' value='Check Availability'>
  </p>
</form>
  <script>
  $(document).ready(function() {
    $("button").button();
  });
  </script>
  <button> Refer! </button>
  </br>
  <div id='username_availability_result'></div>
  </br>
<div id='email_availability_result'></div>
  </div>
    <div id="fragment-2">
<table border="1">
<tr>
<th>User</th>
<th>Referal Request</th>
</tr>
<tr>
<td>MrMan121</td>
<td>Pending</td>
</tr>
<tr>
<td>olimoli123</td>
<td>Accepted</td>
</tr>
</table>
    </div>
    <div id="fragment-3">
Rewards include:
dadadad.
    </div>
</div>
</body>
</html>

And here is the php script it talks to: http://pastebin.com/tQppn84s

<?php
//connect to database
require ("config/config.php");

//get the username
$email = mysql_real_escape_string($_POST['email']);

//mysql query to select field username if it's equal to the username that we check '
$result = mysql_query('select email from refer where email = "'. $email .'" AND referred = "true"');
$row = mysql_fetch_array($result);
$usercheck = $row['username'];
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysql_num_rows($result)>0){
        //and we send 0 to the ajax request
        echo 0;
}else{
        //else if it's not bigger then 0, then it's available '
        //and we send 1 to the ajax request
        echo 1;
}

?>

and $usercheck should check for the username.

Upvotes: 0

Views: 385

Answers (1)

Robot Woods
Robot Woods

Reputation: 5687

You just need to change the formatting of the response, and modify how it's handled on receipt. For instance, instead of just echo 0; you could do something like (I wasn't sure how you wanted username used, so that was just a guess)

echo '{"available":false, "username":"'.$usercheck.'"}';

then, in your HTML page, you would

$.post("check_email.php", { email: email },
function(result){
    response=jQuery.parseJSON(result);
    //if the result is 1
    if(response.available){
        //show that the username is available
        $('#email_availability_result').html('<span class="is_available"><b>' +email + '</b> is Available</span>');
    }else{
        //show that the username is NOT available
        $('#email_availability_result').html('<span class="is_not_available"><b>' +email + '</b> has already been referred by user '+response.username+'</span>');
});

and, for the IS available case, you'd

echo '{"available":true}';

Upvotes: 1

Related Questions