Reputation: 363
I'm learning PHP and working on forms right now. I have an assignment to create a form, one GET and one POST on the same page and have the results posted on the same page below each form.
I created an if/else statement but maybe I'm going about it incorrectly.
<div>
<h1>POST Method Form</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
First Name:<br>
<input type="text" name="firstname"><br>
Last Name:<br>
<input type="text" name="lastname"><br>
E-mail: <input type="text" name="email"><br>
Database Utilized: <br>
<input type="checkbox" name="dba" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba" value="Oracle">Oracle<br>
<input type="checkbox" name="dba" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="postsubmit" value="Submit">
</form>
<br>
<h1>Database Consulting POST Form Results</h1>
<p>
<?php
if (isset($_POST['postsubmit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['laststname'];
$email = $_POST['email'];
$dba = $_POST['dba'];
echo $firstname; <br>
echo $lastname; <br>
echo $email; <br>
echo $dba; <br>
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
<br>
<h1>GET Method Form</h1>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
First name:<br>
<input type="text" name="firstname_get"><br>
Last name:<br>
<input type="text" name="lastname_get"><br>
E-mail: <input type="text" name="email_get"><br>
Database Utilized<br>
<input type="checkbox" name="dba_get" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba_get" value="Oracle">Oracle<br>
<input type="checkbox" name="dba_get" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="getsubmit" value="Submit">
</form>
<br>
<br>
<h1>Database Consulting GET Form Results</h1>
<p>
<?php
if (isset($_GET['getsubmit'])) {
$firstname_get = $_GET['firstname'];
$lastname_get = $_GET['laststname'];
$email_get = $_GET['email'];
$dba_get = $_GET['dba'];
echo $firstname_get; <br>
echo $lastname_get; <br>
echo $email_get; <br>
echo $dba_get; <br>
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
</div>
Upvotes: 1
Views: 2251
Reputation: 16122
You misspelled $_GET['lastname']
as $_GET['laststname']
, change your action to action=""
and include your <br>
in your echo statement like echo $firstname .'<br>';
instead of echo $firstname; <br>
<h1>POST Method Form</h1>
<form method="POST" action="" >
First Name:<br>
<input type="text" name="firstname"><br>
Last Name:<br>
<input type="text" name="lastname"><br>
E-mail: <input type="text" name="email"><br>
Database Utilized: <br>
<input type="checkbox" name="dba[]" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba[]" value="Oracle">Oracle<br>
<input type="checkbox" name="dba[]" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="postsubmit" value="Submit">
</form>
<br>
<h1>Database Consulting POST Form Results</h1>
<p>
<?php
if (isset($_POST['postsubmit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$dba = $_POST['dba'];
echo $firstname .'<br>';
echo $lastname .'<br>';
echo $email .'<br>';
foreach ($dba as $database) {
echo $database .'<br>';
}
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
Upvotes: 3
Reputation: 6334
first of all, I would suggest having a hidden field called action like:
<input type="hidden" name="action" value="submituserinfo" />
then put your php logic in the top not the bottom, process before the page shows! Your PHP_SELF deal is good. But your logic is off. The correct logic should be:
if(isset($_REQUEST['action']) && $_REQUEST['action']=='submituserinfo'){
//analyze the data
if($errors){
//set a var to show errors below
}else{
//enter the data, whatever
}
}
That should get you started in the right direction. If you put the coding at the head of the document, you can better handle output or even use header() to redirect based on success or error.
Upvotes: -1