Click to See Complete Forum and Search --> : saving variables


davey
12-11-2003, 09:42 PM
i have never used php in my life but ive done some basic reading so i sorta understand a little
well anyway i need a way to store these javascript variables

var secondsToWait=5;
var loses=0;
var wins=0;
var money=10000;
var global=50;

to a mysql database
i got a database at http://www.freesql.org/
and the address is
www.freesql.org
port 3306
as user davey
database dbzbattles

any help you could give me will be greatly appreciated

pyro
12-11-2003, 10:02 PM
There are only a few ways to move JavaScript variables to PHP. You will want to use one of these two:

1 - Enter the values into a form, and submit the form.

2 - Attach the values to a query string, and parse them off on the resulting page.

Once you've got you JavaScript variables into PHP variables, you just connect to the database, and run an INSERT sql statement.

davey
12-11-2003, 10:08 PM
do you think you could explain how to do that or show me where to find out how

pyro
12-11-2003, 10:09 PM
Which part?

davey
12-11-2003, 10:15 PM
all of it? (im very new to php)

pyro
12-11-2003, 10:27 PM
Umm.... ok.... (all untested)

<script type="text/javascript">
foo = "test 1";
bar = "test 2";
onload = function() {
document.forms[0].foo.value = foo;
document.forms[0].bar.value = bar;
}
</script>
</head>
<body>
<form action="sompage.php" method="post">
<p><input type="text" name="foo"><br>
<input type="text" name="bar"><br>
<input type="submit" name="submit" value="submit"></p>
</form>

and somepage.php:

<?PHP
mysql_connect('localhost','username','password');
mysql_select_db('database_name');
$foo = htmlspecialchars(stripslashes($_POST['foo']));
$var = htmlspecialchars(stripslashes($_POST['bar']));
$sql = "INSERT INTO `tablename` (`column_one`, `column_two`) VALUES ('$foo', '$bar')";
mysql_query($sql);
?>