I am getting the internal server error 500 on my ajax call. I am using a LAMP stack as my server and I've checked the permissions so that can't be my error.
Here's my javascript code:
//$('#addAthleteForm').trigger("reset");
//$(document).ready(function() {
$(function () {
$("#dialog").dialog({
autoOpen: false,
maxWidth: 600,
maxHeight: 500,
width: 500,
height: 460,
close: function () {
$('#addAthleteForm').trigger("reset");
}
});
$("#addAthlete").on("click", function () {
$("#dialog").dialog("open");
});
$("#addAthleteForm").submit(function (e) {
e.preventDefault();
var postData = jQuery(this).serialize();
$("#dialog").dialog("close")
$.ajax({
type: "POST",
url: "AddAthletes.php",
dataType: 'json',
data: postData,
success: function (data) {
alert(data);
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
$("#editAthlete").submit(function (e) {
e.preventDefault();
var editData = jQuery(this).serialize();
$.ajax({
type: "POST",
url: "GetAthlete.php",
dataType: 'json',
data: editData,
success: function (data) {
var form = document.forms['addAthleteForm'];
form.fname.value = data.fname;
form.lname.value = data.lname;
form.school.value = data.school;
form.agegrp.value = data.agegrp;
}
});
$("#dialog").dialog("open");
});
})
and here's my php code:
<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$school = $_POST['school'];
$agegrp = $_POST['agegrp'];
$db = mysqli_connect("localhost", "root", "passwrd","site");
if(!$db){
exit("Error in database connection");
}
else{
$result = mysqli_query($db, "SELECT * FROM `School` WHERE `SchoolLong`='$school'");
$row = mysqli_fetch_array($result));
$SchoolID = $row['SchoolID'];
$result = mysqli_query($db,"SELECT * FROM `AgeGroup` WHERE `AgeGroupLong`='$agegrp'");
$row = mysqli_fetch_array($result));
$AgeGroupID = $row['AgeGroupID'];
mysqli_query($db, "INSERT INTO `Athlete` (`NameFirst`,`NameLast`, `SchoolID`, `AgeGroupID`) VALUES ('$fname', '$lname', $schoolID, $agegrpID)");
}
echo json_encode($fname);
?>
I'm not to sure where this error is coming from. The code used to work.
Syntax error in below line:-
$row = mysqli_fetch_array($result));
It should be :-
$row = mysqli_fetch_array($result);
same mistake for below mysqli_fetch_array()
Your corrected block of code are:-
$result = mysqli_query($db, "SELECT * FROM `School` WHERE `SchoolLong`='$school'");
$row = mysqli_fetch_array($result);
$SchoolID = $row['SchoolID'];
$result = mysqli_query($db,"SELECT * FROM `AgeGroup` WHERE `AgeGroupLong`='$agegrp'");
$row = mysqli_fetch_array($result);
$AgeGroupID = $row['AgeGroupID'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.