Ram Narayanan Ram Narayanan - 5 months ago 37
PHP Question

500 Internal Server Error due to JQuery Ajax Post

Found the answer:

The error was the semicolon and it worked ! But as "KIKO Software pointed out" due the code that didn't make sense the data was added twice in the database hence I removed the "$("script").html(result);" line in the html file and everything worked like a charm. And this was nothing related to server error 500.... Just a few errors in the code.



I am stuck while trying to perform an AJAX post. For some reason, I'm getting a server 500 error. I can see it hit break points in the controller. I tried alerting and echo from the PHP file, and it doesn't work. I tried looking at other stack overflow queries and it didn't help.

The Ajax call in add_pet.html

<script>
function add_trigger(){
var name=$('#name').text();
var breed=$('#breed').text();
$.ajax({
type: "POST",
url: "add_pet.php",
data: "PET_NAME="+name+"&PET_BREED="+breed,
cache: false,
success: function(result) {
$("script").html(result);
window.location.href='view.html';
}
});
}
$('div[contenteditable]').keydown(function(e) {
if (e.keyCode === 13) {
add_trigger();
return false;
}
});
</script>

<form>
<div id="Heading" style="font-weight:400">Join</div>
<div id='name_label' style="font-weight:100">Name:</div>
<div id='name' contenteditable='true' style="font-weight:100"></div>
<div id='breed_label' style="font-weight:100">Breed:</div>
<div id='breed' contenteditable='true' style="font-weight:100"></div>
<div id='add_trigger' onclick='add_trigger()' style="font-weight:400"><span style="cursor:pointer">Add pet</span></div>
</form>


add_pet.php code to insert the data into the database:

<?php
session_start();
$doctorid=$_SESSION['DOCTOR_ID'];
$PET_NAME=$_POST['PET_NAME'];
$PET_BREED=$_POST['PET_BREED'];

$count_results=execute_MYSQL("SELECT * FROM PETS;");
$PET_ID=$count_results->num_rows;
echo "<script>alert('line=INSERT INTO PETS VALUES ($PET_ID,\"$PET_NAME\",\"$PET_BREED\",$doctorid'));</script>";
execute_MYSQL("INSERT INTO PETS VALUES ($PET_ID,'$PET_NAME','$PET_BREED',$doctorid);");
echo "<script>window.location.href='view.html'</script>"


Screen shot of the error

enter image description here

Answer

The Internal server error is caused due to the error in server side scripting. It hasn't everything to concern with the client side script. So I guess you should check the php code.. You are missing a

';'

on last echo on server side.. and you should also give your full php code I mean the server database connection and the execute_MYSQL function definition. The error may also be in them.

Comments