I have a simple search form with a search box and a result box.
When I type a search word a request is created like: http://www.site.com/php_handler.php?s=hello
In the php script and a result is given back to the script this way:
<?php return $s; ?>
The problem is that my htmlrequest stops at readyState 3 it doesn't get to 4.
The javascript looks like this:
var xmlhttp = sajax_init_object();
function sajax_init_object() {
var A;
try {
A=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
A=new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
A=null;
}
}
if(!A && typeof XMLHttpRequest != "undefined")
A = new XMLHttpRequest();
if (!A)
sajax_debug("Could not create connection object.");
return A;
}
function getSearchItem()
{
gs=document.forms.mainform.resultsfield;
var searchword=document.forms.mainform.searchform.value;
if (searchword.length>=3)
{
setWaitCursor();
clearResults();
var uri = "http://site.com/ajax_handler.php?s="+searchword;
console.log(uri);
xmlhttp.open("GET", uri, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4) {
processResults(xmlhttp.responseText);
removeWaitCursor();
}else{
console.log(xmlhttp.readyState);
}
}
xmlhttp.send(null);
}
else
{
alert("please add at least 3 characters .");
}
}
Can someone tell me why it stops at 3?
edit: here is also the php code:
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
session_start();
//include main file
require_once($_SESSION["FILEROOT"] . "xsite/init.php");
//check if formulier is posted
$zoekterm = C_GPC::getGETVar("s");
$s="";
if ($zoekterm != "") {
$query="SELECT number,name,city,zib,zip_annex FROM articles WHERE version='edit' AND (naam LIKE '%$school%' OR brinnummer='$school') ORDER BY name";
if ($rs=C_DB::fetchRecordSet($query)) {
while ($row=C_DB::fetchRow($rs)) {
if ($row["plaats"]!="") {
$s.=$row["name"].", ".$row["city"]."|".$row["number"]."\n";
} else {
$s.=$row["name"].", ".$row["zip"].$row["zip_annex"]."|".$row["number"]."\n";
}
}
}
}
return $s;
?>
edit:
I missed a semicolon in my php script and now the ready state only gets to 2
edit:
The problem is even different. It gets to 4 but it doesn't show the result text.
1> Don't send Cache-Control: post-check=0, pre-check=0. These don't do what you think they do, and they're entirely unnecessary.
2> Your AJAX results page needs to send a Content-Length or Connection: Close header.
3> Try adding a random to your request URL to ensure you're not looking at a stale cache entry.
ReadyState 3 => Some data has been received
ReadyState 4 => All the data has been received
Maybe the XMLHTTPRequest object is still waiting for some data.
Are you sure your php script ends correctly ?
Is the content-length alright ?
To debug this you have two options, type the URL directly into the browser [since you are using a GET] and see what is happening.
OR
You can use a tool such as Fiddler and see what is exactly happening with the XMLHttpRequest
Related
ag.php
<?php
ignore_user_abort(true);
set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
$i=0;
while(1){
echo $i;
$i++;
ob_flush();
flush();
if (connection_aborted()){
break;
}
usleep(1000000);
}
ajax:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
console.log(xhttp.readyState+" "+xhttp.status);
if (xhttp.readyState === 3 && xhttp.status ===200) {
console.log(xhttp.responseText+"\n");
}
};
xhttp.open("GET", "ag.php", true);
xhttp.send();
hi, at above code, i want to make a persistent connection with php and echo data in while block in 1 second interval and data comes browser like this;
0
01
012
0123
...
but i want echo data to browser like;
0
1
2
3
...
but couldn't achive it so i found this [How to clear previously echoed items in PHP
about my question but not exactly what i want i think.
anybody know is there any way to empty/remove previous echoed data? is it possible? help please.
use substr()
var xhttp = new XMLHttpRequest();
var lastResponse = '';
xhttp.onreadystatechange = function() {
//console.log(xhttp.readyState+" "+xhttp.status);
if (xhttp.readyState === 3 && xhttp.status ===200) {
var currentAnswer = xhttp.responseText.substr(lastResponse.length);
lastResponse = xhttp.responseText;
console.log(currentAnswer+"\n");
}
};
xhttp.open("GET", "ag.php", true);
xhttp.send();
Perhaps ob_clean is what you are looking for.
void ob_clean ( void )
This function discards the contents of the output buffer.
This function does not destroy the output buffer like ob_end_clean() does.
The output buffer must be started by ob_start() with PHP_OUTPUT_HANDLER_CLEANABLE flag. Otherwise ob_clean() will not
work.
I am having trouble executing this code in my index.php.
It says 'CartAction not set'
I need your help php gurus. I can display any files you need to fix this error.
Here is the code:
// Handle AJAX requests
if (isset ($_GET['AjaxRequest']))
{
// Headers are sent to prevent browsers from caching
header('Expires: Fri, 25 Dec 1980 00:00:00 GMT'); // Time in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: text/html');
if (isset ($_GET['CartAction']))
{
$cart_action = $_GET['CartAction'];
if ($cart_action == ADD_PRODUCT)
{
require_once 'C:/vhosts/phpcs5/presentation/' . 'cart_details.php';
$cart_details = new CartDetails();
$cart_details->init();
$application->display('cart_summary.tpl');
}
else
{
$application->display('cart_details.tpl');
}
}
else
trigger_error('CartAction not set', E_USER_ERROR);
}
else
{
// Display the page
$application->display('store_front.tpl');
}
It's because your code is expecting a parameter named 'CartAction' in the url
Example:
www.yoursite.com/?CartAction=ADD_PRODUCT
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character. Source
You check if $_GET['CartAction'] has a value ( from the above url this superglobal variable has the value 'ADD_PRODUCT' )
What #Mackiee (in comments) and your error message are both telling you is that the problem is that there is a query parameter missing. The URL that calls this needs to include either ?CartAction=ADD_PRODUCT or &CartAction=ADD_PRODUCT
I am trying to call a PHP function using AJAX. Below is the script I used.
<script type="text/javascript" src="jquery.1.4.2.js">
$(document).ready(function () {
// after EDIT according to
// #thecodeparadox answer
$('#local').click(function(e){
e.preventDefault();
e.stopPropagation();
promptdownload();
});
});
function promptdownload(e)
{
$.ajax({
type: "POST",
url: "js/prompt.php",
data: { "get" : "runfunction", "action" : "promptlocal" },
success: function (response) {
}
});
}
</script>
The corresponding PHP code (prompt.php) is:
<?php
$path1 = "downloads/1.jpg";
$browserFilename1 = "Local Travel";
$mimeType1 = "image/jpeg";
function promptToDownload($path, $browserFilename, $mimeType)
{
if (!file_exists($path) || !is_readable($path)) {
return null;
}
header("Content-Type: " . $mimeType);
header("Content-Disposition: attachment; filename=\"$browserFilename\"");
header('Expires: ' . gmdate('D, d M Y H:i:s', gmmktime() - 3600) . ' GMT');
header("Content-Length: " . filesize($path));
// If you wish you can add some code here to track or log the download
// Special headers for IE 6
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
$fp = fopen($path, "r");
fpassthru($fp);
}
if ($_POST["action"] = 'promptlocal')
{
promptToDownload($_GET[$path1], $browserFilename1, $mimeType1);//comments
}
?>
This is how I code the button that is supposed to trigger the function:
<input type="button" id="local" name="local" value="Local Travel">
My expected output is to have this button promt the user: "where to save 1.jpg file".
However I couldn't make it work.
Any advise is highly appreciated.
$('local').click(function(e){
should be
$('#local').click(function(e){
As local is an id so you should use # before it. And also in your php code there are some missing quotes.
Use Firebug(FF), Dragonfly(Opera), Developer Tools(Chrome). You can see all javascript errors, warnings and exceptions, and can see ajax requests data.
data: { "get" : "runfunction", "action" : "promptlocal" },
Try to remove the quotes from "get" and "action".
Like this :
data: { get : "runfunction", action : "promptlocal" },
It looks to me like you are trying to download a file with jquery/ajax. You will not get this to work with only ajax. This question has been answered several times on stackoverflow.
I hope this link will help you: Ajax File Download using Jquery, PHP
I am trying to stream data read from a file to chrome client. I am able to stream data successfully but my responses are getting cached and I want to prevent that from happening. This situation is there because my flat file contains data entries which are independent of each other and i want to treat them likewise. For example, my file contains :
{idle_time:94125387364,system_time:98954710321,user_time:3683963615}
{idle_time:94125387789,system_time:98954710456,user_time:3683963845}
{idle_time:94125387876,system_time:98954710678,user_time:3683963986}
so instead of getting
{idle_time:94125387876,system_time:98954710678,user_time:3683963986}
(THIRD ENTRY)
as xmlhttprequest.responsetext, I receive
{idle_time:94125387364,system_time:98954710321,user_time:3683963615} <br/>
{idle_time:94125387789,system_time:98954710456,user_time:3683963845} <br/>
{idle_time:94125387876,system_time:98954710678,user_time:3683963986}
NOTE : I am not worried about breakline tags and blankspace.
My PHP script looks like this,
test.php
<?php
set_time_limit(0);
$filename = 'D:\Smoke_Test\data.txt';
function flush2 (){
echo(str_repeat(' ',256));
// check that buffer is actually set before flushing
if (ob_get_length()){
#ob_flush();
#flush();
#ob_end_flush();
}
#ob_start();
}
$file_last_modified_time = 0;
while(true)
{
$modified_time = filemtime($filename);
$processor_info = "";
if ($file_last_modified_time < $modified_time)
{
header("Expires: Sun, 20 Jan 1985 00:00:00 GMT"); // date in the past
header("Cache-Control: no-cache");
header("Pragma: no-cache");
$file_last_modified_time = $modified_time;
$handle = fopen($filename,"r");
$processor_info = fgets ($handle);
fclose ($handle);
#ob_clean();
echo $processor_info."<br/>";
//flush2();
}
flush2();
sleep(1);
clearstatcache(true, $filename);
}
?>
and my html page looks like this:
Home.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type="text/javascript" language = "javascript">
function read_file ()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 || xmlhttp.readyState==3 ) //&& xmlhttp.status==200)
{
handle_data (xmlhttp.responseText);
}
}
xmlhttp.open("POST","test.php",true);
xmlhttp.send();
}
function handle_data (input)
{
document.getElementById("txtResponse").innerHTML=input;
}
</script>
</head>
<body>
<p>
<input type="button" id="dtnSendRequest" value="Send Request" onclick="read_file()"/>
</p>
<p>
response : <span id="txtResponse"></span>
<!-- <input type="text" id="txtResponse" width="500"/> -->
</p>
</body>
</html>
try adding this to the top of your php file
header("Expires: Sun, 20 Jan 1985 00:00:00 GMT"); // date in the past
header("Cache-Control: no-cache");
header("Pragma: no-cache");
Use this to force no-cache:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
Make sure you call this before sending any output.
You cant avoid whats happening with responseText. Its the nature of Http Streaming (not local or any caching) makes that happen. I found a very good article which states my situation as well as the solution.
The article is http://ajaxpatterns.org/archive/HTTP_Streaming.php
Following paragraph contains my situation as well as solution for it.
"The responseText property of XMLHttpRequest always contains the content that's been flushed out of the server, even when the connection's still open. So the browser can run a periodic check, e.g. to see if its length has changed. One problem, though, is that, once flushed, the service can't undo anything its output. For example, the responseText string arising from a timer service might look like this: "12:00:00 12:01:05 12:01:10", whereas it would ideally be just "12:00:00", then just "12:01:05", then just "12:01:10". The solution is to parse the response string and only look at the last value. To be more precise, the last complete value, since it's possible the text ends with a partial result. An example of this technique works in this way. To ease parsing, the service outputs each message delimited by a special token, "#END#" (an XML tag would be an alternative approach). Then, a regular expression can be run to grab the latest message, which must be followed by that token to ensure it's complete"
Hi I have two dropdowns and I am populating second one on the basis of first one. My first select is
<select name="projects" id="projects" onchange="populate_users(this.value);">
<option value='1'>ABC</option>
<option value='2'>DEF</option>
</select>
And I populate second select box on the basis of first one which is
<select name="users" id="users">
</select>
Here is my populate_users method
function populate_users(project_id)
{
var url='<?php echo($this->url(array(),'admin/clientproject1'));?>';
url2=url+'project_id='+project_id;
//alert(url2);
jQuery('#users').html('<div style="position:absolute;">'+jQuery('#users').html());
//ajax call
jQuery.ajax({url:url2,success:function(data){jQuery('#users').html(data);}});
}
And on admin/clientproject1 I simply query to table and start a loop to draw options like this
$rd=$db->fetchAll($q);
for($i = 0; $i < count($rd); $i++)
{
?>
<option value="<?php echo($rd[$i]->id);?>">
<?php echo($rd[$i]->username);?></option>
<?php
}
?>
$rd is having values. The second select is populated and all values are showing in
Firefox but in IE it is just showing blank dropdown and not showing any error.
You should return a json string of values and create the option elements in javascript. Then inject those option elements into your select. A bit more coding, but it's better.
Whenever I see the words AJAX, IE, and not populating, I think of the IE cache and how it doesn't play nice with AJAX. Since the issue is browser based, I don't see how it can be a problem with your actual php and the formatting appears to be correct, maybe try setting some headers in the php page that is querying the db.
header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
My solution is create the 'option adding' in server and javascript 'eval' in client the passing Ajax result:
Server Sample PHP code Populate Select :
leetablaIE.php :
$DBName=$_GET["db"];
$tabla=$_GET["tabla"];
$Query=$_GET["query"];
$id=$_GET["id"];
$User="???????";
$Host="????????";
$Password="????????";
$Link=mysql_connect( $Host, $User, $Password);
if (!$Link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($DBName, $Link) or die('Could not select database.');
$Result=mysql_db_query ($DBName , $Query , $Link);
echo 'var obj_option;';
echo "obj_option = document.createElement('option');" ;
echo "obj_option.setAttribute('value', '');";
echo "obj_text = document.createTextNode('".utf8_encode('Select Value')."');" ;
echo "obj_option.appendChild(obj_text);";
echo "document.getElementById('$id')".".appendChild(obj_option);"; //myselect
while($Row = mysql_fetch_array($Result)) {
$valor=utf8_encode($Row[0]);
$texto=utf8_encode($Row[1]);
echo "obj_option = document.createElement('option');" ;
echo "obj_option.setAttribute('value', '".$valor."');";
echo "obj_text = document.createTextNode(\"".$texto."\");" ;
echo "obj_option.appendChild(obj_text);";
echo "document.getElementById('$id')".".appendChild(obj_option);"; //myselect
}
mysql_free_result($Result);
in Javascript Client :
function leetabla(db,tabla,query,id,bro){
// bro = passing browser ex: MSIE
// id = passing id select to populate in example myselect
// db = database
// tabla = table (mysql id this case)
// query = query you need to populate ('select ....')
////////////////////////////////////////////////////
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if(bro!='MSIE'){
document.getElementById(id).innerHTML="";
document.getElementById(id).innerHTML=xmlhttp.responseText;
}
else
{
//alert(xmlhttp.responseText);
eval(xmlhttp.responseText);
}
}
}
if(bro!='MSIE'){
//alert(query);
//normal populate code not in this code 'leetabla.php'
xmlhttp.open("GET","leetabla.php? db="+db+"&tabla="+tabla+"&query="+query+"&id="+id,true);
}
else
{
//alert(query);
// call to php script to IE case leetablaIE.php (upper sample)
xmlhttp.open("GET","leetablaIE.php? db="+db+"&tabla="+tabla+"&query="+query+"&id="+id,true);
}
xmlhttp.send();
}