Click here to Skip to main content
12,511,553 members (51,221 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript HTML PHP Ajax
How can I call a javascript function without onclick event?? I want use Ajax code in my project without click , when I update the Mysql from another page, show automatic and immediately in my page.

<html>
<head>
<script type="text/javascript">
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","xxxxxx.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body  önload="showUser();">
 
 
<div id="txtHint"  runat="server"  önclick="showUser()">
<form id="txtHint" onchange="showUser(this.value) method="GET">
 

<table border="0" cellpadding="0" cellspacing="0" align="center" valign="center">
                style="width: 20%; height: 50%;">
                <tr>
                    <td bgcolor="Gray" class="style17" colspan="2" rowspan="2" valign="top">
               <?php        					  
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
 
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
 
@mysql_select_db("HOUSEDATA") or die( "Unable to select database");
 
$query="SELECT Door FROM Door";
 
$result=mysql_query($query);
$image=mysql_result($result,0);
if($image == 1)
  	 echo  "<img src='Images/bulb_off.png'>";
if($image == 0)
      echo  "<img src='Images/bulb_on.png'>";
mysql_close();
?>
                    </td>
                   
            </tr></table>
 
</form>
 
</body>
</html>
Posted 27-Nov-11 4:06am
Updated 27-Nov-11 18:00pm
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Have you tried the setInterval() javascript function?
It will automatically execute your code in a specified time(in milliseconds).
Usage:
setInterval( [you code or function call][, time interval in milliseconds] );
this will take care of updating your page via ajax without the need for user interaction.

Instead of calling your custom function in the onload event, you can try this:

<script type="text/javascript">
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","xxxxxx.php?q="+str,true);
xmlhttp.send();
}
 
// create an interval calling the specified function every 10 seconds.
setInterval('showUser()',10000); 
 
//or you can use it like this
setInterval(function(){ showUser();}, 10000);
</script>
  Permalink  
v4
Comments
Mohammad Ali_7 28-Nov-11 21:50pm
   
I dont know how can I use in my code could u please explain more? where can I put this line? thank you for answer.
Mohammad Ali_7 28-Nov-11 22:11pm
   
Thank youuuuuuuu , finally It's working. really thank you ;)
astjane 28-Nov-11 22:17pm
   
Thank you! Glad it worked for you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It's very simple :

Call the JavScript Function/Code on body onLoad event.

<html>
<body  önload="showUser();">
    ...............
    ...............
    ...............
</body>
</html>

Or
<html>
<script language="'javascript'">
function happycode(){
   alert('helo');
}
</script>
<body> 
<h1>Javascript call after page loaded</h1>
 
<script>
//call after page loaded
window.onload=happycode ; 
</script>
</body>
</html
Or Try this :

call a javascript function after a page load
  Permalink  
v3
Comments
Mohammad Ali_7 27-Nov-11 10:51am
   
Thank you for answering but doesn't work. Is not the solution ! I try before! any idea?
Manoj K Bhoir 27-Nov-11 11:33am
   
Try updated solution.I hope it will help you.
Mohammad Ali_7 27-Nov-11 11:49am
   
really tanx to helping, and maybe ur solution is right but still can not refresh the page when put it end of the page ! I tried to research
Mohammad Ali_7 27-Nov-11 11:52am
   
maybe because my page is php , and this solution is for html, I want to refresh the page AUTOMATIC , without onclick or button, because I want refresh my PHP query in table !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 28 Nov 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100