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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 宇视监控服务器无法登录
    • ¥15 PADS Logic 原理图
    • ¥15 PADS Logic 图标
    • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
    • ¥20 气象站点数据求取中~
    • ¥15 如何获取APP内弹出的网址链接
    • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
    • ¥50 STM32单片机传感器读取错误
    • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
    • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)