我的ajax帖子有什么问题

时间:2017-08-28 07:20:25

标签: ajax

我想要做的是发布(note.php中发送者ID的下拉菜单的值)并将其传递给名为(getuser)的PHP文件。在PHP getuser中,它将使用mysqli查询中的speaker下拉值并将其显示到表中。然后表格将显示在(在custom.php中具有结果id的div)。

当我按f12时,这是我得到的错误 enter image description here

这是我的PHP代码,下拉扬声器:

<!DOCTYPE html>

<?php
    error_reporting(E_ALL & ~E_NOTICE);
    error_reporting(E_ERROR | E_PARSE);
    session_start();
    date_default_timezone_set('Asia/Manila');

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "srdatabase";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $selectspeakers = "SELECT * FROM speakers";
    $sp_result = mysqli_query($conn, $selectspeakers);
?>

<html>
<head>
<title>Reservation</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>

    <form id="reservationform" name="reservationform" method="POST">

    <select id="speaker" name="speaker" class="dropdown" style='text-transform:capitalize;'>
        <?php while($array = mysqli_fetch_array($sp_result)):;?>
        <option value = "<?php echo $array['speaker_fullname'];?>" <?php if($_SESSION["selectedSpeaker"] == $array['speaker_fullname']) echo "selected";?> ><?php echo $array['speaker_fullname'];?></option>
        <?php endwhile;?>
    </select>
    </form>

<div id="footer">Copyright 2017</div>

<script>
$(document).ready(function(){
    $('#speaker').change(function(){
        var txt = $(this).val();
        if(txt != '')
        {
            $.ajax({
                url:"getuser.php",
                method: "post",
                data:{search:txt},
                dataType:"text"
                success:function(data)
                {
                    $('#result').html(data);
                }
            });
        }
        else
        {
            $('#result').html('');
        }
    });
});
</script>


</body>
</html>

这是使用发布数据并将其显示到表

的php
<!DOCTYPE html>
<html>
<head>

<style>
table {
    width: 100%;
    border-collapse: collapse;
}

td{
    border: 1px solid ddd;
    padding: 10px;
}

th 
{
    text-align: left;
    height:20px;
    background-color: #4CAF50;
    color: white;
    padding:10px;
    border: 1px solid #ddd;
}
</style>
</head>
<body>

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "srdatabase";

$con = new mysqli($servername, $username, $password, $dbname);

$sql="SELECT * FROM reservations WHERE speaker = '".$_POST["search"]."' AND reservationstatus = 'approved' ORDER BY date DESC";
$result = mysqli_query($con,$sql);
echo "<h3>Speaker Schedule</h3>";
echo "<table class='reservations-table'>
<tr>
<th>Speaker</th>
<th>Reservation Date</th>
<th>Reservation Time</th>
<th>Location</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['speaker'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
    echo "<td>" . $row['time'] . "</td>";
    echo "<td>" . $row['location'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这是因为你在ajax success

之前错过了一个逗号
$.ajax({
    url:"getuser.php",
    method: "post",
    data:{search:txt},
    dataType:"text",    //comma
    success:function(data)
    {
        $('#result').html(data);
    }
});