weixin_33670786 2015-07-26 20:45 采纳率: 0%
浏览 57

如何运行一个ajax按钮?

I'm designing a button to insert data into a database using ajax, but i could not find the source of the problem to why the code is not working. Each button that appears on the screen has value in it, once the user clicks on it, the value will be sent to a database and be stored.

This is my code for the button,

<?php
require 'dbfunction.php';
require 'checkLoginStatus.php';


$con = getDbConnect(); // invoke the getDbConnect() function to get a database connection
$day = date("l");

$adminid = $staff_information["staff_Id"]; // retrieve admin email 

if (!mysqli_connect_errno()) { // connection to database is successful
    $sqlQueryStr = "SELECT BI.timetable_Id, BI.academicInstitution, BI.startTime, BI.endTime, BI.day " .
            "FROM timetableschedule BI " .
            "WHERE BI.staffID ='" . $adminid . "' AND BI.day='" . $day . "' " .
            "ORDER BY BI.timetable_Id";

    $result = mysqli_query($con, $sqlQueryStr);  // execute the SQL query<dl></dl>

    while ($schedule = mysqli_fetch_array($result)) {
        $scheduleRegList[$schedule['timetable_Id']] = $schedule;
    }
    mysqli_close($con);

    if (isset($scheduleRegList)) { // studentList exist
        foreach ($scheduleRegList as $timetable_Id => $key) {
            echo '<input type="button" name="' . $timetable_Id . '" class="scheduleReg" value="';
            echo $key['academicInstitution'] . "<br />";
            echo $key['startTime'] . "-" . $key['endTime'] . "hrs<br />";
            echo '"></input>';
        }
        echo "</select><br /><br/ >";
    } else {
        echo "no record.";
    }
} else {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

<?php 
if (isset($scheduleRegList)) {
?>
<script>
    $(document).ready(function(){
        $(".scheduleReg").click(scheduleReg);
    });
            
    function scheduleReg(){
        var data = new Object();
        data.timetable_Id = this.name;
        $.post("register.php",data, loadModule);
    }
</script> 
<?php
}
?>

And this is my register.php

<!DOCTYPE html>
<?php
session_start();

require 'dbfunction.php';

$timetable_Id = $_POST['timetable_Id']; // get module code from $_POST

$con = getDbConnect(); // invoke the getDbConnect() function to get a database connection

if (!mysqli_connect_errno($con)) { // connection to database is successful
    $sqlQueryStr =
            "INSERT INTO report (staff_Id) " . "VALUES ('$timetable_Id')";

    mysqli_query($con, $sqlQueryStr);    
    mysqli_close($con);
}
?>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?