I want to code a web page that will work like this:
- Input a number from the user,
- Use the number to display some information,
- Provide the user with the option to use that number and information for some purpose,
- If so asked, use the number and information provided as directed.
I am having trouble with the last step - after the user selects the option to use the number, the number is gone - it is no longer set. I tried making the number static, which is how I would do this in languages other than PHP. I also tried something called the $GLOBALS array, but that did not help.
Below is source code illustrating the problem.
There must be a standard or prototypical way of dealing with this interaction in PHP, but I am not savvey to it. Any advice or correction would be appreciated!
<h2>Give Me a Number</h2>
<form method="post">
<label for="MyNumber">Number Please</label>
<input type="text" id="MyNumber" name="MyNumber">
<input type="submit" name="view" value="View Number">
</form>
<?php
static $MyNumber;
if (isset($_POST['view']))
{
try
{
// Get the Number from the post variable for binding
$MyNumber = $_POST['MyNumber'];
echo "Your Number is ", $MyNumber, " on Line 24";
echo "<br>", "Your GLOBAL Number is ", $GLOBALS['MyNumber'], " on Line 25";
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
if (isset($_POST['view']))
{
if ($MyNumber > 0 )
{ ?>
<h2>Again, Your Number is <?php echo $MyNumber, " on Line 40"; ?> </h2>
<h2>Again, Your Number is <?php echo $GLOBALS['MyNumber'], " on Line 41"; ?> </h2>
<table>
<thead>
<tr>
<th>My Number</th>
<th>Global Number</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 1; $i <= $MyNumber; $i++)
{ ?>
<tr>
<td><?php echo $MyNumber; ?></td>
<td><?php echo $GLOBALS['MyNumber']; ?></td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<blockquote>No NUMBER !!!! on Line 68</blockquote>
<?php
} ?>
<form method="post">
<input type="submit" name="use" value="Use My Number">
</form>
<?php }?>
<?php
//////////////// USE the NUMBER //////////////////
if (isset($_POST['use']))
{
if (isset($MyNumber) )
{
echo "I can USE (", $MyNumber, ") My Number !!!";
echo "<br>", "and(", $GLOBALS['MyNumber'], ") My GLOBAL Number!";
}
else
{
echo "Hey, what happened to (", $MyNumber, ")? I wanted to use it!";
echo "<br>", "Hey, GLOBAL (", $GLOBALS['MyNumber'], ") is gone also!";
}
}
?>