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
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);
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.