0

嗨,我在显示从我的 php 文件返回的结果时遇到问题。

jQuery部分:

$("#dates").load('input_date.php');
    $("#datepicker").datepicker({
        dateFormat: "dd-mm-yy",
        onClose: function() { 

            var $form = $( "#input" ),
            treat = $('#treatment option:selected').val(),
            book = $( '#datepicker' ).val(),
            url = "input_date.php" ;

            $.post( url , { treatment: treat, bookdate: book  },
                function(data) {
                    var content = $( data ).find( '#timeslots' );
                    alert(treat);
                    console.log(content);
                    $( "#dates" ).empty().append( content );}


            /*$.post( "input_date.php" , { treatment: treat, bookdate: book  },
                function(data) {
                    var $data = $(data);
                    var content = $data.is('#timeslots') ?   $data : $data.find('#timeslots');
                    alert(content);
                    $( "#dates" ).empty().append( content );}*/
        );



    }});

php 部分(在 input_date.php 中)

<?php
include('connection.php');
error_reporting(E_ALL);
$treatment = $_POST['treatment'];
$bookdate = $_POST['bookdate'];
if(isset($treatment) && isset($bookdate)){

$exp = explode("-", $bookdate);

//determine what day of the week it is
$timestamp = mktime(0,0,0,$exp[1],$exp[0],$exp[2]);
$dw = date( "w", $timestamp); // sun0,mon1,tue2,wed3,thur4,fri5,sat6
echo $dw."weekday"; //week day 
echo"<br/>";

//find bookings with same date
$q = mysql_query("SELECT BOOK_SLOT_ID FROM BOOKINGS WHERE BOOK_DATE='$bookdate'");
//make array of booking slots
$array1 = array();
while ($s = mysql_fetch_array($q)) {
$array1[] = $s['BOOK_SLOT_ID'];
}
$q2 = mysql_query("SELECT SL_ID FROM SLOTS");
//make array of all slots
$array2 = array();
while ($s2 = mysql_fetch_array($q2)) {
$array2[] =  $s2['SL_ID'];
}

//remove bookings from all slots
$arr_res = array_diff($array2, $array1);

//make selectable options of results
echo '<SELECT id="timeslots">';
foreach($arr_res as $op){
$r = mysql_query("SELECT SL_TIME FROM SLOTS WHERE SL_ID='$op'");
$q3 = mysql_fetch_array($r);
echo "<OPTION value=".$op.">".$q3['SL_TIME']."</OPTION>";
}
echo '</SELECT>';
}else{
$else = mysql_query("SELECT * FROM SLOTS");
echo '<SELECT>';
while($array_else = mysql_fetch_array($else)){
echo "<OPTION value=".$array_else['SL_ID'].">".$array_else['SL_TIME']."</OPTION>";
}
echo "</SELECT>";
print $bookdate;
}

?>

大更新:我在 $.post 部分发现了一个错误,导致变量无法正确传递给 php。现在,如果我检查我的控制台,最初它会给出 php 未定义的错误,但是我从 post 获得的返回显示了它应该显示的内容,它不会显示在我的 #dates div 中。关于如何让信息显示的任何想法?提前致谢

4

1 回答 1

2

jQuery.find()只搜索给定元素的后代,而不是元素本身。由于#timeslots是脚本输出的顶级元素,因此找不到它。你可以写:

var $data = $(data);
var contents = $data.is('#timeslots') ? $data : $data.find('#timeslots');
于 2012-10-08T21:00:12.187 回答