为什么我的jQuery .ajaxForm代码不起作用?

时间:2015-10-26 10:06:20

标签: php jquery ajax

我希望有人可以帮助我...

基本上,if (data.status=='error') {} else if (data.status=='success') {似乎都没有被调用,但数据正好被插入到数据库中。

add_alarm_code.php;

<?php

require_once('../inc/config.php');

$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

// define and escape each POST as a variable

foreach ($_POST as $key => $value) {

if (!empty($value)) {

${$key} = mysqli_real_escape_string($con, $value);

}
}

// insert data into database

$sql="INSERT INTO alarm_code (
alarm_code_property,
alarm_code_code,
alarm_code_notes,
alarm_code_date_added
)
VALUES (
'$alarm_code_property',
'$alarm_code_code',
'$alarm_code_notes',
'" . date('Y-m-d') . "'
)";

if (!mysqli_query($con,$sql)) {
echo '{ "status": "error", "message": "Error Inserting Alarm Code Data into Database: ' . mysqli_error($con) . '" }';
} else {
echo '{ "status": "success", "message": "The alarm code has successfully been added to the system. The page will now refresh." }';
}

mysqli_close($con);

?>

jQuery on view_property.php;

// add alarm code submit form
$('#add_alarm_code').ajaxForm({ 
dataType: 'json',
success: processJson,
error: function (xmlRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});

function processJson(data) { 

if (data.status=='error') {

alert('Error 1');
$('#ajax_form_status_alarm_code').removeClass('green_background')
$('#ajax_form_status_alarm_code').addClass('red_background')
$('#ajax_form_status_alarm_code').html(data.message);
alert('Error 2');

} else if (data.status=='success') {

$('#add_alarm_code').clearForm();
$('#ajax_form_status_alarm_code').removeClass('red_background')
$('#ajax_form_status_alarm_code').addClass('green_background')
$('#ajax_form_status_alarm_code').html(data.message);

setTimeout(function() {

var currentURL = window.location.href;
var currentHashValue = window.location.hash;

if (currentHashValue!='') {

    location.reload(true);

} else {

    window.location.href += "#keys";
    location.reload(true);

}
}, 3000);

}
}
view_property.php上的

表单(表单在fancybox / lightbox中加载时隐藏在div中)

echo '<div style="display:none">';

echo '<form id="add_alarm_code" action="' . SITE_AJAX . 'add_alarm_code.php" method="post">';

echo '<input type="hidden" id="alarm_code_property" name="alarm_code_property" value="' . $property_id . '">';

echo '<fieldset><legend>Add an Alarm Code</legend>
<table class="nobord">';

echo '<tr><td><label for="alarm_code_code">Alarm Code:</label></td>
<td><input type="text" id="alarm_code_code" name="alarm_code_code" maxlength="10" required></td></tr>';

echo '<tr><td><label for="alarm_code_notes">Notes:</label></td>
<td><textarea id="alarm_code_notes" name="alarm_code_notes" rows="6" cols="35"></textarea></td></tr>';

echo '<tr><td colspan="2"><input type="submit" value="Save"></td></tr>';

echo '</table>';

echo '<div id="ajax_form_status_alarm_code" class="ajax_form_status"></div>';

echo '</fieldset></form>';

echo '</div>';

view_property.php上的整个jQuery(如果它有助于发现错误!);

<script>
$(document).ready(function() {

// tab functionality on load
$(function(e) {

// define variables
var currentAttrValue = window.location.hash;

if (currentAttrValue!='') {

// show/hide tabs
jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();

// change/remove current tab to active
jQuery(currentAttrValue).parent('li').addClass('active').siblings().removeClass('active');

e.preventDefault();

}

});

// tab functionality on click
jQuery('.tabs .tab-links a').on('click', function(e)  {
var currentAttrValue = jQuery(this).attr('href');

// show/hide tabs
jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();

// change/remove current tab to active
jQuery(this).parent('li').addClass('active').siblings().removeClass('active');

e.preventDefault();

});

// fancybox
$(".fancybox").fancybox({
closeBtn        : true,
modal           : false
});

// apply validation
$( "#add_certificate" ).validate({
errorClass: "error-class",
});
$( "#add_keys" ).validate({
errorClass: "error-class",
});
$( "#add_alarm_code" ).validate({
errorClass: "error-class",
});

// add certificate submit form
$('#add_certificate').ajaxForm({ 
dataType: 'json',
success: processJson,
});

function processJson(data) { 

if (data.status=='error') {

$('#ajax_form_status_certificate').removeClass('green_background')
$('#ajax_form_status_certificate').addClass('red_background')
$('#ajax_form_status_certificate').html(data.message);

} else if (data.status=='success') {

$('#add_certificate').clearForm();
$('#ajax_form_status_certificate').removeClass('red_background')
$('#ajax_form_status_certificate').addClass('green_background')
$('#ajax_form_status_certificate').html(data.message);

setTimeout(function(){

var currentURL = window.location.href;
var currentHashValue = window.location.hash;

if (currentHashValue!='') {

    location.reload(true);

} else {

    window.location.href += "#certificates";
    location.reload(true);

}
}, 3000); 
}
}

$( "#add_1_year" ).click(function() {

if ($('#certificate_start_date').val()!='') {

var expiry_date = moment($('#certificate_start_date').val()).add(1, 'year').format("YYYY-MM-DD");
$('#certificate_expiry_date').val(expiry_date);

} else {

alert('Please enter the certificate start date.');

}

});

// add alarm code submit form
$('#add_alarm_code').ajaxForm({ 
dataType: 'json',
success: processJson,
error: function (xmlRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});

function processJson(data) { 

if (data.status=='error') {

alert('Error 1');
$('#ajax_form_status_alarm_code').removeClass('green_background')
$('#ajax_form_status_alarm_code').addClass('red_background')
$('#ajax_form_status_alarm_code').html(data.message);
alert('Error 2');

} else if (data.status=='success') {

$('#add_alarm_code').clearForm();
$('#ajax_form_status_alarm_code').removeClass('red_background')
$('#ajax_form_status_alarm_code').addClass('green_background')
$('#ajax_form_status_alarm_code').html(data.message);

setTimeout(function() {

var currentURL = window.location.href;
var currentHashValue = window.location.hash;

if (currentHashValue!='') {

    location.reload(true);

} else {

    window.location.href += "#keys";
    location.reload(true);

}
}, 3000);

}
}

// add keys submit form
$('#add_keys').ajaxForm({ 
dataType: 'json',
success: processJson,
});

function processJson(data) { 

if (data.status=='error') {

$('#ajax_form_status_keys').removeClass('green_background')
$('#ajax_form_status_keys').addClass('red_background')
$('#ajax_form_status_keys').html(data.message);

} else if (data.status=='success') {

$('#add_keys').clearForm();
$('#ajax_form_status_keys').removeClass('red_background')
$('#ajax_form_status_keys').addClass('green_background')
$('#ajax_form_status_keys').html(data.message);

setTimeout(function(){

var currentURL = window.location.href;
var currentHashValue = window.location.hash;

if (currentHashValue!='') {

    location.reload(true);

} else {

    window.location.href += "#keys";
    location.reload(true);

}
}, 3000);

}
}

}); 
</script>

0 个答案:

没有答案