Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量从php传递回jquery?_Php_Jquery_Html - Fatal编程技术网

将变量从php传递回jquery?

将变量从php传递回jquery?,php,jquery,html,Php,Jquery,Html,我正在尝试将我的登录页面转换为使用html,并使用jquery和php获取和处理结果,原因是如果我想用手机处理我的项目,那么我就快到了 我遇到的问题是将变量从php传递回jquery以同时显示 我的例子 index.html <!DOCTYPE HTML> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> &l

我正在尝试将我的登录页面转换为使用html,并使用jquery和php获取和处理结果,原因是如果我想用手机处理我的项目,那么我就快到了

我遇到的问题是将变量从php传递回jquery以同时显示

我的例子 index.html

<!DOCTYPE HTML>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Auth Demo 2</title>
    <script src="jquery/jquery-1.7.2.min.js"></script>
    <script src="main.js"></script>
</head>

<body onload="handleLogin()">

        <form id="loginForm">

            <label for="email">Email:</label>
            <input type="text" name="email" id="email" value="" placeholder="email" />



            <label for="password">Password:</label>
            <input type="password" name="password" id="password" value="" placeholder="Password" />


        <input type="submit" value="Login" id="submitButton">
        </form>

</body>
</html> 
login.php

<?//get the posted values
require_once("../backend/functions.php");
dbconn(true);


if ($_POST["email"] && $_POST["password"]) {
    $password = passhash($_POST["password"]);

    if (!empty($_POST["email"]) && !empty($_POST["password"])) {
        $res = SQL_Query_exec("SELECT id, password, secret, status, enabled FROM users WHERE email = " . sqlesc($_POST["email"]) . "");
        $row = mysql_fetch_assoc($res);

        if ( ! $row || $row["password"] != $password )
            $message = "Wrong password";
        elseif ($row["status"] == "pending")
            $message = "Account Pending";
        elseif ($row["enabled"] == "no")
            $message = "Account Suspened";
    } else
        $message = "No Username/password added";

    if (!$message){
        logincookie($row["id"], $row["password"], $row["secret"]);
        if (!empty($_POST["returnto"])) {
            header("Refresh: 0; url=" . $_POST["returnto"]);
            die();
        }
        else {
            echo  "yes";
            die();
        }
    }else{
        echo $message;
    }
}

logoutcookie();

最好的方法是使用$.ajax()并使用“XML”类型。然后让login.php返回一个XML文件,其中包含您需要的任何参数(用户id、错误消息(如果有)、用户名等)。然后可以使用jQuery解析XML文件以立即使用这些变量。无论如何,在后端处理错误更安全。

只需警报数据即可

  if(data=='yes') //if correct login detail
  {
      alert( "Request success: ");
  } 
  else{
        alert (data)
   }
但是,我建议以JSON的形式获得响应

if(u!= '' && p!= '') {
     $.post("http://www.mysite.co.uk/login.php",{ 
               email:$('#email', form).val(),
               password:$('#password', form).val(),
               rand:Math.random() 
         } ,function(data)
             {
                if(data=='yes') //if correct login detail
                {
                    alert( "Request success: ");
                }
                else
                {                                                      //add reason in alert box here
                    alert (data)
                 }
           });

  } else {
         alert( "Username/password empty");
  }

     return false;//not to post the  form physically
  }
尝试使用JSON

JSON.stringify(your_object, null, 2);
在PHP中使用以下内容捕获帖子以获取数组:

PHP完成后,返回数组数据的编码JSON字符串:

在jQuery中,用于测试do

console.log(data);
console.log(data.yes);

我花了整整2分钟来整理你的答案(用wierd fromatting)。。。。该死的!!我仍然看到一些额外的空间..:(你没有在任何地方提交表单。很抱歉,Bipen下次会确保表单整洁JAI-im从顶级索引提交。htmlno prbolem……):)这确实有效,谢谢,我会研究json,因为你们都推荐了它,我确信一旦我掌握了它。它将在将来帮助我;)欢迎看看JSON。。。最好用那个…:):):。。。快乐编码…)
$data = json_decode($_POST);
echo json_encode($return_data);
console.log(data);
console.log(data.yes);
function handleLogin(){

    var form = $("#loginForm");
    var u = $("#email", form).val();
    var p = $("#password", form).val();

    if(u!= '' && p!= '') {

        jQuery.ajax({
             type: "POST", 
             url: "http://www.mysite.co.uk/login.php", 
             data: 'username='+u+'&password='+p+'&rand='+Math.random(),
             complete: function(data){

                 if(data.responseText == 'yes'){
                    alert( "Request success: ");
                 }else{
                    alert( "failed: ");
                 }

         });

    } else {

        alert( "Username/password empty"); 

    }

     return false;//not to post the  form physically

 }