I'm not sure if this question is more suited for a javascript forum, but here it goes anyway. I am creating a webpage that I want to access a php page asynchronously. I decided to use ajax, but javascript won't recognize the responseText my php page is returning.
so, in order, here is the javascript to load the page:
function thisFunc(someVal)
{
if(someVal=="default")return;
var button = '<img src="someloadingimage"></img>';
document.getElementById("place").innerHTML=button;
var ajax;
if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}
else{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && ajax.status==200){
if(ajax.responseText=="success")
document.getElementById("place").innerHTML='<input type="button" id="someAId" value="Save" disabled="disabled" />';
else if(ajax.responseText == "Permission Denied." || ajax.responseText == "wrong")
document.getElementById("place").innerHTML='<font style="value:somepersonalfont">Error!</font><input type="button" id="someId" value="Save" onclick="thisFunc(this.parentNode.previousSibling.value)" />';
else
document.getElementById("place").innerHTML=ajax.responseText;//for error checking purposes; once I figured out what my problem was, the else if would have been an the else. The problem is that when it returns, it always does the else.
}
}
ajax.open("POST","thepagetobeloaded",true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("picturevar="+someVal);
}
the js above then loads this page:
<?php
//PHP code starts here
session_start();
if(!isset($_SESSION['mysession']) || !isset($_SESSION['mysession']['sessionison']) || $_SESSION['mysession']['sessionison']!=true || !isset($_SESSION['mysession']['userfolder']) || !isset($_POST['picturevar'])){
exit('Permission Denied.');
}
else
{
$picturevar = filter_var(htmlentities($_POST['picturevar']), FILTER_SANITIZE_STRING);
if($picturevar=='default')exit('wrong');
$file = file('somelocation'.$_SESSION['mysession']['userfolder'].'/somefilename.xml');
for($i=0;$i<count($file);$i++)
if(strpos($file[$i],'somenode'))
break;
$nodeplace=$i;
$string = "";
if($nodeplace==count($file)){
for($i=0;$i<count($file);$i++)
if(strpos($file[$i],'lastnode'))
break;
$lastnode=$i;
$start=$lastnode-1;
$text = "some_tabs_and_opennode".$picturevar."closenode_and_newline";
array_splice($file,$start,0,$text);
$file2 = fopen('somelocation'.$_SESSION['mysession']['userfolder'].'/somefilename.xml','w');
for($i=0;$i<count($file);$i++)
$string .= $file[$i];
fwrite($file2,$string);
exit('success');
}
else{
$text = "some_tabs_and_opennode".$picturevar."closenode_and_newline";
array_splice($file,$lastnode,1,$text);
$file2 = fopen('somelocation'.$_SESSION['mysession']['userfolder'].'/somefilename.xml','w');
for($i=0;$i<count($file);$i++)
$string .= $file[$i];
fwrite($file2,$string);
exit('success');
}
}
//PHP code ends here
?>
after this page is loaded and returns either "success", "Permission Denied.", or "wrong", the javascript should interpret the responseText as it is, however it usually displays "success" where it should put the input button back. That's right, the else (in my javascript onreadysuccesschange function) always happens and displays "success". Any ideas?