Help with a form using $_POST and $PHP_SELF
I will try to describe what I want to do as best as possible. I want to create a webpage using HTML and PHP. The webpage will use several forms to receive inputs from a user to be stored for later use. I only want a form to appear to the user after the previous form has been submitted. For instance, I have 3 forms:
- What is your first name? _ [SUBMIT]
- What is your middle name? _ [SUBMIT]
- What is your last name? _ [SUBMIT]
I only want the 2nd question to show up after the 1st submit is set. And I only want the 3rd question to show up after both the 1st and 2nd submits are set.
I want the webpage to look like this as the user enters and submits his information:
How the webpage looks at first:
- What is your first name? Billy [SUBMIT]
How the w开发者_如何学运维ebpage looks after user presses 1st submit:
- What is your first name? Billy [SUBMIT]
- What is your middle name? Joe [SUBMIT]
How the webpage looks after user presses second submit:
- What is your first name? Billy [SUBMIT]
- What is your middle name? Joe [SUBMIT]
- What is your last name? Smith [SUBMIT]
I was hoping to use accomplish this using $PHP_SELF in the action fields of my forms and by also using the $_POST method. Do you have any suggestions? Thank you very much.
Edit: Clarity
This Should be it
<?php
session_start();
if($_POST['btnOne'])
$_SESSION['ShowSecond'] = true;
if($_POST['btnTwo'])
$_SESSION['ShowThird'] = true;
?>
<form name="formOne">
...
<input type="submit" name="bntOne" value="One">
</form>
<?php if(isset($_SESSION['ShowSecond'])) { ?>
<form name="formTwo">
...
<input type="submit" name="bntTwo" value="Two">
</form>
<?php }
if(isset($_SESSION['ShowThird']))
{
?>
<form name="formThree">
...
<input type="submit" name="btnThree" value="Three">
</form>
<?php } ?>
?>
just make that session_start() is executed as first line in your php files
You can use session as suggested by Senad Meškin or create hidden input fields in forms to pass data across multiple submissions. Play around with this code and figure out how it goes.
<form name="myform" method="post" action="">
<?php if(!isset($_POST['firstname']))
{?>
First Name?
<input type="text" name="firstname" value="">
<?php }
elseif(!isset($_POST['middlename']))
{?>
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname'] ?>">
First Name? <?php echo $_POST['firstname'] ?><br>
Middle Name?
<input type="text" name="middlename" value="">
<?php }
elseif(!isset($_POST['lastname']))
{?>
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname'] ?>">
<input type="hidden" name="middlename" value="<?php echo $_POST['middlename'] ?>">
First Name? <?php echo $_POST['firstname'] ?><br>
Middle Name? <?php echo $_POST['middlename'] ?><br>
Last Name?
<input type="text" name="lastname" value="">
<?php }else{ ?>
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname'] ?>">
<input type="hidden" name="middlename" value="<?php echo $_POST['middlename'] ?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname'] ?>">
First Name? <?php echo $_POST['firstname'] ?><br>
Middle Name? <?php echo $_POST['middlename'] ?><br>
Last Name? <?php echo $_POST['lastname'] ?><br>
<?php } ?>
<input type="submit" name="submitbtn" value="Submit">
</form>
Ofcourse this code may be organised better into functions as you can see there is a repeating pattern.
精彩评论