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');
}
Related
Can't get responce from Following ajax Script That Call Php Script that Echo Cost Of Given Item name
function ajaxfunc2(Item_name)
{
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("cost").value=xmlhttp.responseText;
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","find.php?Item="+Item_name,true);
xmlhttp.send(null);
}
But I will Get Output from Php File
with this link
127.0.0.1:8888/Pharmaceutical Distribution System/PDS/find.php?Item=1st tshirt
<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!!
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();
}
};
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.
This is a very simple function if one where to use an input tag, unfortunately i have to use a link tag since i'm working with alphabet grouping that would retrieve the values on a different page.
here's a code i found for link tag to hold a value to be submitted to ajax
echo '<a onclick="passPagination(\'3\');passLetter(\'S\')">NEXT</a>';
i want to call all letters "s" page "3" from the other page.
function passLetter(str)
{
if (str=="")
{
document.getElementById("retail_group").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("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?letter="+str,true);
xmlhttp.send();
}
function passPagination(str)
{
if (str=="")
{
document.getElementById("retail_group").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("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?pageno="+str,true);
xmlhttp.send();
}
also i want to call again the letter that was passed to the otherpage so that i would make it as a reference for my pagination links that is also being hold in this page.
echo 'NEXT';
i am really at lost here since i am not familiar with link tag and ajax working together, also as you may have noticed i am not also that good with ajax judging from my very long ajax code.
Your help is greatly appreciated, Thank You.
i am really new at ajax. :)
The solution is to make a function with multiple arguments:
function passPaginationAndLetter(page, letter)
{
if (page=="" || letter == "")
{
document.getElementById("retail_group").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("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?letter="+letter+"&pageno="+page,true);
xmlhttp.send();
}
You can now call this with
echo '<a onclick="passPaginationAndLetter(\'3\',\'S\')">NEXT</a>';
The difference is that you are not calling two AJAX methods, just one.
I'm not pretty sure if i got what you mean, but i guess you want to something like that
echo '<a onclick="get(\'S\', '. ($_GET['page'] + 1) .')">NEXT</a>';
function get(letter, page) {
if (!letter) {
document.getElementById("retail_group").innerHTML="";
return;
}
if (!page) {
page = 1;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?letter=" + letter + "&pageno=" + page,true);
xmlhttp.send();
}
if it's really that, don't forget to validate the variables got from $_GET before use that, i just didn't because it's not the point...
I think this is what you're looking for:
html
PREVIOUSNEXT
javascript
var current = {
page: 0,
letter: a
};
function ajax(url) {
var xmlhttp = window.XMLHttpRequest || new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByI("retail_group").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function next() {
ajax("otherpage.php?letter=" + current.letter + "&pageno=" + (++current.page));
}
function previous() {
ajax("otherpage.php?letter=" + current.letter + "&pageno=" + (--current.page));
}
This code is not complete of course, it doesn't check for the max/min values of the pages, and there's no way of replace the letter, but it should point you in the right direction.