好吧,這讓我瘋了,我有這個工作,但它似乎沒有抓住我的ajax調用在我的PHP文件中的任何變量..我回聲,只看到沒有價值。 ..任何人都可以解釋什麼即時做錯了,我找不到任何錯誤,,Ajax調用將不起作用
var owner_name = $('#owner_name').html();
var owner_email = $('#owner_email').html();
var owner_mobile = $('#owner_mobile').html();
var owner_phone = $('#owner_phone').html();
function jobcardCreate() {
$.ajax({
dataType: 'json',
url: './ajax/jobcard-send.php',
type: 'POST',
data: {
owner_name: owner_name,
owner_email: owner_email,
owner_mobile: owner_mobile,
owner_phone: owner_phone,
},
success: function(data) {
}
});
}
jobcard-send.php:
<?php
include("../includes/connection.php");
$owner_name = $_POST['owner_name'];
$owner_email = $_POST['owner_email'];
$owner_mobile = $_POST['owner_mobile'];
$owner_phone = $_POST['owner_phone'];
// Insert new data into users table
$query = " INSERT INTO";
$query .= " owner";
$query .= " (owner_name, owner_email, owner_mobile, owner_phone)";
$query .= " VALUES";
$query .= " ('{$owner_name}', '{$owner_email}', '{$owner_mobile}', '{$owner_phone}')";
echo $owner_name;
echo $query;
$result = mysqli_query($connection, $query);
//Check if there was an error with the query
if ($result) {
echo json_encode(array("status" => "$owner_name"));
} else {
echo "Database query failed, " . mysqli_error($connection) . "";
}
?>
浪費了很多很多的時間在這個已經,之後我曾經有過工作,:/
這是htm L:
<div class="card-content one-third">
<div class="list-title thin">Owner Contact</div>
<ul class="detail-list">
<li class="button rounded"><a class="list-heading">Name:</a> <a id="owner_name" class="list-result-edit" contenteditable="true"></a></li>
<li class="button rounded"><a class="list-heading">Email:</a> <a id="owner_email" class="list-result-edit" contenteditable="true"></a></li>
<li class="button rounded"><a class="list-heading">Mobile No:</a> <a id="owner_mobile" class="list-result-edit" contenteditable="true"></a></li>
<li class="button rounded"><a class="list-heading">Phone No:</a> <a id="owner_phone" class="list-result-edit" contenteditable="true"></a></li>
</ul>
</div>
您是否在瀏覽器控制檯中觀看過AJAX請求/響應? #owner_name et.al.輸入框?如果是這樣,你應該使用'.val()'而不是'.html()' –
是的,它運行查詢罰款,並將新的數據添加到數據庫..但只是空白..因爲它不拉在我的POST變量從ajax調用。 – 22Ryann
**危險**:您很容易[SQL注入攻擊](http://bobby-tables.com/)**,您需要[防禦](http://stackoverflow.com/questions/ 60174/best-way-to-prevent-sql -injection-in-php)自己從。 – Quentin