php script runs too many times - php

This is the XML code I use in index.html which calls the show_vote.php script.
I call this function in the body of index.html like this<script>showVoteResult()</script>. For some reason, in the CPU usage of my cpanel, it says the PHP files are requested over and over again and has exceeded the CPU usage limit. I need help understanding where I did wrong.
function showVoteResult() {
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 (this.readyState==4 && this.status==200) {
document.getElementById("currentVote").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","show_vote.php",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');
}

Can't get Response From Ajax

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

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>

Cannot make call from one php file to another php file

I have 3 files: fileNum1.php, fileNum2.php and fileNum3.php.
fileNum1.php file makes a call for fileNum2.php 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.open("GET","fileNum2.php",false);
xmlhttp.send();
and fileNum2.php makes a call for fileNum3.php:
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","fileNum3.php",false);
xmlhttp.send();
The call to fileNum3.php is NOT WORKING!
What is the the problem?
(Can it be because I've started new XMLHttpRequest inside existing XMLHttpRequest?)
JavaScript is client-side code. This means that a browser is required to interpret and execute the JavaScript code.
If you access fileNum1.php via a browser, the call to fileNum2.php will execute, as the JavaScript for fileNum1.php is interpreted.
The call from fileNum2.php to fileNum3.php will NOT execute, as it is not returned to the browser. The JavaScript on the 3rd file will be returned as output to the server, which is making the request, and the JavaScript will never make it back to the browser.
If you want an action from fileNum3.php to return to fileNum1.php, you will have to catch the data when fileNum3.php is called on fileNum2.php, and then return the response to fileNum1.php.
Or, you can do it the right way and say if fileNum1.php called fileNum2.php and everything was ok, then call fileNum3.php.
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","fileNum2.php",false);
xmlhttp.send();
if(xmlhttp.responseText !== "undefined"){
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.open("GET","fileNum3.php",false);
xmlhttp2.send();
}

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.

Categories