我有一个通过AJAX在fancybox中加载的表单。表单使用ajaxForm提交,数据保存到数据库中。我唯一的问题是成功'没有调用ajaxForm的函数。
来自“网络”标签中的来电的响应
<script>
$(document).ready(function() {
// submit form
$('#edit_letting_task').ajaxForm({
dataType: 'json',
success: processJsonEditLettingTask
});
function processJsonEditLettingTask(data) {
// 'data' is the json object returned from the server
alert(data.message);
}
});
</script>
{ "message": "Test" }
edit_letting_task.php代码(原始代码);
<?php
require_once('../inc/app_top_cron.php');
?>
<script>
$(document).ready(function() {
// submit form
$('#edit_letting_task').ajaxForm({
dataType: 'json',
success: processJsonEditLettingTask
});
function processJsonEditLettingTask(data) {
// 'data' is the json object returned from the server
alert(data.message);
}
});
</script>
<?php
// process form
if (!empty($_POST)) {
// define and escape each POST as a variable
foreach ($_POST as $key => $value) {
if (!empty($value)) {
${$key} = mysqli_real_escape_string($con, PrepareInput($value));
}
}
// insert data into database
$sql="UPDATE letting_task SET
letting_task_status = '$letting_task_status',
letting_task_notes = '$letting_task_notes'
WHERE letting_task_id = '$letting_task_id'
";
if (!mysqli_query($con, $sql)) {
die('Error Inserting Letting Task Data into Database: ' . mysqli_error($con));
} else {
echo '{ "message": "Test" }';
}
} else {
// define and escape each GET as a variable
if (!empty($_GET)) {
foreach ($_GET as $key => $value) {
if (!empty($value)) {
${$key} = mysqli_real_escape_string($con, PrepareInput($value));
}
}
}
$letting_task_query = mysqli_query($con, "SELECT * FROM letting_task WHERE letting_task_id='" . $letting_task_id . "'") or die(mysql_error());
$letting_task_details = mysqli_fetch_array( $letting_task_query );
// display form
echo '<form id="edit_letting_task" name="edit_letting_task" action="' . SITE_AJAX . 'edit_letting_task.php" method="post">';
echo '<input type="hidden" id="letting_task_id" name="letting_task_id" value="' . $letting_task_id . '">';
// letting task details
echo '<fieldset>
<legend>' . $letting_task_details['letting_task_name'] . '</legend>
<table class="nobord">';
echo '<tr>
<td><label for="letting_task_status">Status:</label></td>
<td>';
echo '<select id="letting_task_status" name="letting_task_status" required>';
$letting_task_status_query = mysqli_query($con, "SELECT * FROM letting_task_status ORDER BY letting_task_status_id ASC");
while ($letting_task_status_row = mysqli_fetch_array($letting_task_status_query)) {
if ($letting_task_status_row['letting_task_status_id'] == $letting_task_details['letting_task_status']) { $selected = ' selected'; } else { $selected = ''; }
echo '<option value="' . $letting_task_status_row['letting_task_status_id'] . '"' . $selected . '>' . $letting_task_status_row['letting_task_status_name'] . '</option>';
}
echo '</select>';
echo '</td></tr>';
echo '<tr>
<td><label for="letting_task_notes">Notes:</label></td>
<td><textarea id="letting_task_notes" name="letting_task_notes" rows="6" cols="40">' . $letting_task_details['letting_task_notes'] . '</textarea></td>
</tr>';
echo '</table>
</fieldset>';
// actions
echo '<fieldset><legend>Actions</legend>
<table class="nobord">';
echo '<tr><td><input type="submit" value="Save"></td></tr>';
echo '</table>
</fieldset>';
echo '</form>';
}
mysqli_close($con);
?>
edit_letting_task.php代码(html输出);
<script>
$(document).ready(function() {
// submit form
$('#edit_letting_task').ajaxForm({
dataType: 'json',
success: processJsonEditLettingTask
});
function processJsonEditLettingTask(data) {
// 'data' is the json object returned from the server
alert(data.message);
}
});
</script>
<form id="edit_letting_task" name="edit_letting_task" action="https://www.property-system-uk.com/admin-area/ajax/edit_letting_task.php" method="post"><input type="hidden" id="letting_task_id" name="letting_task_id" value="1"><fieldset>
<legend>Application to let received</legend>
<table class="nobord"><tr>
<td><label for="letting_task_status">Status:</label></td>
<td><select id="letting_task_status" name="letting_task_status" required><option value="1">Pending</option><option value="2" selected>In Progress</option><option value="3">Completed</option></select></td></tr><tr>
<td><label for="letting_task_notes">Notes:</label></td>
<td><textarea id="letting_task_notes" name="letting_task_notes" rows="6" cols="40"></textarea></td>
</tr></table>
</fieldset><fieldset><legend>Actions</legend>
<table class="nobord"><tr><td><input type="submit" value="Save"></td></tr></table>
</fieldset></form>
答案 0 :(得分:1)
最好为您的ajax请求创建一个单独的php端点,将表单数据添加到数据库中,并将响应作为JSON返回(因为这就是您所要求的)。
问题不在你的JS中,就像你的AJAX实现一样。您应该获得响应,然后使用数据更新页面的dom或显示成功警报等,具体取决于您要执行的操作。
您的AJAX端点唯一应该响应的是JSON响应,没有别的。
有关如何使用JSON进行响应的详细信息,请在堆栈上查看此问题,因为您现在根本没有这样做。 Returning JSON from a PHP Script