【问题标题】:Jquery doesn't get my value from php fileJquery 没有从 php 文件中获取我的值
【发布时间】:2013-04-23 15:16:27
【问题描述】:

我需要你的帮助。在我的 php 代码中,我创建了一个日期倒计时,它可以正常工作,但是我在 jquery 中的代码有问题。我正在尝试获取 $json_result 值。 这是我的php代码:

<?php
function addZero($addZero){
        return ($addZero < 10) ? '0'.$addZero : $addZero;
    }

header('Content-type: application/json');

$year_value = date('Y');
$end_month = $_GET['month'];
$end_day = $_GET['day'];
$end_hours = $_GET['hour'];

$date = strtotime("$year_value-$end_month-$end_day, $end_hours:00:00");
$remaining = $date - time();
$minutes = floor($remaining/60);
$hours = floor($remaining/3600);
$daysLeft = floor($remaining/86400);

if($daysLeft > 0){
        $remaining_hours = $hours - ($daysLeft * 24);
    }else{
        $remaining_hours = $hours;
    }

$remaining_minutes = floor(($remaining - ($hours * 3600))/60);
$remaining_seconds = floor($remaining - ($minutes * 60));

$result = 'Days left:&nbsp;'.$daysLeft.'&nbsp;'.addZero($remaining_hours).':'.addZero($remaining_minutes).':'.addZero($remaining_seconds);
$json_result = json_encode($result);
echo $json_result;
?>

这是我的 html jquery php 代码:

<head>
<script type="application/javascript">
jQuery(document).ready(function(){
    $("#submit").click(countDown);
});

function countDown(e){
    setTimeout(function(){
        $.get('countdown.php',function(data){
        $("#countDown").html(data);
        e.preventDefault(); 
        });
        countDown();
    },1000);    
}
</script>
</head>
<body>
<?php
$monthsArray = array(0 => '0', 1 => 'Jan.', 2 => 'Feb.', 3 => 'Mar.', 4 => 'Apr.', 5 => 'May', 6 => 'Jun.', 7 => 'Jul.', 
8 => 'Aug.', 9 => 'Sep.', 10 => 'Oct.', 11 => 'Nov.', 12 => 'Dec.');
$months = array_slice($monthsArray, date('m'), 12, true);
?>
<label>Month:</label>
<select name="month" id="month">
    <?php
        foreach ($months as $values => $keys) {
            printf('<option value="%u">%s</option>', $values, $keys);        
        }
    ?>
</select>
<?php
$daysArray = array(0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9', 10 => '10', 
11 => '11', 12 => '12', 13 => '13', 14 => '14', 15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19', 20 => '20', 21 => '21', 
22 => '22', 23 => '23', 24 => '24', 25 => '25', 26 => '26', 27 => '27', 28 => '28', 29 => '29', 30 => '30', 31 => '31');

    if(date('L') == 0 && (date('m') == 02 || date('m') == 2)){
        $days = array_slice($daysArray, date('d'), -3, true); // if it's not a leap year february has 28 days
    }elseif(date('L') == 1 && (date('m') == 02 || date('m') == 2)){
        $days = array_slice($daysArray, date('d'), -2, true); // if it's a leap year february has 29 days
    }elseif(date('m') % 2 == 0){
        $days = array_slice($daysArray, date('d'), -1, true); // even month
    }else{
        $days = array_slice($daysArray, date('d'), 31, true); // odd month
    }
?>
<label>Day:</label>
<select name="day" id="day">
    <?php
        foreach ($days as $values => $keys) {
            printf('<option value="%u">%s</option>', $values, $keys);        
        }
    ?>
</select>
<input type="submit" id="submit"  value="Submit">
<div id="countDown">
</div>
</body>

谁能告诉我我做错了什么?我正在使用 jquery 1.9.1 版。

【问题讨论】:

  • 可能在 ajax 调用完成之前重启函数。
  • @SomeSillyName 我没有从我的 index.php 文件中的 php 代码中获取 $json_result 值
  • 我认为我们需要更多信息。你在 ajax 回调中得到了什么吗?您可以将“数据”写入控制台吗?您可以删除 setTimeout 以查看是否是此问题吗?您可以从您的 php 中删除所有内容并让它 echo 1 来查看问题是否存在?
  • 您可能想阅读这篇文章并摆脱 addZero 函数stackoverflow.com/questions/1699958/…(重现 SPL 的形式有点糟糕)其次,json_encode 用于数据数组而不是字符串。你使用它的方式是错误的。 e.preventDefault();不像你想象的那样工作。您实际上正在寻找 clearTimeout() 信息在这里:stackoverflow.com/questions/8495105/cancel-javascript-timeout 查看、安装并了解如何在使用 JS 时使用您的浏览器开发工具 (F12)。并且不要使用 IE。
  • @SomeSillyName 现在我看到了问题,当我添加 console.log(data) 并删除 setTimeout 时,它说数据未定义...

标签: php jquery html get


【解决方案1】:

在你的 php 代码中删除这一行:

header('Content-type: application/json');

因此,您不必使用json encode 来输出结果。

echo $json_result;

【讨论】:

    【解决方案2】:

    删除header('Content-type: application/json');,因为您已经对返回字符串进行了json_encoding。

    更新

    在您的 countDown 函数中将 $.get 替换为 $.post

    $.post('countdown.php',function(data){
    

    发生的情况是,由于您使用的是 get 方法,因此表单值没有被传递给 PHP 脚本,导致返回垃圾值

    在 countdown.php 中将 $_GET 更改为 $_POST

    $end_month = $_POST['month'];
    $end_day = $_POST['day'];
    $end_hours = $_POST['hour'];
    

    您还需要用method='POST' 将下拉列表包装在form 中。此外,您需要将下拉值作为 json 传递给 $.post 函数。请参考jquery post 函数以了解它是如何完成的。

    当您发布时,必须像这样传递值:

    $.post("countdown.php", { 
       month: document.forms[0].month.value, 
       day: document.forms[0].day.value } )
    

    此外,在您的表单中没有小时下拉菜单,但您正试图在 countdown.php 中获取它(这将给出垃圾值)。

    那里有很多事情要纠正,但我认为你应该能够到达那里。

    【讨论】:

    • 是的,我试过了,但我知道我的倒计时不起作用,而且我得到了未定义的月份日期和小时索引..
    • @IvoKuzmanic:不确定您的设置是否有任何不同。当我运行您的代码时,我得到“剩余天数: -15819 0-379648:20:57”作为结果。
    • @IvoKuzmanic:知道了...您所要做的就是将您的$.get 方法更改为$.post。 :) 更新答案..
    • 我运行了它,我得到了一个正确的倒计时时钟,上面写着Days Left: 195 14:48:53,时钟倒计时正确。除了将 $.get 转换为 $.post 之外,我没有更改您发布的代码中的任何内容
    • 试了好几次都没反应,我什至清理了浏览器的缓存...
    猜你喜欢
    • 2021-08-12
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多