简体   繁体   English

如何从另一个PHP文件中的ajax获取变量

[英]How get variable from ajax in another php file

The problem is when I click on "submit data" button I get the response (this is correct), but when I click on "go to the php file" link (which is the same php file as previously) I get Undefined index: firstname and Undefined index: lastname error. 问题是当我单击“提交数据”按钮时,我得到了响应(这是正确的),但是当我单击“转到php文件”链接(与以前的PHP文件相同)时,我得到了未定义索引:名和未定义索引:名错误。 How can I fix it? 我该如何解决?

Here is my code: 这是我的代码:

<html>
<head>
    <script>
        function ajax_post(){
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create some variables we need to send to our PHP file
            var url = "my_parse_file.php";
            var fn = document.getElementById("first_name").value;
            var ln = document.getElementById("last_name").value;
            var vars = "firstname="+fn+"&lastname="+ln;
            hr.open("POST", url, true);
            // Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            // Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if(hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    document.getElementById("status").innerHTML = return_data;
                }
            }
            // Send the data to PHP now... and wait for response to update the status div
            hr.send(vars); // Actually execute the request
            document.getElementById("status").innerHTML = "processing...";
        }
    </script>
</head>
<body>
    <h2>Ajax Post to PHP and Get Return Data</h2>
    First Name: <input id="first_name" name="first_name" type="text">  <br><br>
    Last Name: <input id="last_name" name="last_name" type="text"> <br><br>
    <input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
    <div id="status"></div>
    <a href="my_parse_file.php">go to the php file</a>
</body>

and the php file 和PHP文件

<?php 

echo 'Thank you '. $_POST['firstname'] . ' ' . $_POST['lastname'] . ', says the PHP file';

?>

Because both are different requests : 因为两者是不同的请求:

As you are passing parameters firstname and lastname using AJAX. 当你正在传递参数firstnamelastname使用AJAX。 You have to pass the same in URL using GET request. 您必须使用GET请求在URL传递相同的内容。

go to the php file 转到PHP文件

<?php 

echo 'Thank you '. $_REQUEST['firstname'] . ' ' . $_REQUEST['lastname'] . ', says the PHP file';

?>

Output : 输出:

Thank you abc xyz , says the PHP file 谢谢abc xyz ,说PHP文件

It will work in both Submit button and Hyperlink . 它可以在Submit buttonHyperlink

By clicking the link your browser sends not a POST request, but a GET request to your server-side. 通过单击链接,浏览器不会向服务器端发送POST请求,而是发送GET请求。 That is why the global array $_POST doesn't contains the elements you are trying to retrieve in your PHP file. 这就是为什么全局数组$ _POST不包含您试图在PHP文件中检索的元素的原因。 The error message points you that there is no such elements in the $_POST array like "firstname" and "lastname". 错误消息指出您$ _POST数组中没有“ firstname”和“ lastname”之类的元素。 It is recommended to add a check whether the array elements exist like so: 建议添加检查是否存在数组元素,如下所示:

<?php
    if (isset($_POST['firstname']) && isset($_POST['lastname'])) {
        echo 'Thank you '. $_POST['firstname'] . ' ' . $_POST['lastname'] . ', says the PHP file';
    } else {
        echo 'Nothing to say';
    }

Thanks RJ for explanation. 感谢RJ的解释。 Now i fixed it and works properly. 现在,我修复它并正常工作。

But I am worried, because i used this code only for training. 但是我很担心,因为我仅将此代码用于培训。 In my main problem i need to send the real object (not string etc.) by ajax to my php site, so i cant add it to the url, can I? 在我的主要问题中,我需要通过ajax将真实对象(而不是字符串等)发送到我的php站点,因此我无法将其添加到url中,可以吗?

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

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