AJAX和Jquery自动表单更新无法正常工作

时间:2016-10-27 08:25:04

标签: jquery sql json ajax wordpress

这就是我在表单页面上的内容:

<form action = "/register-process" method = "post" id="form1">
<script type="text/javascript">
jQuery(document).ready(function($) {
$(document).on('focusout', '#dateenglish', function () {
    var dateenglish = $(this).val();
    $.ajax({
        url: 'http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/getdate.php',
        data: {
            'dateenglish': dateenglish
        },
        success:function(data) {
            // This outputs the result of the ajax request
            $('#dateenglish').val(data);
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
});
});
</script>
<input id = "dateenglish" class="form_area" required name = "dateenglish" required type = "text" value="<?= $date; ?>" />
<input id = "datehebrew" class="form_area" required name = "datehebrew" required type = "text" value="<?= $date; ?>"  />
</form>

这就是我在getpage.php中所拥有的:

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );
// Do the following if the form submits
if (isset($_REQUEST['dateenglish'])) {
$date = $_REQUEST['dateenglish'];

global $wpdb;

$stmt = "SELECT
heb_date,
greg_date
FROM calendar
WHERE greg_date = '".$date."'
OR heb_date = '".$date."'
";

$myrows = $wpdb->get_results($stmt);

// Not sure if its an array that is returned, but this will get the first value.
$row = current($myrows);

// Let's check which date is different
if ($date != $row['greg_date']) {
    $date = $row['greg_date'];
} else {
    $date = $row['heb_date'];
}

echo $date;
} else {
echo 'error';
}
?>

我要做的就是让表单从用户输入的“日历”表中查找日期数组,对照数据库进行检查,并将第2行中的相应值返回到另一个字段,并且VICE -VERSA。

即。有人输入一件事,它检查,添加另一件然后它保持。现在它不会留在现场。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

$ .ajax Call问题, 我检查了您的ajax调用,发现您的应用程序不接受GET网址,因此在您的$.ajax电话method : "POST"中再添加一个参数,它就能正常运行。

您可以尝试以下代码

    jQuery(document).ready(function($) {
    $(document).on('focusout', '#dateenglish', function () {
        var dateenglish = $(this).val();
        $.ajax({
            url: 'http://www.mountsinaiparks.org/digital-yahrzeit/wp-content/themes/yahrzeit-theme/getdate.php',
            method : "POST",
            data: {
                'dateenglish': dateenglish
            },
            success:function(data) {
                // This outputs the result of the ajax request
                $('#dateenglish').val(data);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });
    });
    });

更新Php

    <?php
    include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );
    // Do the following if the form submits
    if (isset($_REQUEST['dateenglish'])) {
    $date = $_REQUEST['dateenglish'];

    global $wpdb;

    $stmt = "SELECT
    heb_date,
    greg_date
    FROM calendar
    WHERE greg_date = '".$date."'
    OR heb_date = '".$date."'
    ";

    $myrows = $wpdb->get_results($stmt);

    // Not sure if its an array that is returned, but this will get the first value.

    while ($row = $myrows->fetch_field()){
        // Let's check which date is different
        if ($date != $row['greg_date']) {
            $date = $row['greg_date'];
        } else {
            $date = $row['heb_date'];
        }
        echo $date;
        return;
    }




    } else {
    echo 'error';
    }
    ?>