繁体   English   中英

为什么我的ajaxForm的成功函数没有被调用?

[英]Why is the success function of my ajaxForm not being called?

我有一个通过fancybox内的AJAX加载的表单。 使用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>

最好为ajax请求创建一个单独的php端点,该端点将表单数据添加到数据库中并以JSON形式返回响应(因为这就是您要的内容)。

说到底,这个问题并不像您的AJAX实施那么重要。 您应该获得响应,然后使用数据更新页面的dom或显示成功警报等,具体取决于您要执行的操作。

您的AJAX端点唯一可以响应的就是JSON响应,仅此而已。

有关如何使用JSON进行响应的详细信息,请参见堆栈上的此问题,因为您现在根本不这样做。 从PHP脚本返回JSON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM