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