AJAX+PHP problem

Discussion in 'JavaScript' started by mycomputerguide, Dec 26, 2008.

  1. #1
    Hi folks,

    I have a problem with which im strugling for two days now. I searched on Google but can't find a solution.

    When I call the PHP file directly there are no problems, but when I call the PHP file through AJAX nothing happens because the site I'm trying to open with the PHP file() doesn't seem to be read. When I display the variable $rc in Ajax it displays 0. If anyone can give me a hint why I have this problem, I would be very very thankful. God bless & merry christmas!


    Javascript code:
    
    		function createRequestObject() {
    		    var ro;
    		    var browser = navigator.appName;
    		    if(browser == "Microsoft Internet Explorer"){
    		        ro = new ActiveXObject("Microsoft.XMLHTTP");
    		    }else{
    		        ro = new XMLHttpRequest();
    		    }
    		    return ro;
    		}
    
    		var http = createRequestObject();
    
    		function sndReq(formname) {
    			 var form = document.getElementById(formname);
    			 var submitstring = "";
    			 for(var i=0;i<form.length;i++) {
    				  submitstring = submitstring + form.elements[i].name + "=" + form.elements[i].value + "&";
    			 }
    			 alert(submitstring);
    
    
    		    http.open('get', 'retrieve.php?links='+submitstring);
    		    http.onreadystatechange = handleResponse;
    		    http.send(null);
    		}
    
    		function handleResponse() {
    		    if(http.readyState == 4){
    		        var response = http.responseText;
    		        var update = new Array();
    
    		        if(response.indexOf('|' != -1)) {
    		            update = response.split('|');
    		            document.getElementById(update[0]).innerHTML = update[1];
    		        }
    		    }
    		}
    
    Code (markup):
    HTML code:

    
    <form method="post" id="lb_form" action="">
    	<table cellpadding="0" cellspacing="0" border="0">
    		<tr>
    			<td>
    			<textarea name="links" cols="50" rows="10"></textarea>
    			</td>
    		</tr>
    		<tr>
    			<td><input type="button" value="Retrieve related links" onclick="javascript:sndReq('lb_form');return false;"></td>
    		</tr>
    		<tr>
    			<td>
    			  <div id="out"></div>
    
    			</td>
    		</tr>
    	</table>
    	</form>
    
    Code (markup):
    Part of the PHP Code:
    
    $alexac  = preg_replace("@[\r\n]@", "", preg_replace($search,$replace,trim(implode('', file("http://www.*****.com/***/**/***/".$website."?page=1&q=link:".$website)))));
    
    $rcount = preg_match_all("@1- 10of (.*?) linked to@mis", $alexac, $rcout);
    
    $rc = floor(intval(str_replace(",","",$rcout[1][0]))/10);
    
    Code (markup):
     
    mycomputerguide, Dec 26, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is a bug. You probably want
    response.indexOf('|')  != -1
    Code (markup):
    .

    In the mean time log $rc to a file on your server after you assign to it to see if it contains what it should. I use error_log for this.

    
    
    $rc = floor(intval(str_replace(",","",$rcout[1][0]))/10);
    
    error_log  ("\$rc = $rc\n", 3,  'debug.log.txt');
    
    
    PHP:
     
    LogicFlux, Dec 26, 2008 IP
    GTech likes this.