从jQuery的数据传递到PHP的一个ajax职位 [英] Pass data from jQuery to PHP for an ajax post

查看:154
本文介绍了从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);
}
}

现在我会后的code我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>';
}

不过,我很困惑,如何将数据已被检索。任何帮助是AP preciated。我使用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.

推荐答案

通过 jQuery的帖子可以定义一个回调功能时,返回的数据,执行:

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);
});

数据应该是形式:

{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职位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆