I am getting confused as I read various posts about the typical task I am trying to perform, no doubt countless others have done it before. So at this point in 2013 what is the best practice solution to achieve the following:
Thanks in advance!
code:
<form action="nextfile.php" method="POST">
<input type="text" value="" name="y1" />
<input type="text" value="" name="y2" />
<input type="submit" value="calc me" name="submit" />
</form>
Then in nextfile.php you need get values with $_POST
and if you want them to be saved use $_SESSION
For example
<?php
session_start();
if(isset($_POST['y1']) && isset($_POST['y2']))
{
$_SESSION['res'] = (int)$_POST['y1'] * (int)$_POST['y2'];
}
The code above will perform calculation on two inputs with name y1 and y2 and save them in session.
If you want AJAX then you need to visit this page and see examples
You should think of JavaScript solution because it will fit your needs and no server code is needed.
The simplest way is to submit the form to the same page and repopulate the input fields:
// calc.php
<?php
if (isset($_POST['foo'])) {
echo 'Result: ', $_POST['foo'] + 1;
}
?>
<form action="calc.php" ...>
<input name="foo" value="<?php if (isset($_POST['foo'])) echo htmlspecialchars($_POST['foo']); ?>">
...
</form>
The more modern version would be to submit the calculations via AJAX and populate the result via Javascript without reloading the page.