[英]It is possible to get the variables sent by Ajax in php file
I have my form defined as follows in the PHP file (Using jquery-2.1.4.min.js
) : 我在PHP文件中定义了如下表单(使用
jquery-2.1.4.min.js
):
<form id = "testForm" action="" method="post" >
First name: <input type="text" name="FirstName" id="FirstName" value="Mickey"><br>
Last name: <input type="text" name="LastName" id="LastName" value="Mouse"><br>
<button type = "button" onclick="submit()">Submit Top Test </button>
</form>
The following functions is called when the button is clicked. 单击该按钮时,将调用以下功能。
function submit(){
var firstName = $('#FirstName').val();
alert("Inside SubmitQuery "+firstName);// Work fine
var request = $.ajax({
url: 'php/<path to php file>/processPost.php',
type:'POST',
data:{title:firstName},
success: function(msg) {
alert(msg);// I can see the var_dumps here
}
});
}
In processPost.php
file, I have defined the following two var_dumps
: 在
processPost.php
文件中,我定义了以下两个var_dumps
:
1) var_dump(isset($_POST['title'])); 1)var_dump(isset($ _ POST ['title']));
2) var_dump ($_POST['title']); 2)var_dump($ _POST ['title']);
I can see the values getting printed in the alert(msg)
window. 我可以在
alert(msg)
窗口中看到正在打印的值。 Same is true in the Response
tab of the Network
tab of developers tools window. 在开发人员工具窗口的“
Network
选项卡的“ Response
选项卡中也是如此。 But I want to have these variables available in the processPost.php
file so that I could use it to make a curl request to webservice call. 但我想在
processPost.php
文件中提供这些变量,以便可以使用它向Webservice调用发出curl请求。 Is it possible to get these variables inside processPost.php
file? 是否可以在
processPost.php
文件中获取这些变量?
I mean, you basically answered your own question in your question. 我的意思是,您基本上回答了自己的问题。 The fact that you have this file:
您拥有此文件的事实:
processPost.php: processPost.php:
<?php
var_dump( $_POST['title'] );
?>
and your success: function(msg){ alert(msg); }
和您的
success: function(msg){ alert(msg); }
success: function(msg){ alert(msg); }
is properly alerting "Coder" (or whatever name you submit) Literally means that the variable $_POST['title']
is available in your processPost.php
file. success: function(msg){ alert(msg); }
会正确地警告“编码器”(或您提交的任何名称), 从字面上看意味着变量$_POST['title']
在您的processPost.php
文件中可用。 If alert(msg)
gave you nothing, or an error, then you'd know something is wrong. 如果
alert(msg)
没有给您任何帮助或出现错误,那么您将知道有问题。
success: function(msg)
means that, if successful, take the output value from your url
(in this case, processPost.php), and make it available to your javascript as a variable named msg
. success: function(msg)
表示,如果成功,则从url
获取输出值(在本例中为processPost.php),并将其作为名为msg
的变量提供给JavaScript。
Yes, and you already have it. 是的,您已经拥有了。
Look closer at what's happening here. 仔细看看这里发生的事情。 You are making an Ajax request to a php file.
您正在向php文件发出Ajax请求。 That file processes the $_POST variable and outputs it using
var_dump()
. 该文件处理$ _POST变量,并使用
var_dump()
其输出。 Since the came from jQuery, the response goes back to jQuery. 由于来自jQuery,因此响应可以返回到jQuery。 You have used an
alert()
to display that message. 您已使用
alert()
显示该消息。
In your php script, you can use $_POST['title']
to create your curl request. 在您的PHP脚本中,您可以使用
$_POST['title']
创建curl请求。
Please be careful that you sanitize or validate your input so you don't create an injection hole. 请注意清理或验证输入内容,以免造成注入孔。 Don't take user input and run it directly.
不要接受用户输入并直接运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.