2016-06-18 181 views
0

我有一個窗體,正在加載通過AJAX內fancybox。表單使用ajaxForm提交,數據保存到數據庫中。我唯一的問題是沒有調用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

你如何發送回覆?你在網絡標籤中看到什麼?你是否得到200或500的迴應? – Jacques

+0

@Jacques網絡標籤看起來不錯,獲得200響應,數據庫更新,因爲它應該 –

+0

在您的網絡選項卡中該呼叫的響應是什麼? – Jacques

回答

1

這將是最適合你的,增加了表單數據到數據庫並返回響應爲JSON(因爲這是你問的)你的Ajax請求創建一個單獨的PHP端點。

這個問題不在你的JS中說,就像你的AJAX實現一樣。您應該獲得響應,然後使用數據更新頁面的dom或顯示成功警報等,具體取決於您想要執行的操作。

您的AJAX端點應迴應的唯一一件事是JSON響應,沒有別的。

在堆棧上查看此問題以獲取有關如何使用JSON響應的詳細信息,因爲您現在根本沒有這樣做。 Returning JSON from a PHP Script

相關問題