簡體   English   中英

使用XMLHttpRequest向數據庫添加記錄

[英]Add a record to the database with XMLHttpRequest

我有以下表格:

            <form id="form" name="form">
            <img id="close" src="images/3.png" onclick ="div_hide()">
            <h2>Grade</h2>
            <hr>
            <input id="fn" name="fn" placeholder="Faculty number" type="number">
            <select id="grade_type" name="grade_type">
                <option value="test" selected="selected">Тест</option>
                <option value="attendance">Attendance</option>
                <option value="homework">Homework</option>
            </select>
            <input id="grade" name="grade" placeholder="Points" type="number">
            <a href="javascript:%20check_empty()" id="submit">Add record</a>
            </form>

當我單擊提交按鈕時,我想將分數和grade_type添加到數據庫中。 因此,我正在使用JavaScript和PHP:

// Validating Empty Field
function check_empty() {
if (document.getElementById('grade').value == "") {
alert("Fill the fields!");
} else {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        alert("xmlhttpreq");
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var grade = String(document.getElementById('grade').value);
    var grade_type = document.getElementById('grade_type');
    var grade_type_value = String(grade_type.options[grade_type.selectedIndex].value);
    var fn = String(document.getElementById('fn').value);
    xmlhttp.open("GET","getuser.php?grade="+grade+"grade_type="+grade_type_value+"fn="+fn,true);
    xmlhttp.send();
    document.getElementById('form').submit();
}
}

getuser.php文件的內容為:

<?php
    require "config.php";

    $fn =  $_GET["fn"];
    $grade = $_GET["grade"];
    $type = $_GET["grade_type"];

    echo "<script type='text/javascript'>alert('$fn');</script>";

    try {
        $conn = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
    }
    catch(PDOException $e) {
        die("Database connection could not be established.");
    }


    $sql = $conn->prepare("SELECT * FROM students WHERE fn = ?");
    $sql->execute(array($fn));
    if($sql->rowCount() > 0) {
        $statement = $conn->prepare("INSERT INTO points (student_fn, type, grade, datetime)
                                    VALUES (?, ?, ?, CURRENT_TIMESTAMP)");
        $statement->execute(array($fn, $type, $grade));
    }
    else {
        echo "<script type='text/javascript'>alert('No such fn');</script>";
    }
    $conn = null;
?>

但是,我認為它從未執行過,因為我從未看到過警報的結果。 我以前從未使用過XMLHttpRequest,所以我什至不知道我的代碼是否有效。 我將不勝感激任何幫助。

您可以使用jquery做到這一點。

$('#submit').click(function(){
    $.ajax({
        url: 'getuser.php',
        type: 'GET',
        data: $('#form1').serialize(),
        success: function(result){
             alert("Your data has been uploaded");
           }

     });         

});

確保您需要像這樣將jquery文件添加到您的網站

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM