使用ajax将表单数据发送到php,将数据插入MySQL数据库

时间:2019-07-09 11:19:13

标签: javascript php jquery ajax

我正在使用ajax向php发送数据,然后将其插入数据库。我收到一个未声明变量的错误。请帮忙。

通知:未定义索引:第4行E:\ xampp \ htdocs \ user-accounts \ messaging.php中的receiver_id1 注意:第5行的E:\ xampp \ htdocs \ user-accounts \ messaging.php中未定义的索引:receiver_id2 注意:第6行的E:\ xampp \ htdocs \ user-accounts \ messaging.php中的未定义索引:receiver_id3 注意:第7行的E:\ xampp \ htdocs \ user-accounts \ messaging.php中的未定义索引:receiver_name1 注意:第8行的E:\ xampp \ htdocs \ user-accounts \ messaging.php中的未定义索引:receiver_name2 注意:第9行的E:\ xampp \ htdocs \ user-accounts \ messaging.php中的未定义索引:receiver_name3 注意:未定义变量:第10行上的xampp \ htdocs \ user-accounts \ messaging.php中的用户 注意:未定义的索引:第11行xampp \ htdocs \ user-accounts \ messaging.php中的from_user_name 致命错误:未捕获的PDOException:SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与xampp \ htdocs \ user-accounts \ messaging.php中的令牌数量不匹配:29堆栈跟踪:#0 \ xampp \ htdocs \ user -accounts \ messaging.php(29):PDOStatement-> execute()#1 {main}在第29行的xampp \ htdocs \ user-accounts \ messaging.php中抛出

我有一个发送消息的表格。我有一个.php文件来插入数据。但是看来Ajax代码无法正常工作。

$output = '
<div class="form-body">
                <div class="form-group">
                    <div class="col-md-6">
                        <input value="" class="form-control" id="sender_id" name="sender_id" readonly="True" type="hidden">
                    </div>
                </div>
<div class="form-group flex">
                    <label class="control-label col-md-3" style="margin-top: 23px;">Receipient:</label>
<div class="col-sd-10">
                      <label class="" for="textinput" style="font-size: 11px;">Teachers</label><select class="form-control" id="receiver_id1" required="required" onchange="disableMenu1();">
                        <option value="0" selected="selected">Select Recipient</option>
                        <option value="10">All Teachers</option>';
                        foreach($result1 as $row)
                        {$output .= '<option value="'.$row['teacher_no'].'">'.$row['teacher_name'].'</option>';
                        }
                        $output .= '</select>
                        <p id="demo" class="text-danger"></p>
                    </div>
<script>

$(document).ready(function(){
    $('#sending').on('submit', function(event){
        event.preventDefault();
        var receiver_id1 = $('#receiver_id1 :selected').val();
        var receiver_id2 = $('#receiver_id2 :selected').val();
        var receiver_id3 = $('#receiver_id3 :selected').val();
        var receiver_name1 = $('#receiver_id1 :selected').text();
        var receiver_name2 = $('#receiver_id2 :selected').text();
        var receiver_name3 = $('#receiver_id3 :selected').text();
        var subject = $('#subject').val();
        var message = $('#message').val();
        $.ajax({
                url:"messaging.php",
                method:"POST",
                data:{receiver_id1:receiver_id1, receiver_id2:receiver_id2, 
                      receiver_id3:receiver_id3, receiver_name1:receiver_name1, 
                      receiver_name2:receiver_name2, receiver_name3:receiver_name3,
                      subject:subject, message:message},
                success:function(data) {
                    $('#receiver_id1').val("0");
                    $('#receiver_id2').val("0");
                    $('#receiver_id3').val("0");
                    $('#subject').val("");
                    $('#message').val("");
                }
            });

        }
    });
});

这是php代码

<?php
include('config.php');
include 'userSignup.php';
$receiver_id1       =    $_POST['receiver_id1'];
$receiver_id2       =    $_POST['receiver_id2'];
$receiver_id3       =    $_POST['receiver_id3'];
$receiver_name1     =    $_POST['receiver_name1'];
$receiver_name2     =    $_POST['receiver_name2'];
$receiver_name3     =    $_POST['receiver_name3'];
$from_user_id       =    $user;
$from_user_name     =    $_POST['from_user_name'];
$subject            =    $_POST['subject'];
$message            =    $_POST['message'];

$stmt = $connect->prepare("
INSERT INTO message 
(to_user_id, from_user_id, sender, reci, message_id, subject, message) 
VALUES (:receiver_id1, :from_user_id, :from_user_name, :receiver_name1, :from_user_id, :subject, :message)
");

$stmt->bindparam(':receiver_id1'  ,    $receiver_id1);
$stmt->bindparam(':receiver_name1',    $receiver_name1);
$stmt->bindparam(':receiver_name2',    $receiver_name2);
$stmt->bindparam(':receiver_name3',    $receiver_name3);
$stmt->bindparam(':from_user_id'  ,    $from_user_id);
$stmt->bindparam(':from_user_name',    $from_user_name);
$stmt->bindparam(':subject'       ,    $subject);
$stmt->bindparam(':message'       ,    $message);
$stmt->execute()

?>

代码应将数据发送到messagement.php文件。我刚刚从前端文件中添加了部分代码。我将非常感谢您的帮助。谢谢。

0 个答案:

没有答案