Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP with Ajax Intergration

Status
Not open for further replies.

tedal

Technical User
Mar 16, 2011
3
0
0
US
Hello Guys.

I am not sure if this belongs in the PHP forums or the Javascript forum, but I'll post it here unless told otherwise.

I have been trying to write a little web interface page that would allow a user to run a script when they clicked a button.

The first time they clicked the button it would create a timestamp in a .tmp file. The second time they clicked it, it would compare that time stamp with the current time and if it has been been, lets say 30 minutes, it would recreate the timestamp. If it had not been 30 minutes yet, it would let you know.

When the button accepts the input, meaning it has been over 30 minutes, I wanted it to execute a VBScript, but no matter what I do I can't get it to work. Right now it is a simple VBScript that ECHO's "HI". Here is the code I am using.

This is the index.php
Code:
<html>
<head>
<script type="text/javascript">
var xmlhttp;

function getTime()
{
  // create the xmlhttp object
  xmlhttp=GetXmlHttpObject();

  if (xmlhttp==null)
  {
    alert ("Your browser doesn't support AJAX or you are blocking Javascript");
    return;
  }

  // define a variable that indicates which backend PHP script we want to run
  var url="time.php";
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function stateChanged()
{
  if (xmlhttp.readyState==4)
  {
    // modify the HTML inside of the "myResponse" div tag
    document.getElementById("myResponse").innerHTML=xmlhttp.responseText;
  }
}


function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}
</script>

</head>
<body>
<?php 
$filename="timestamp.tmp";
if(file_exists($filename)){
$fileHandle=fopen($filename,"r");
$timestamp=fread($fileHandle, filesize($filename));
}
else{
$timestamp= "Script Hasn't Run";
}


$f=fwrite($fileHandle, $timestamp);

fclose($fileHandle);

?>

<script type='text/javascript'>
function runApp(which) {
  WshShell = new ActiveXObject("WScript.Shell");
  WshShell.Run (which,1,false);
}
</script>

<input type="button" value="Run Script on My Computer" style="height: 25px; width: 175px; float:left;" onClick="getTime();">

<div style="float:left;" id="myResponse"><?php echo date("m-d-y h:i:s", $timestamp);?></div>

</body>
</html>

This is another file called 'time.php'
Code:
<?php 
$filename="timestamp.tmp";

if(file_exists($filename)){

$fileHandle=fopen($filename,"r");
$timestamp=fread($fileHandle, filesize($filename));

if(canRun($timestamp,1)){

$timestamp=time();
$fileHandle=fopen($filename,"w+");

echo '<script type="text/javascript"> runApp("V:\hi.vbs"); </script>';

echo date("m-d-y h:i:s", $timestamp);
} else {
echo 'Next Run time is ', date("m-d-y h:i:s", $timestamp+(50*1));
}
}
else {

$timestamp=time();
$fileHandle=fopen($filename,"w+");

}


$f=fwrite($fileHandle, $timestamp);

fclose($fileHandle);




function canRun($timestamp, $diff)
{

// Lets turn hours into seconds
$diff = $diff*50;

// When can the user post next?
$nextrun = $timestamp+$diff;

// What time is it now?
$timenow = time();

fclose($fileHandle);
// Is the time now greater than the
// next available post time?
if ($timenow > $nextrun) {
return true;
} else {
return false;
}

}
?>

Notes:
I have tested the script by putting
Code:
<script type='text/javascript'>
runApp("V:\hi.vbs");
function runApp(which) {
  WshShell = new ActiveXObject("WScript.Shell");
  WshShell.Run (which,1,false);
}
</script>
in the index.php and it works just fine. I tested this just to make sure I could run the script from the web browser without a problem.

Any help is welcome
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top