【发布时间】:2023-03-27 12:10:02
【问题描述】:
当我点击按钮使用下面的 ajax 代码提交我的表单时,没有任何反应,没有任何警报被触发来告诉我是否有错误或成功,所以我不确定如何知道是什么发生了,无论我尝试什么,我都无法触发警报。
我已将直接 url 放入表单并绕过了 ajax,所以我知道 php 页面工作正常,只是通过 AJAX 代码传递时似乎没有获取数据
有什么方法可以查看传递到 php 页面的数据?
$(document).ready(function() {
$('form.submit').submit(function() {
var name = $(this).find('.name').attr('value');
var address = $(this).find('.address').attr('value');
var number = $(this).find('.number').attr('value');
var price = $(this).find('.price').attr('value');
var deposit = $(this).find('.deposit').attr('value');
var product = $(this).find('.product').attr('value');
var payment_type = $(this).find('.payment_type').attr('value');
var deal_date = $(this).find('.deal_date').attr('value');
var install_date = $(this).find('.install_date').attr('value');
var installed = $(this).find('.installed').attr('value');
var notes = $(this).find('.notes').attr('value');
var contract_received = $(this).find('.contract_received').attr('value');
var id = $(this).find('.id').attr('value');
// ...
$.ajax({
type: "POST",
url: "http://www.thinkecosolutions.co.uk/test/services/update.php",
data: $('form.submit').serialize(), // or just: data: $(this).serialize(),
success: function() {
alert('success');
},
error: function() {
alert('failure');
}
});
return false;
});
});
好的,因此使用了开发人员工具,很明显代码没有进入 php 页面,所以这一定是我构建 js 页面的方式,并且表单可能没有看到 ajax 部分?下面是我的js页面。
$('#detailsPage1').live('pageshow', function(event) {
var id = getUrlVars()["id"];
$.getJSON(serviceURL + 'getemployee.php?id='+id, displayEmployee1);
});
function displayEmployee1(data) {
var employee = data.item;
console.log(employee);
$('#fullName').html('<form action="" id="submit" class="submit"><label>Name</label><br><input name="name" class="form-control" type="text" placeholder="Name" value="' + employee.name + '"/><br><label>Number</label><br><input name="number" class="form-control" type="text" placeholder="Number" value="' + employee.number + '"/><br><label>Address</label><br><input name="address" class="form-control" type="text" placeholder="Address" value="' + employee.address + '"/><br><label>Price</label><br><input name="price" class="form-control" type="text" placeholder="Price" value="' + employee.price + '"/><br><label>Deposit</label><br><input name="deposit" class="form-control" type="text" placeholder="Deposit" value="' + employee.deposit + '"/><br><label>Payment Type</label><br><input name="payment_type" class="form-control" type="text" placeholder="Payment type" value="' + employee.payment_type + '"/><br><label>Deal Date</label><br><input name="deal_date" class="form-control" type="text" placeholder="Deal Date" value="' + employee.deal_date + '"/><br><label>Install Date</label><br><input name="install_date" class="form-control" type="text" placeholder="Install Date" value="' + employee.install_date + '"/><br><label>Installed</label><br><input name="installed" class="form-control" type="text" placeholder="Installed" value="' + employee.installed + '"/><br><label>Notes</label><br><input name="notes" class="form-control" type="text" placeholder="Notes" value="' + employee.notes+ '"/><br><input name="id" id="id" type="hidden" value="' + employee.id + '" /><br><label>Contract Received</label><br><input name="contract_received" class="form-control" type="text" placeholder="Contract Received" value="' + employee.contract_recieved + '"/><br><br><input type="submit" name="button" id="button" value="Update" /></form>');
$(document).ready(function(){
$('form.submit').submit(function () {
var name = $(this).find('.name').attr('value');
var address = $(this).find('.address').attr('value');
var number = $(this).find('.number').attr('value');
var price = $(this).find('.price').attr('value');
var deposit = $(this).find('.deposit').attr('value');
var product = $(this).find('.product').attr('value');
var payment_type = $(this).find('.payment_type').attr('value');
var deal_date = $(this).find('.deal_date').attr('value');
var install_date = $(this).find('.install_date').attr('value');
var installed = $(this).find('.installed').attr('value');
var notes = $(this).find('.notes').attr('value');
var contract_received = $(this).find('.contract_received').attr('value');
var id = $(this).find('.id').attr('value');
// ...
$.ajax({
type: "POST",
url: "http://www.thinkecosolutions.co.uk/test/services/update.php",
data: $('form.submit').serialize(), // or just: data: $(this).serialize(),
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
return false;
});
});
console.log(employee.telephone);
if (employee.notes) {}
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
【问题讨论】:
-
你试过浏览器的检查器吗? Firefox 和 Chrome(我更喜欢 Chrome)都有非常好的网络检查器。它们都有一个名为“网络”的部分,向您显示发送和接收的内容。右键单击网页的一部分并选择“检查”(或类似)以调出检查器。这还将让您看到显示错误消息的控制台。
-
您可能会在调用 ajax 之前提交实际表单(刷新页面),试试
$('form.submit').submit(function(e) { e.preventDefault();