0

我正在创建用于使用 PHP/jQuery 和 AJAX 发送和接收消息的测试应用程序。当我注释掉这部分代码时,我的代码可以正常工作:

$("#sendUser").submit(function(){
        return false;
    });

如果我不注释这部分,那么 jQuery 就不会从<select>标签中接收值。有人可以告诉我如何解决这个问题。这是我的完整代码:

<form action="send_toUser.php" method="post" id="sendUser">
        <label>Title: </label>
        <input type="text" name="title" id="title"><br>
        <label>To: </label>
        <select name="user" id="user">
            <?php $getData = $mysqli->query("select * from login");
                while($row = $getData->fetch_assoc()):
            ?>
                <option><?php echo $row['username']; ?></option>
            <?php endwhile; ?>
        </select><br>
        <label>Message:</label><br>
        <textarea name="content" id="content"></textarea><br>
        <input type="submit" value="Send" id="sendToUserButton">
        <label id="outputLabel" hidden></label>
    </form>

send_toUser.php

<?php 
    session_start();
    include 'functions.php';
    protect();

    $title = $_POST['title'];
    $content = $_POST['content'];
    $user = $_POST['user'];

    $con = new mysqli("localhost", "root", "alen", "loginregister") or die($con->error);
    $queryReceiver = $con->query("select * from login where username = '$user'");
    $idReceiver = $queryReceiver->fetch_assoc();
    $idReceiver = $idReceiver['user_id'];

    $queryUser = $con->query("select * from admin where username='admin'");
    $idUser = $queryUser->fetch_assoc();
    $idUser = $idUser['id_admin'];

    date_default_timezone_set('UTC');
    $date = date('Y-m-d');

    $con->query("insert into message values('', '$idReceiver', '$idUser', '$title', '$content','', '$date')") or die("Error while sending!");
    echo "Message sent";

?>

脚本.js

$("#sendToUserButton").click(function(){
        $.post("send_toUser.php"),
        {"title" : ("#title").val(), "user" : ("#user").val(), "content" : ("#content").val()},
        function(data){
            $("#outputLabel").html(data);
        }
    });
    $("#sendUser").submit(function(){
        return false;
    });
4

0 回答 0