【问题标题】:AJAX and Jquery auto form update not workingAJAX 和 Jquery 自动表单更新不起作用
【发布时间】:2016-10-27 08:25:04
【问题描述】:

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

<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 行中的相应值返回到另一个字段中,AND VICE -VERSA。

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

【问题讨论】:

    标签: jquery sql json ajax wordpress


    【解决方案1】:

    $.ajax 调用问题, 我检查了您的 ajax 调用,发现您的应用程序不接受 GET url,因此在您的 $.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';
        }
        ?>
    

    【讨论】:

    • 日期只是在焦点消失时消失。奇怪
    • tat 可能是因为$data 为空。检查您的数据库记录。
    • 表中没有$data行,分别命名为“greg_date”和“heb_date”
    • 你能在这里写一个数据库记录的值吗?
    • 是的,数据库如下:标题是 heb_date 和 greg_date。 '3rd of Av 5782', '7/31/2022'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多