0
我真的需要你的帮助。我无法从ajax访问php中的发布数据。我不知道这里发生了什么。我试图在stackoverflow中搜索解决方案,但他们都没有帮助我。这里是我的脚本..无法从ajax获取发布数据到php
<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);
?>
在发布前习惯于从代码中移除登录凭证。你不希望别人利用这一点。 – RST
很高兴看到您正在使用code academy的脚本。我一直使用它,节省代码编写 – Brad
为什么你想获取发布数据?为什么不把发布数据放到数据库表中呢? – Brad