将数据从 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 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)