向ajax发送数据
Sending data to ajax
使用ajax将数据从表单发送到PHP。当我们更改单选按钮时,数据不清除。请检查代码。
这是密码,请帮帮我。这是我的from
<form name="theform" action="" method="post" id="paymentHistory">
<div class="co1">Type:
<table width="240">
<tr>
<td>
<input type="checkbox" name="Successfull" value='Successfull'>
</td>
<td>Successfull</td>
<td>
<input type="checkbox" name="Failure" value='Failure' />
</td>
<td>Failure</td>
</tr>
</table>
</div>
<div class="co">Data Range:
<table width="422">
<table>
<div>
<tr>
<td style="width: 8px;">
<input type="radio" name="Today" id="Today" value='Today'>
</td>
<td style="width: 80px;">
<label for="Today">Today</label>
</td>
<td style="width: 8px;">
<input type="radio" name="Today" id="Lastweek" value='Lastweek'>
</td>
<td style="width: 80px;">
<label for="Lastweek">Lastweek</label>
</td>
<td style="width: 8px;">
<input type="radio" name="Today" id="30days" value='30days'>
</td>
<td style="width: 80px;">
<label for="30days">30days</label>
</td>
</table>
</div>
</form>
这是脚本。
<script type="text/javascript">
$(document).ready(function () {
$('#paymentHistory input:radio').click(function () {
if (this.checked) {
var opts = [];
opts.push($('#paymentHistory input:radio:checked').val());
$('#paymentHistory :checkbox').click(function () {
alert($('#paymentHistory input:radio:checked').val())
if (this.checked) {
opts.push(this.name);
} else {
if (!this.checked) {
alert(this.name);
opts.pop(this.name)
}
}
$.ajax({
type: "POST",
url: "tabletest/submit.php",
dataType: 'json',
cache: false,
data: {
filterOpts: opts
},
success: function (records) {
$('#phones tbody').html(makeTable(records));
}
});
function makeTable(data) {
var tbl_body = "";
$.each(data, function () {
var tbl_row = "";
$.each(this, function (k, v) {
tbl_row += "<td align='center'>" + v + "</td>";
})
tbl_body += "<tr>" + tbl_row + "</tr>";
})
return tbl_body;
}
});
}
});
});
</script>
这是我的php代码数据没有得到。请帮帮我。
<?php
$select = 'SELECT *';
$from = ' FROM payment_details';
$where = ' WHERE TRUE';
$nickname = $_SESSION['username'];
$opts = $_POST['filterOpts'];
$options = explode("&",$opts);
$Today = "";
$Lastweek = "";
$days = "";
$Successfull = "";
$Failure = "";
foreach($options as $key)
{
$key = explode("=",$key);
$type = $key[2];
if ("Today" == $type)
{
$Today= "Today";
}
if ("Lastweek" == $type)
{
$Lastweek = "Lastweek";
}
if ("30days" == $type)
{
$days = "30days";
}
if ("Successfull" == $type)
{
$Successfull = "Successfull";
}
if ("Failure" == $type)
{
$Failure = "Failure";
}
}
if( $Today == "Today" )
{
date_default_timezone_set('Asia/Kolkata');
$d = date('Y-m-d 00:00:00');
$where .= " AND tran_ini_date >= '$d' ";
}
if('Lastweek' == $Lastweek)
{
$d = date('Y-m-d 00:00:00', strtotime("-7 days") );
$where .= " AND tran_ini_date >= '$d' ";
}
if('30days' == $days)
{
$d = date('Y-m-d 00:00:00', strtotime("-30 days") );
$where .= " AND tran_ini_date >= '$d' ";
}
if("30days" == $days)
{
$d = date('Y-m-d 00:00:00', strtotime("30 days") );
$where .= " AND tran_ini_date <= '$d' ";
}
$check = 0;
if('Successfull' == $Successfull)
{
$check = 1;
$where .= " AND (status='success' ";
}
if('Failure' == $Failure)
{
if($check == 0)
$where .= " AND (status='pending') ";
else
$where .= " OR status='pending') ";
}
$sql = $select . $from . $where . " and nickname="."'".$nickname."'";
$conn = mysql_connect("localhost","root","********");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("**********",$conn);
$result = mysql_query($sql,$conn);
$data = array();
while ($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
$json = json_encode($data);
echo($json);
echo $sql;
?>
您的代码中有错误,在:单选单击事件中使用:复选框单击的方法是不正确的。请参阅修改后的代码,这些代码应该适合您:
$(document).ready(function ()
{
makeTable = function(data)
{
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k , v) {
tbl_row += "<td align='center'>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
};
var pushToServer = function(opts){
alert(opts);
$.ajax({
type: "POST",
url: "tabletest/submit.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(records){
$('#phones tbody').html(makeTable(records));
}
});
};
$('#paymentHistory input:radio, #paymentHistory input:checkbox').click(function() {
pushToServer($('#paymentHistory').serialize());
});
});
演示相关文章:
- Ajax-如何获取数据
- JS验证ajax返回的html中的表单数据
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 使用ajax将数据从一个步骤发送到下一个步骤的3步表单
- node.js请求数据事件未在CORS ajax调用中触发
- React ajax数据集成
- 发送和接收数据?Ajax、Jquery和PHP
- 从返回的数据 ajax post 创建一个变量
- Laravel在成功后附加数据ajax
- Python - 从服务器获取数据 (ajax) - 'str' 对象不是可调用的错误
- 阻止执行数据 ajax 属性
- 表单中的文本框数据-AJAX提交时未传递
- 发送数据ajax php
- 加载更多数据ajax PHP onscroll
- Rails 4数据ajax路由问题
- 将数据ajax传递到每个页面的同一个页面
- 发送图像+数据ajax post到ashx处理程序
- 传递购物车数据…Ajax或PHP Post
- 将键值对作为单独的数据Ajax发布
- 使用数据Ajax发送