Javascript 将数据从 jQuery 传递到 PHP 以用于 ajax 帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:23:38  来源:igfitidea点击:

Pass data from jQuery to PHP for an ajax post

phpjavascriptjqueryajaxpost

提问by macha

Hello I am a newbie working with jQuery and Ajax. I am trying submit data to the server using Jquery POST method. And the data that I am passing is a string. Now I am unable to understand how do I pass the data and how do I retrieve the data. I have tried searching for articles for my problem, but I have found none. I believe my problem is very basic.

你好,我是一个使用 jQuery 和 Ajax 的新手。我正在尝试使用 Jquery POST 方法向服务器提交数据。我传递的数据是一个字符串。现在我无法理解如何传递数据以及如何检索数据。我曾尝试为我的问题搜索文章,但没有找到。我相信我的问题是非常基本的。

if (1)//validateStep(step)
{
if(step==1)
{
var data = document.getElementById('hiddenContact').value;
$.post('/callcenter/admin/postContacts', data);
}
}

Now I'll post the code for my postContacts action, which isn't a big thing.

现在我将发布 postContacts 操作的代码,这不是什么大事。

function postContacts()
{
$this->autoRender = false;
echo '<script>console.log("post contacts");</script>';
}

But I am confused as to how the data has to be retrieved. Any help is appreciated. I am using cakePHP, so I have had to use autoRender=false; which makes the view optional.

但我对如何检索数据感到困惑。任何帮助表示赞赏。我正在使用 cakePHP,所以我不得不使用 autoRender=false; 这使得视图可选。

回答by Marcus Whybrow

With jQuery postyou can define a callback function which is executed when the data is returned:

使用jQuery post,您可以定义一个回调函数,该函数在返回数据时执行:

$.post('/callcenter/admin/postContacts', data, function(returnedData) {
    // do something here with the returnedData
    console.log(returnedData);
});

The datashould be in the form:

data应在形式:

{name: 'value', anotherName: 'another value'}

which equates to the post names on the PHP end accessible in plain PHP like this:

这等同于 PHP 端的帖子名称,可在纯 PHP 中访问,如下所示:

echo $_POST['name'];           # prints "value"
echo $_POST['anotherName'];    # print "another value"

回答by mike

The data param is supposed to be an object that has keys and values.

数据参数应该是一个具有键和值的对象。

var data = {
    hiddenContact: document.getElementById('hiddenContact').value
}
$.post('/callcenter/admin/postContacts', data);

Then in PHP you can retrieve it like this:

然后在 PHP 中,您可以像这样检索它:

$hiddenContact = $_POST["hiddenContact"];

I'm not a big CakePHP user but I believe the CakePHP version is like this:

我不是 CakePHP 的大用户,但我相信 CakePHP 版本是这样的:

$hiddenContact = $this->params["hiddenContact"];

回答by Jason Benson

//javascript
if(step==1)
{
   var data = {'MyFieldName':document.getElementById('hiddenContact').value};
   $.post('/callcenter/admin/postContacts', data, function(returnData){
      alert('The server said ' + returnData);
   });
}

//read the post in php
<?
   echo 'Your ajax post data was '. $_POST['MyFieldName'];
?>