XMLHttpRequest delays in starting session - php

Iam beginner to ajax. When i used ajax for search function it delays in starting operation
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?gender="+str,true);
xmlhttp.send();
}
</script>
When the radio button on the product.php page is clicked showUser(str) is called. The value is accessed in variable gender in getuser.php and the session started with the value in varible gender and the sql query is executed using the session variable
$sql="SELECT * FROM tbl_product WHERE gender = '".$_SESSION['gender']."'";
and the values are echoe in txtHint div. My problem is When i clicked the radio butoon for the very first time the function is called, but the value is not echoed in page.When i reloaded the page and try it again it works fine.Please help to fix this
Thankyou

Put your code in
window.onload
function. Like this
window.onload = function() {
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?gender="+str,true);
xmlhttp.send();
}
};

Related

AJAX Nearly identitcal xmlhttp, one works the other does not?

I'm having a weird problem...
I inserted an alert here:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
alert('fine');
}else {
alert('fail');
}
Both functions that call the xmlhttp alert "fail" but one works while the other does not.
I checked that the serialize was working, and it is, I get the written value in the form.
The php script doesn't show an error... any idea what could be going on? The calls are separate, not called simultaneously, I pretty much replicated the working one for the other call.
This is the full xmlhttp function of the working one
function insertEntry()
{
var data = $("#entry").serialize();
var xmlhttp;
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.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
alert('fine');
}else {
alert('fail');
}
}
xmlhttp.open("POST","write-script.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);
clearup();
showSuccess();
}
Not working, the alerts go all the way through
function insertNote()
{
alert('clicked');
var note = $("#note").serialize();
alert(note);
var xmlhttp;
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.status==200)
{
// document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
alert('fine');
}else {
alert('fail');
}
}
xmlhttp.open("POST","write-new-note.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(note);
alert('done');
}

Refresh div every two seconds using ajax without jquery

I made a chat website. And it has a div with id=chatlogs. I want it to get chatlogs from logs.php every 2 seconds. How can I do that? I want to use ajax with it and avoid using jquery.
You can do smoething like this:
<script>
setInterval(refresh_logs(), 2000); // 2000 = 2 Seconds
function refresh_logs()
{
var xmlhttp;
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.status==200)
{
document.getElementById("logs_div").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","get_logs.php",true);
xmlhttp.send();
}
</script>

query string syntax in ajax function in php to get results from the redirected page

<script>
function serviceAdd(rateid,vendorid,countrycode,destination,routetype,rate)
{
alert(rateid+vendorid+countrycode+destination+routetype+rate);
var xmlhttp;
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.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","check_servicing.php?rate_id="+rateid+"&vendor_id="+vendorid+"&countrycode_id="+countrycode+"&destination_id="+destination+"&routetype_id="+routetype+"&rate="+rate+"",true);
xmlhttp.send();
}
</script>
This is the ajax function. Its values are fetched but not redirecting.
This page is not redirecting to check_servicing.php.
Please Help!!

Clearinterval not working in ajaxrequest

I made an ajax call and im calling a setInterval function which is working fine. Unfortunately when im trying to stop it it's not working.
function test(str)
{
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.status==200)
{
if (str=="?stage=3"){
test('?stage=4');
}
if (str=="?stage=4"){
document.getElementById("main").innerHTML+=xmlhttp.responseText;
prog=window.clearInterval(prog);
}else{
document.getElementById("main").innerHTML+=xmlhttp.responseText;
}
}
else
{
if (str=="?stage=3"){
var prog = self.setInterval( "ajaxrequest('progress_track.php', 'main')", 1000 );
}
}
}
xmlhttp.open("GET","test.php"+str,true);
xmlhttp.send();
}
Any help is really appreciated.
EDIT:
I remade my code because i found out that it calls somehow 3 times the setinveral function. Whit this code it only calls 2 times. I just dont understand why.
var prog = 0;
function test(str)
{
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.status==200 && str=="?stage=3")
{
test('?stage=4');
}else{
if( str=="?stage=3"){
prog = self.setInterval( "ajaxrequest('progress_track.php', 'main')", 1000 );
}
}
if (xmlhttp.readyState==4 && xmlhttp.status==200 && str=="?stage=4")
{
prog=window.clearInterval(prog);
}
if (xmlhttp.readyState==4 && xmlhttp.status==200 && str!="?stage=4" && str!="?stage=3")
{
document.getElementById("main").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","test.php"+str,true);
xmlhttp.send();
}
But the cleariterval still not working.
EDIT:
I found the problem. Whit this code it never arrives in this state:
if (xmlhttp.readyState==4 && xmlhttp.status==200 && str=="?stage=4")
{
prog=window.clearInterval(prog);
}
Now the only question is why not?
You need to declare "prog" at a higher scope. You could move it outside of your function declaration:
var prog;
function test(str) {
...
if (str=="?stage=3"){
prog = self.setInterval(
"ajaxrequest('progress_track.php', 'main')", 1000
);
}
...
}
The way you've got it right now, prog is undefined when you call clearInterval()
The reason it's not working is because you're declaring the variable to keep the timer ID inside your function, yet you're trying to use it before it's defined. When you are trying to clear it, the variable is not defined. What you can do is define the variable outside and then use it. Something like this:
var prog = 0;
function test(str)
{
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.status==200)
{
if (str=="?stage=3"){
test('?stage=4');
}
if (str=="?stage=4"){
document.getElementById("main").innerHTML+=xmlhttp.responseText;
prog=window.clearInterval(prog);
}else{
document.getElementById("main").innerHTML+=xmlhttp.responseText;
}
}
else
{
if (str=="?stage=3"){
prog = self.setInterval( "ajaxrequest('progress_track.php', 'main')", 1000 );
}
}
}
xmlhttp.open("GET","test.php"+str,true);
xmlhttp.send();
}
You may also want to check whether it's 0 before clearing. If it's 0 it means it's not set yet and it doesn't need to be cleared.

2 AJAX functions on one page - Follow/Unfollow button with PHP - not working

I want to have a Follow/Unfollow button on a page.
The user clicks Follow and this passes the parameter 'artist' to the PHP script, updates a db and the button then says Unfollow.
When the user clicks Unfollow, this passes the parameter 'artist' to the same PHP script and this then updates a db via PHP and then button then says Follow.
I can get the Follow to change to Unfollow but cannot get Unfollow to change to Follow.
My code is as follows:
index.php
<script type="text/javascript">
function followArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
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.status==200)
{
document.getElementById("artist").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=y",true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function unfollowArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
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.status==200)
{
document.getElementById("artist").innerHTML=XMLHTTPlhttp.responseText;
}
}
xmlhttp.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=n",true);
xmlhttp.send();
}
</script>
<!--Follow button for user to click-->
<div class="follow_text" id="artist">
<h1>Follow artist</h1>
</div>
follow.php
<?php
if(isset($_GET['follow']))
{
$follow = $_GET['follow'];
if($follow=='y')
{
$artist = $_GET['artist'];
#############do a database action
?>
<h1>Unfollow artist</h1>
<?php
}
else
{
$artist = $_GET['artist'];
#############do a database action
?>
<h1>Follow artist</h1>
<?php
}
}
?>
Any ideas why this is not working?
You can see a live version of the script at http://soundshelter.net/release.php?id=421928
Thanks in advance!
I have changed the second xmlhttp object which throws error.
<script type="text/javascript">
function unfollowArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("artist").innerHTML=xmlhttp2.responseText;
}
}
xmlhttp2.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=n",true);
xmlhttp2.send();
}
</script>
<!--Follow button for user to click-->
<div class="follow_text" id="artist">
<h1>Follow artist</h1>
</div>
Your second
<h1>Unfollow artist</h1>
shoud be Follow artist

Categories