Jump to content

HTML/JS page wont call PHP script or PHP isn't being run.


hibernated
 Share

Recommended Posts

Hi everyone. I'm new here and new to making a website too so this might be a really dumb request, I've searched though and done the tutorials and I don't know what is wrong so.. here I am. The problem is that I've made a HTML and JavaScript page which calls a PHP script with an XMLHttpRequest Object and an Open/GET command (I hope this is the right terminology, please do correct me if it isn't) and as far as I can tell all the code is hunky dory but when I press the button to get the PHP script to work it doesn't seem like anything happens...I got fed up trying to figure out what was wrong with the script and wondered if I'd done something fundamentally wrong so I uploaded the examples from this page:http://www.w3schools.com/php/php_ajax_php.aspand that didn't work either.I used the same filenames it said on the tutorial with the right file extensions and I had them both in the same directory but no dice, I type in the box but nothing happens. :sAnyway, I took those back off the server and put my one back on and I found if I just went straight to my PHP script by typing the url used in the javascript call (including the bit with the question mark, like script.php?passedParameter=10 or something similar) straight into the browser, the browser showed the text that should be sent to the html page. But if I try and get at it from the HTML page, nothing. :)Oh, and the server I'm using is a free page on www.zymic.com which says it supports PHP and even has a bunch of PHP tutorials on there as well (though none of them helped with my problem).I think I must've missed something fundamental to how it all works but I just don't know what it is or where to find it, so I'd be really thankful for any suggestions.Cheers. :)Wannabecoder.

Edited by wannabecoder
Link to comment
Share on other sites

Hi, thanks for replying. :)Well I'm not sure it is the code, it might be that I'm doing something wrong when I'm hosting it I'm not sure...Like I said I also tried the code on this page:http://www.w3schools.com/php/php_ajax_php.aspand that didn't work either. But that code is straight from the tutorial so I guess it should work fine?It seems like the server executes the PHP if I go direct to the PHP page, but if I open the HTML page that tries to get the PHP nothing happens. Is there some server prerequisite for using XMLHttpRequest or something? :sBut yeah, to look at code look at the code on that page cos I know that wont have any other problems. One problem at a time and all that. :)Cheers.

Link to comment
Share on other sites

Hi, thanks for replying. :)Well I'm not sure it is the code, it might be that I'm doing something wrong when I'm hosting it I'm not sure...Like I said I also tried the code on this page:http://www.w3schools.com/php/php_ajax_php.aspand that didn't work either. But that code is straight from the tutorial so I guess it should work fine?It seems like the server executes the PHP if I go direct to the PHP page, but if I open the HTML page that tries to get the PHP nothing happens. Is there some server prerequisite for using XMLHttpRequest or something? :sBut yeah, to look at code look at the code on that page cos I know that wont have any other problems. One problem at a time and all that. xDCheers.
no problem :Pi'm not into AJAX yet myself, so i wouldn't be able to comment, but it usually helps if the admin/mods can see the basic structure of how your code looks like.it does sound like it's an issue with your host though, or a less forgiving environment.i just had an example recently that worked on localhost, but failed onsite.(it was something so basic like calling a variable before it was even declared !! :) )
Link to comment
Share on other sites

as has been mentioned, please post the exact code you have on your computer right now. typo's occur even when people copy/paste their own code here, let alone a user copying code from a tutorial, and then making changes. (let alone any slips ups from copying their own code back onto here).so, show us what you have, clearly explain what you are trying to accomplish, and what is happening instead. make sure you are checking for errors, and define the location of all files in use. tell us if the problem is occurring locally, and/or on a host.

Edited by thescientist
Link to comment
Share on other sites

Ok, here is a massively trimmed down version of my code. I tested this also and still the same result.book/bookReader.html

<head><script type="text/javascript">function getPage(roomRequest)	{document.getElementById("pageTextArea").innerHTML="Failed...";	var xmlhttp;	if (window.XMLHttpRequest)		  {// Chrome, FF, new IE, Opera, Safari		  xmlhttp=new XMLHttpRequest();		  }	else		  {// old IE (5/6)		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");		  }	xmlhttp.onreadystatechange=function()		{		if (xmlhttp.status==404)			{			document.getElementById("pageTextArea").innerHTML="404 connection error.";			}		else if (xmlhttp.readyState==0 && xmlhttp.status==200)			{			document.getElementById("pageTextArea").innerHTML="Thinking...";			}		else if (xmlhttp.readyState==1 && xmlhttp.status==200)			{			document.getElementById("pageTextArea").innerHTML="Requesting new page...";		 	}		else if (xmlhttp.readyState==2 && xmlhttp.status==200)			{			document.getElementById("pageTextArea").innerHTML="Request recieved!";			}		else if (xmlhttp.readyState==3 && xmlhttp.status==200)			{			document.getElementById("pageTextArea").innerHTML="Processing request...";			}		else if (xmlhttp.readyState==4 && xmlhttp.status==200)			{			document.getElementById("pageTextArea").innerHTML=xmlhttp.responseText;			}		}	xmlhttp.open("GET","bookText.php?nextRoom="+roomRequest,true);	xmlhttp.send();	document.body.style.cursor='default';	}</script></head><html><body><div id="mainarea" style="position:absolute;left:90;top:30;">	<div id="pageTextArea">		<p class="option">Some Text<span class="PageRef" onclick="document.body.style.cursor='wait';getPage(1)" onmouseover="document.body.style.cursor='pointer'" onmouseout="document.body.style.cursor='default'"> One </span></p>	</div></div></body></html>

book/bookText.php

<html><body><?php//array of rooms/pages$room[0] = "Contents Page";$room[1] = "Page 1";$room[2] = "Page 2";$room[3] = "Etc...";$nextRoom = $_GET["nextRoom"];$outText = $room[$nextRoom];echo $outText;?></body></html>

But I also tried the exact code in that tutorial I mentioned and that didn't work either... Seriously, copied and pasted straight from the webpage... :SCheers for the replies, I appreciate the help.

Link to comment
Share on other sites

Your PHP page shouldn't output things like html and body tags. This is an HTML fragment that you're inserting into another document, don't put an entire document into another one, only fragments.Other than that, the basics: check for Javascript errors; use a debugger like Firebug where you can click on the Net tab and verify whether or not the request is even going out, and if so what the response was.If you want someone to look at your site, post a link to the live page.

Link to comment
Share on other sites

Oh...Oh dear...It was the html tags...I find it difficult (though not impossible) to believe I actually did that... and then couldn't figure it out either. :) Thank you so much for solving that for me... I think I spent so much time looking at everything else that I wasn't even registering them as being there.Thoroughly embarrassed but also very, very grateful. Thank you justsomeguy, and everyone else for your help too.I'm gonna have a look to see if I can close the topic now. If not then, moderators, please close. :)I'm such a numpty...

Link to comment
Share on other sites

##CUTE##I'm such a numpty...
not a numpty, a newbie ! :)these things will occur less & less as we get more experience.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...