简体   繁体   English

在 PHP 和 Javascript 之间传递变量时遇到问题

[英]Trouble passing variables between PHP and Javascript

I have a simple HTML5 login page that requests a username and a password.我有一个简单的 HTML5 登录页面,需要用户名和密码。 The information is passed via POST to PHP for comparison with data in MySQL.信息通过 POST 传递到 PHP 以与 MySQL 中的数据进行比较。 I am trying to echo a simple "YES" if data was successfully validated and a "NO" otherwise.如果数据成功验证,我试图回应一个简单的“是”,否则回应“否”。 I want to take the text value of the response and manipulate it using Javascript.我想获取响应的文本值并使用 Javascript 对其进行操作。 Everything seems to be working properly, but when the $response variable is echoed it is shown by itself on a blank HTML document and the JavaScript never gets executed.一切似乎都在正常工作,但是当 $response 变量被回显时,它会在一个空白的 HTML 文档中单独显示并且 JavaScript 永远不会被执行。 Following is the code I am using :以下是我正在使用的代码:

<ul>
<li>
<form action="login_submit.php" id="loginTest" method="post">
<span class="un"><i class="fa fa-user"></i></span><input type="text" id="maindata_email" name="maindata_email" value="" maxlength="40" required class="text" placeholder="Usuario"/></li>
<li>
<span class="un"><i class="fa fa-lock"></i></span><input type="password" id="maindata_password" name="maindata_password" value="" maxlength="10" required class="text" placeholder="Password"/></li>
<li>
<input type="hidden" name="form_token" value="<?php echo $form_token; ?>">
<div class="up">
<input type="submit" id ='ingresar' value="ingresar" class="btn">
<!--window.alert(5 + 6);-->
</form>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript">
    var myData = <?php echo json_encode($response); ?>;
    window.alert(myData);
    if (myData == "YES") {
        alert("Usuario aceptado!");
        var href = 'http://index2.html';
        window.location=href;
    } else {
        alert("Usuario invalido!");
        var href = 'http://index.html';
        window.location=href;       
    }
</script>
<input type="submit"  id = 'editar' value="editar" class="btn">
</form>
</div>

</li>
</ul>

And here is the PHP section :这是 PHP 部分:

 /*** bind the parameters ***/
        $stmt->bindParam(':maindata_email', $maindata_email, PDO::PARAM_STR);
        $stmt->bindParam(':maindata_password', $maindata_password, PDO::PARAM_STR, 40);

        /*** execute the prepared statement ***/
        $stmt->execute();

        /*** check for a result ***/
        $user_id = $stmt->fetchColumn();

        /*** if we have no result then fail boat ***/
        if ($user_id == false)
        {
                echo $response = "NO";
        }
        /*** if we do have a result, all is well ***/
        else
        {
                /*** set the session user_id variable ***/
                $_SESSION['user_id'] = $user_id;

                /*** tell the user we are logged in ***/
                echo $response = "YES";
        }


    }
    catch(Exception $e)
    {
        /*** if we are here, something has gone wrong with the database ***/
        $message = 'No se puede procesar su ingreso.  Favor de intentar mas tarde.'."<br />\r\n";
    }
}

After I click on ingresar in the HTML the username and password are properly sent and received by PHP, the information is properly processed and compared to the values contained in the MySQL database and I get the proper response upon validation.在 HTML 中单击 ingresar 后,PHP 正确发送和接收用户名和密码,正确处理信息并与 MySQL 数据库中包含的值进行比较,并在验证时得到正确的响应。 Could anyone please explain why I get the blank page with the $response and the JavaScript code is never executed???任何人都可以解释为什么我得到带有 $response 的空白页面并且 JavaScript 代码永远不会被执行??? Thanx谢谢

Your code seems to be behaving as expected.您的代码似乎按预期运行。 Your form, upon submission makes a POST request to the servar at login_submit.php , and the browser displays the response from the server, which is where you've echoed the value of $response .您的形式,在提交使得POST请求在servar login_submit.php ,浏览器显示从服务器,这是你到哪儿去呼应的价值响应$response There is no javascript or HTML printed on the PHP page, so no javascript is going to be run, and no HTML is going to be displayed... the browser is merely displaying the raw contents of the POST response from the form action since it's just interpreting it as a text response with no markup. PHP 页面上没有打印 javascript 或 HTML,因此不会运行 javascript,也不会显示 HTML……浏览器仅显示来自表单操作的 POST 响应的原始内容,因为它是只是将其解释为没有标记的文本响应。

Now, what you're actually wanting is to get the data from the response on a web-page and run some javascript using that response data.现在,您真正想要的是从网页上的响应中获取数据并使用该响应数据运行一些 javascript。 There are a couple ways you could tackle this problem:有几种方法可以解决这个问题:

  1. Avoid a page reload by using AJAX techniques to asynchronously request data back from the server through login_submit.php .通过使用 AJAX 技术通过login_submit.php从服务器异步请求数据来避免页面重新加载。 When the data arrives back from the server after some indeterminate period of time, a javascript callback is called on the page which involves an action on the incoming response data.当数据在某个不确定的时间段后从服务器返回时,将在页面上调用 javascript 回调,该回调涉及对传入响应数据的操作。
  2. Your login_submit.php page prints a full HTML page with the javascript embedded on that page, and you populate the variables in the resulting javascript from the PHP code that runs the MySQL query.您的login_submit.php页面会打印一个完整的 HTML 页面,该页面上嵌入了 javascript,您可以在运行 MySQL 查询的 PHP 代码生成的 javascript 中填充变量。
  3. Take a page from ASP.NET forms and POST back to the page itself rather than a separate page, and use PHP to detect when a POST-back has taken place.从 ASP.NET 表单中取出一个页面并 POST 回页面本身而不是一个单独的页面,并使用 PHP 来检测何时发生了 POST-back。 So all the code for the form, MySQL query, etc. exists in the same PHP document, and you POST to the page itself.因此,表单、MySQL 查询等的所有代码都存在于同一个 PHP 文档中,您可以 POST 到页面本身。 When the page reloads from the POST, it will populate the necessary javascript variables on the page with the correctly values.当页面从 POST 重新加载时,它将用正确的值填充页面上必要的 javascript 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM