jQuery的POST数据到PHP脚本,并在同一个PHP脚本文件,立即显示出来 [英] jquery post data to a php script and display it immediately in the same php script file
问题描述
如何将数据发布使用Ajax PHP脚本(showitemid.php)和一个超链接的点击和显示器发布的数据ThickBox时立即打开相同的脚本(showitemid.php)。下面是我的code:
postitemid.php
本文件由多个的复选框。用户将勾选复选框并单击超链接。在单击全部选中复选框值将被张贴到showitemid.php,然后立即showitemid.php将在ThickBox打开并显示所接收到的值的超链接。但它没有收到我的code数值?需要帮助。
$('#showitem)。点击(函数()
{
VAR数据= $('输入:复选框:选中)图(函数(){。
返回THIS.VALUE;
})。得到();
$阿贾克斯({类型:POST,
网址:showitemid.php,
数据:数据成功:成功的dataType:数据类型});
});
showitemid.php
$数据='';
如果(使用isset($ _ POST ['数据']))
{
$数据= $ _ POST ['数据'];
}
ELSEIF(使用isset($ _ GET ['数据']))
{
$数据= $ _GET ['数据'];
}
回声D ='$的数据。
与原来的code的主要问题是发送方没有密钥名为数据数据
匹配 $ _ POST ['数据'])
让 $ _ POST ['数据'])
是空的。你必须发送键/值对,你只发送不带键的值。你很可能会变得有点糊涂了,因为你不断用相同的变量名
VAR dataArray中= $('输入:复选框:选中)图(函数(){。
返回THIS.VALUE;
})。得到();
VAR dataToServer = {数据:dataArray中} / *现在的关键是在PHP *匹配$ _REQUEST /
现在可以使用AJAX速记方法负载()
来填充内容
$('#myContainer中)。负载(yourfile.php,dataToServer,函数(){
/ *新的HTML这里存在运行开放的ThickBox code * /
})
How to post data to a php script (showitemid.php) using ajax and open the same script (showitemid.php) immediately in a thickbox on a hyperlink click and display that posted data. Below is my code:
postitemid.php
This file consists of multiple check-boxes. The user will tick the checkboxes and click a hyperlink. On clicking the hyperlink all the selected check-box values would be posted to showitemid.php and then immediately showitemid.php would open in a thickbox and display the received values. But it isn't receiving any values in my code ? Need help.
$('#showitem).click(function()
{
var data = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
$.ajax({type: 'POST',
url: 'showitemid.php',
data: data,success: success,dataType: dataType});
});
showitemid.php
$data = '';
if (isset($_POST['data']))
{
$data = $_POST['data'];
}
elseif (isset($_GET['data']))
{
$data = $_GET['data'];
}
echo 'd='.$data;
The main problem with the original code is the data being sent has no key named data
to match $_POST['data'])
so $_POST['data'])
is empty. You have to send key/value pairs, you are only sending a value with no key. You are likely getting a bit confused since you use the same variable name constantly
var dataArray = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
var dataToServer= { data : dataArray} /* now have the key to match $_REQUEST in php */
Now can use AJAX shorthand method load()
to populate content
$('#myContainer').load( "yourfile.php", dataToServer, function(){
/* new html exists run open thickbox code here*/
})
这篇关于jQuery的POST数据到PHP脚本,并在同一个PHP脚本文件,立即显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!