沃梦达 / 编程问答 / php问题 / 正文

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

Pass data from jQuery to PHP for an ajax post(将数据从 jQuery 传递到 PHP 以用于 ajax 帖子)

本文介绍了将数据从 jQuery 传递到 PHP 以用于 ajax 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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.

推荐答案

With jQuery post你可以定义一个回调函数,当数据返回时执行:

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

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

data 的格式应该是:

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

这等同于 PHP 端的帖子名称,可以像这样在纯 PHP 中访问:

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

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

这篇关于将数据从 jQuery 传递到 PHP 以用于 ajax 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将数据从 jQuery 传递到 PHP 以用于 ajax 帖子

基础教程推荐