这是我的java脚本文件:
$('#addSchoolForm').trigger("reset");
//$(document).ready(function() {
$(function() {
$("#dialog").dialog({
autoOpen: false,
maxWidth:600,
maxHeight: 350,
width: 500,
height: 300,
});
$("#addSchool").on("click", function() {
$("#dialog").dialog("open");
});
$("#addSchoolForm").submit(function(e) {
e.preventDefault();
$("#dialog").dialog("close")
var postData = jQuery(this).serialize();
$.ajax({
type: "POST",
url: "AddSchools.php",
data: postData,
success: function(data){
alert(data); }
});
});
$("#editSchool").submit(function(e) {
e.preventDefault();
var editData = jQuery(this).serialize();
$.ajax({
type: "POST",
url: "GetSchoolID.php",
data: editData,
dataType: 'json',
success: function(data){
var schoolID = $.parseJSON(data);
alert("success");
alert(schoolID.name);
//alert(data["json"]);
//alert(data);
//document.addSchoolForm[sname].value = data[0].name;
//document.addSchoolForm[abbrev].value = data[abbrev];
//document.addSchoolForm[abbrev].value = data[0].abbrev;
}
alert(schoolID.name);
});
//$("#dialog").dialog("open");
});
})
这是我的 get schoolID php 文件
<?php
$school_id = $_POST['school_id'];
$db = mysqli_connect("localhost", "root", "imagroup123","mytrack");
if(!$db){
exit("Error in database connection");
echo("couldn't connect to database");
}
else{
$q = "SELECT * FROM `School` WHERE `SchoolID`='$school_id'";
$schoolresults = mysqli_query($db,$q);
$row = mysqli_fetch_assoc($schoolresults);
$school["name"] = $row['SchoolLong'];
$school["abbrev"] = $row['SchoolShort'];
echo json_encode($school);
}
?>
当我刚刚使用 jsonlint.com 测试 php 文件时,我得到了一个正确的 json 对象,但它没有通过 javascript 文件进行。我对此很陌生,所以我很讨厌这个问题。我还想将数据添加到表单值,然后打开对话框表单。