8 Replies - 39251 Views - Last Post: 22 May 2014 - 12:14 PM

#1 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 07:24 AM

I have a popup(div) that needs to be populated with different content for different occasions, I am using jQuery's $.ajax function to populate the div.

The PHP file that the ajax calls will need to read from a database in order to populate the div with the correct content.

The PHP file does have one include at the top of the file which I am guessing is the problem, however this is necessary for it to connect to the database.

I have tried with multiple file system directories and web server directories for the incldue with no luck.

/var/www/vhosts/

and

C://

and

../ relative paths

The error I get is 500 (Internal Server Error)

I am guessing that it is having trouble finding the directory of the include as I know for sure I am directing correctly in the jQuery file.

It must be a problem with the include?

Is this a common problem with calling PHP file with ajax that contains includes?

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4254
  • View blog
  • Posts: 13,485
  • Joined: 08-June 10

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 08:47 AM

Quote

It must be a problem with the include?

not necessarily.


Quote

Is this a common problem with calling PHP file with ajax that contains includes?

usually not.


Quote

The error I get is 500 (Internal Server Error)

might be something in your webserver setup.


Quote

I am guessing that it is having trouble finding the directory of the include …

a missing include triggers a PHP fatal error, but that usually appends some text to the output and doesn’t change the HTTP status.


Quote

… as I know for sure I am directing correctly in the jQuery file.

given that you tried the include with totally incompatible examples, I’m not so sure.


if you post some code, we are able to say more about the issues.

From the point of PHP it doesn’t matter if you used a regular request, a form submit or an AJAX call. it all uses HTTP (which is stateless) and all are routed through the webserver.
Was This Post Helpful? 1
  • +
  • -

#3 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 10:04 AM

thank you Dormilich his has helped me understand the problem better, I have set up a little tester purely for this issue and here is what I found:

first of all let me show you the code I have in front of me, very simple version of what I am trying to do but concept is the same

ajax.js

$(document).on("click", "#container", function(e){
	//$(this).load('inlcudes/populate.php');

	$.ajax({
		url: "inlcudes/populate.php",
		dataType : "html",
		success: function ( html ) {
			$("#container").html(html)
		}
	});
});




populate.php

<?php
include('included_file.php');

?>

<p>This is the rest of the document</p>



include_file.php

<p>I have been included, yey</p>



index.php
<html>
<head>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script src="ajax.js"></script>
</head>
<body>

	<div id="container" style="width: 100px; height: 100px; background-color: #ff0000;">
	
	</div>

</body>
</html>



When I click it runs the jQuery and returns
<p>This is the rest of the document</p>
with no errors this time, but obliviously is not including the other PHP file. :notify:/>

This post has been edited by typhlosion: 22 May 2014 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 7,627
  • Joined: 15-January 14

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 10:21 AM

Add this to the top of your PHP file, before you include the file:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Was This Post Helpful? 0
  • +
  • -

#5 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 10:47 AM

seems to work now I think google chrome caches old page or something ^-^/>/>

same code as in my last post.

http://tech-bods.co....ajaxphpinclude/

just click the red box

and the file structure under that directory is:

includes/included_file.php
includes/populate.php
ajax.js
index.php

However I will be trying this later but, if I have a class in "included_file.php" would it be useable in "populate.php"

This post has been edited by typhlosion: 22 May 2014 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 7,627
  • Joined: 15-January 14

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 11:17 AM

Including a file is similar to copying and pasting code from the included file into the source file. If you have a class definition in an included file it would work the same as if you copied and pasted that code into where you have the include statement.
Was This Post Helpful? 0
  • +
  • -

#7 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 11:22 AM

well I need to now go back to the actually site I am working on and find the problem with this new knowledge =p
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 7,627
  • Joined: 15-January 14

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 11:53 AM

Make sure to enable error reporting. When I'm debugging things with ajax I prefer to have PHP use an error log that I can check for error messages.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3098
  • View blog
  • Posts: 11,844
  • Joined: 03-December 12

Re: Calling a PHP file with jQuery ajax gives a 500 Internal Server Error

Posted 22 May 2014 - 12:14 PM

I doubt the code posted would work, unless you actually did this on purpose, but

Quote

02
    //$(this).load('inlcudes/populate.php');
03
 
04
    $.ajax({
05
        url: "inlcudes/populate.php",


Was the file name intentional?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1