2 Replies - 411 Views - Last Post: 19 August 2012 - 07:04 AM

#1 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Ajax with PHP problem...

Posted 19 August 2012 - 05:56 AM

Hi there, I am having trouble passing a variable via POST through a xmlhttp request: I have a main page index.php and in there a form that contains a textarea and a submit button.

In index.php:
echo <<< SEARCH
	<div align="center">
		<form name="SearchBar" method="post" action="index.php?"> 
  			<div align="center"> 
     			<textarea id="textinput" name="qinput" cols="80" rows="4" onclick="request_information('test',this.value)">$mesg</textarea>
				<input type="submit" name="SearchButton" value="Search" /> 
			</div> 
		</form>
	</div>
SEARCH;


I linked a javascript file that contains the "request_information" function:

// service request function
function request_information( element_id,str )
{
	// get the object used to do 
	var xmlhttp;
	//var element_id = "test";
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 	}
 	
 	// message while waiting
 	document.getElementById(element_id).innerHTML = "<p> request_information: Searching...</p>";
 	
 	// assign the request callback
 	xmlhttp.onreadystatechange=function()
  	{
  		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
    		document.getElementById(element_id).innerHTML="<p> request_information:" + xmlhttp.responseText + "</p>";
    	}else
    	{
    		document.getElementById(element_id).innerHTML= "<p> request_information: Failed</p>";
    	}
  	}
 	var query_str = "helluuuu";	
 	xmlhttp.open("POST","cat.php?qu="+query_str,true);
 	xmlhttp.send();
}




Here I'm trying the simplest case scenario by trying to pass the value of variable "query_str" to the "cat.php" file which contains:

<?php
	include "connect.php";
	
	// new process request
	$status = request_process(0);
	
	$q = "";
	if (isset($_POST["qu"]))
		$q = $_POST["qu"];
	else
		$q = "qu not passed";
	echo <<<yea

<h5> PID: $status </h5>
<h5> Query: $q </h5>

yea;
?>



I always get "qu not passed" where I print output. Before I handled the error with if statements I was getting "Undefined index 'qu' in 'cat.php' refered from 'index.php?'" in apache2 error log. Also, the 'this.value' parameter I pass doesn't work neither. All these files are in the same directory except the javascript file which is in a js/ directory..

According to W3C examples, this should work. Have I missed something crucial here?

Thankyou for your help!
dustyd

Is This A Good Question/Topic? 0
  • +

Replies To: Ajax with PHP problem...

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Ajax with PHP problem...

Posted 19 August 2012 - 06:07 AM

You're not passing it as a POST variable, you're passing it in the URL, making it available through the GET superglobal array. For a POST request you need to send the data not in the URL, but in the send() function, and set the appropriate HTTP headers.

For you to read.
Was This Post Helpful? 1
  • +
  • -

#3 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: Ajax with PHP problem...

Posted 19 August 2012 - 07:04 AM

Thankyou JackOfAllTrades, that was incredibly accurate and useful information. Everything works as expected.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1