0

I am looking to have the results of a query to be posted on the same HTML page that data is entered into. I believe that this can be done with the isset() command, but I don't truly understand what it does and what is telling my page to go to a new php url to give me my table of data.

<html>
<body>
<form action="results.php" method="post">
<table border="0">
<td align="center"><head>Orders</head>
</tr>
<tr>
<td>Enter order number:</td>
<td align="center"><input type="text" name="enter" size="3" maxlength="3"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

This is my current code. Does the

<form action="results.php" method="post">

Line tell the code to post to a new page? Does

if(isset($_POST)) 

go somewhere in the code in my html file or in my results.php file?

user2105982
  • 187
  • 2
  • 6
  • 19

3 Answers3

1

If your form is on index.php page than first thing to do is to change action="results.php" to action="index.php" so you are not redirected to result.php page. Than you can do something like this:

<html>
<body>
<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo "Order number - " . $_POST['enter'];
    }
?>
<form action="index.php" method="post">
<table border="0">
<td align="center"><head>Orders</head>
</tr>
<tr>
<td>Enter order number:</td>
<td align="center"><input type="text" name="enter" size="3" maxlength="3"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

And if you want also fill form fields with entered value than you could do this:

<html>
<body>
<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo "Order number - " . $_POST['enter'];
    }
?>
<form action="index.php" method="post">
<table border="0">
<td align="center"><head>Orders</head>
</tr>
<tr>
<td>Enter order number:</td>
<td align="center"><input type="text" name="enter" size="3" maxlength="3" value="<?php echo isset($_POST['enter'])?$_POST['enter']:'';?>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
muktupavels
  • 348
  • 4
  • 14
0

If you want the same file to show the results,

  1. you have to rename the .html of the current file to .php
  2. use <form method="post" action="<?php echo $PHP_SELF;?>"> instead of <form action="results.php" method="post">
  3. now you add if(isset($_POST)) somewhere in the same file

you can add html code as usual to a .php file but you cannot add php code to a .html file

Anubhav
  • 7,138
  • 5
  • 21
  • 33
0

you can use:

if(isset($_POST['enter']))

also if you want to execute the output when "enter" field doesn't have a blank value, then you can use :

if(isset($_POST['enter']) && $_POST['enter']!="")

also keep the form action value to the same page like this:

<form action="" method="post">

OR

<form action="<?php print($_SERVER['PHP_SELF']); ?>" method="post">
Pranay Bhardwaj
  • 1,059
  • 8
  • 9