Jquery没有从php文件中获取我的值

时间:2013-04-23 15:16:27

标签: php jquery html get

我需要你的帮助。在我的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。

2 个答案:

答案 0 :(得分:0)

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

<强>更新

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

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

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

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

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

您还需要使用form将下拉包装在method='POST'中。此外,您需要将下拉值作为json传递给$.post函数。请参阅jquery post功能,了解具体方法。

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

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

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

所以要纠正那些事情,但我认为你应该能够到达那里。

答案 1 :(得分:0)

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

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

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

echo $json_result;