Jump to content

need help with refresh php


dezkit

Recommended Posts

i have this php code

<?php
session_start();
session_register("count");
$sessid = session_id();
if (!isset($_SESSION)) 
{
$_SESSION["count"] = 0;
echo "<p>Counter initialized</p>\n";
} 
else {
       $_SESSION["count"]++; }
         echo "<p>The counter is now <b>$_SESSION[count]</b></p>";
  echo "<p>please reload this page to increment</p>";
?>

Click <a href="javascript:function?">here</a> to refresh the page.

 

And i was wondering so that if somebody presses "here" the counter adds by one without the browser refreshing, thanks and please post back :)

Link to comment
https://forums.phpfreaks.com/topic/125893-need-help-with-refresh-php/
Share on other sites

//CREATES A XMLHttpRequestObject
function newXHRO()
{
  try { return new XMLHttpRequest(); } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  alert("XMLHttpRequest not supported");
  return null;
}

//Make call  (pass the function an id
function updateCtr(what)
{
  var x = document.getElementById(what);
    x.innerHTML = 'Updating...';

  var XHRO = new newXHRO();
  var url = 'scriptname.php';  // CHANGE THIS TO YOUR URL
  
  XHRO.onreadystatechange=function()
  {
    if(XHRO.readyState==4 && XHRO.status==200)
    {
      x.innerHTML = XHRO.responseText;
    }
  }
  
  XHRO.open("GET",url,true);
  XHRO.send(NULL);
}

what? lol.

 

<?php
session_start();
session_register("count");
$sessid = session_id();
?>
<script language="javascript" type="text/javascript">
<!-- 
function newXHRO()
{
  try { return new XMLHttpRequest(); } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  alert("XMLHttpRequest not supported");
  return null;
}
//Make call  (pass the function an id
function updateCtr(what)
{
  var x = document.getElementById(what);
    x.innerHTML = 'Updating...';

  var XHRO = new newXHRO();
  var url = 'test1.php';  // CHANGE THIS TO YOUR URL
  
  XHRO.onreadystatechange=function()
  {
    if(XHRO.readyState==4 && XHRO.status==200)
    {
      x.innerHTML = XHRO.responseText;
    }
  }
  
  XHRO.open("GET",url,true);
  XHRO.send(NULL);
}
//-->

</script>


<?php
if (!isset($_SESSION)) 
{
$_SESSION["count"] = 0;
echo "<p>Counter initialized</p>\n";
} 
else {
        $_SESSION["count"]++; }
          echo "<p>The counter is now <b>$_SESSION[count]</b></p>";
  echo "<p>please reload this page to increment</p>";
?>

Click <a href="javascript:updateCtr(what)">here</a> to refresh the page.

 

is this corerct?

that is your ajax code - that xtopolis just provided you:

 

//CREATES A XMLHttpRequestObject
function newXHRO()
{
  try { return new XMLHttpRequest(); } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  alert("XMLHttpRequest not supported");
  return null;
}

//Make call  (pass the function an id
function updateCtr(what)
{
  var x = document.getElementById(what);
    x.innerHTML = 'Updating...';

  var XHRO = new newXHRO();
  var url = 'scriptname.php';  // CHANGE THIS TO YOUR URL
  
  XHRO. />  {
    if(XHRO.readyState==4 && XHRO.status==200)
    {
      x.innerHTML = XHRO.responseText;
    }
  }
  
  XHRO.open("GET",url,true);
  XHRO.send(NULL);
}

 

put your php script in a different page and name it "scriptname.php" (for example purposes only - name it what you want to; just make sure you define the "url" variable in the AJAX script above; with the same name).

 

<?php
session_start();
session_register("count");
$sessid = session_id();
if (!isset($_SESSION)) 
{
$_SESSION["count"] = 0;
echo "<p>Counter initialized</p>\n";
} 
else {
        $_SESSION["count"]++; }
          echo "<p>The counter is now <b>$_SESSION[count]</b></p>";
  echo "<p>please reload this page to increment</p>";
?>

 

then in your html page; if your calling the function with link - do it something like this:

 

<span id="counter"></span>

Click <a href="javascript:updateCtr('counter')">here</a> to refresh the page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.