【问题标题】:How can i retrieve multiple values into my jquery form from php?如何从 php 将多个值检索到我的 jquery 表单中?
【发布时间】:2012-03-11 00:41:25
【问题描述】:

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

这是 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 应该检查用户名。

【问题讨论】:

  • 这是一个过于本地化的问题,因为它不会帮助任何人
  • Olivier,为了在未来获得支持,请确保您的帖子被表述为一个问题。这样一来,阅读的人就可以立即找出您遇到问题的方面。

标签: php jquery


【解决方案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}';

【讨论】:

  • 我看到您之前有不同的评论,您对正在做的事情有任何疑问吗?在此更新版本中,您的 Web 服务返回 JSON 格式的数据,然后 HTML 页面将其解析为您从中提取各个部分的对象。您也可以以类似的方式使用 XML
猜你喜欢
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
相关资源
最近更新 更多