【问题标题】:jQuery append result from php not showing来自php的jQuery追加结果未显示
【发布时间】:2012-09-28 20:58:44
【问题描述】:

嗨,我认为我的 jQuery 做错了什么。我正在开发一个在线预订系统,在选择治疗和日期后,我想动态显示可用时间段列表。我有一个 php 文件,它创建一个数组并在 SELECT OPTION 列表中回显结果。我已经用另一个文件进行了测试,并且 jQuery post 函数可以正常工作,所以问题肯定是要显示结果。以下是我的第一页jQuery。在更改日期时,SELECT 列表会消失。

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

            var $form = $( "#input" ),
            treat = $form.find( 'input[name="treatment"]' ).val(),
            book = $form.find( 'input[name="bookdate"]' ).val(),
            url = "input_date.php";

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

        );
    }});
});

这是我的 php 文件:

<?php
include('connection.php');
error_reporting(0);
$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;
}

?>

【问题讨论】:

  • @WebnetMobile.com 请在 SO 上插入英文链接:en.wikipedia.org/wiki/SQL_injection
  • 感谢您向我指出安全漏洞,但这对我的问题有何帮助?
  • @feeela 哎呀...我没有注意到我切换了语言。对不起。这里是SQL Injection in English
  • alert(content); 看看你是否在那个变量中得到了结果
  • 也许你应该将 /echo "';/ ?

标签: php jquery append


【解决方案1】:
var content = $( data ).find( '#timeslots' );
                    $( "#dates" ).empty().append( content );    
                }

这是错误

$( data ).find( '#timeslots' );

返回一个object

你可以使用

$( data ).find( '#timeslots' ).text();// or html()

【讨论】:

  • 我都这样尝试过:function(data) { var content = $( data ).find( '#timeslots' ).html();警报(console.log(数据)); $( "#dates" ).empty().append( content );但它仍然返回未定义,也带有文本而不是 html
  • @JakeRowsell 使用$("#dates").html(content); 而不是$( "#dates" ).empty().append( content );
  • 它仍然提醒 undefined.. 我认为问题在于找到#timeslots 内容..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 2016-02-16
  • 2018-03-19
相关资源
最近更新 更多