[英]Not able to get post data from ajax to php
我真的需要你们的帮助。 我无法从ajax访问php中的发布数据。 我不知道这是怎么回事。 我尝试在stackoverflow中搜索解决方案,但没有一个对我有帮助。 这是我的剧本,..
<script>
$('form.ajax').on('submit',function() {
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
console.log(data.name);
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
}
});
return false;
});
</script>
这是我的PHP代码,
<?php
$con=mysqli_connect("localhost","username","password","databasename");
if(mysqli_connect_errno())
{
echo "failed to connect to MySQL:"+mysqli_connect_error();
die();
}
$myQuery = "SELECT * FROM Invoice";
$result1 = mysqli_query($con,$myQuery);
if(isset($_POST['name'])){
$name = $_POST['name'];
$query = "SELECT * FROM Invoice WHERE InvoiceNumber LIKE '%".$name."'";
$result1 = mysqli_query($con,$query);
}
$data1 = array();
while($row = mysqli_fetch_array($result1)) {
$row_data = array(
'InvoiceNumber' => $row['InvoiceNumber'],
'InvoicePONumber' => $row['InvoicePONumber']
);
array_push($data1,$row_data);
}
echo json_encode($data1);
?>
$.ajax
的默认请求方法是GET,因此您永远不会在$_POST
var中获得任何结果。 相反,您应该使用$.post
或将请求类型添加到您的ajax
调用中。
$.ajax({
...
type: 'POST'
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.