如何从php中检索多个值到我的jquery表单?

时间:2012-03-11 00:41:25

标签: php jquery

基本上我想要一个检索值1或0的表单,以查看表单中是否存在电子邮件,并且我还想检索与电子邮件位于同一行的“用户名”列的名称。 / p>

这是javascript / php脚本(我从这里开始): 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 = "user@example.com" 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>

这是它与之交谈的PHP脚本: 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;
}

?>

和$ usercheck应该检查用户名。

1 个答案:

答案 0 :(得分:1)

您只需更改响应的格式,并修改收据处理方式。例如,你可以做一些像echo 0;这样的事情(我不确定你想用什么用户名,所以这只是猜测)

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

然后,在您的HTML页面中,您将

$.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>');
});

并且,对于IS可用案例,你是

echo '{"available":true}';