Is it possible to pass info(variables) between php and javascript code?

  • Thread starter Thread starter hughmanwho
  • Start date Start date
H

hughmanwho

Guest
1) I would like to be able to open up a file and read in a value(I believe needs to be done in php)

2)Then if a certain button is clicked I would like to update that file(php), display a message if certain conditions are met(seems to require javascript).

3) Finally reload the page(have it working in javascript)

So for example I can store a counter in a file, read it in and add to it if a certain button is pressed. If the button is pressed it will display a message saying how many times it's been pressed and update the file.Then it will reload the page.
Any suggestions? Thanks!
 
<?php
//connect your database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("counter") or die(mysql_error());

if(isset($_POST['ok'])) {

$result = mysql_query(" SELECT * from mycounter") ; // your tablename
$row = mysql_fetch_array( $result );
$num = $row['ctr']; // field from your table
$ctr2= $num + 1;

mysql_query("update mycounter set ctr='$ctr2' ");
?>

<SCRIPT language=JavaScript>
<!--
alert("Button has been pressed: <?php echo $ctr2; ?> times");
// End -->
</script>

<?php
}
?>

<form method="POST">
<input type="submit" name="ok" id="ok" value=" Ok ">
</form>
 
Back
Top