<?php 

$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbName = "test";

$conn = mysql_connect($dbHost, $dbUser, $dbPass);

if(!$conn){
    die(mysql_error());
}

mysql_select_db($dbName, $conn) or die(mysql_error());

    if(isset($_POST['submit'])){

        $field = $_POST['field'];

    }

    if(isset($_POST['submit2'])){

        $field2 = $_POST['field2'];

    }

    if(isset($_POST['submit3'])){

        $field3 = $_POST['field3'];

    }


?>
<?php
    if(!isset($_POST['submit']) || isset($_POST['submit']) != "true"){
?>
<form id="create" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

 <label>field :</label>
 <input type="text" name="field" />
 <input type="submit" name="submit" value="submit">

</form>
<?php 
    }
    elseif(!isset($_POST['submit2']) || isset($_POST['submit2']) != "true"){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

 <label>field 2 :</label>
 <input type="text" name="field2" />
 <input type="submit" name="submit2" value="submit">

</form>
<?php 
    }
    elseif(!isset($_POST['submit3']) || isset($_POST['submit3']) != "true"){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

 <label>field 3 :</label>
 <input type="text" name="field3" />
 <input type="submit" name="submit3" value="submit">

</form>
<?php } ?>

Recommended Answers

All 4 Replies

Member Avatar for diafol

Are you building a form wizard?
Looks a bit odd.
You have 'multipart' set but no file upload fields.
You assign your post vars to $field etc, but then use the original $_POST vars to conditionally display your forms.

<?php echo $_SERVER['PHP_SELF']; ?>

You could probably do without all of this. The action default is to send to itself.

thanks for your reply, yes, i want a build a form wizard, can you submit a code related to my idea, i am beginner in php.

Member Avatar for diafol

can you submit a code related to my idea,

No sorry - that's your job. Search Google for "php form wizard" - you should get plenty of hits. There are right ways and wrong ways. I'll just outline a few ideas here though.

You should send all form data to a separate form handler file which then redirects to a form page, using header(). Data can be transferred via url querystring (using GET to pick up the data) or conveniently hidden away using SESSION variables.

Try to separate your php and html as much as possible. One approach could be:

session_start();

if(isset($_SESSION['next_form'] && in_array($_SESSION['next_form'], array(2,3)){
    $form_no = $_SESSION['next_form'];
    unset($_SESSION['next_form']);
}elseif{
    $form_no = 1;
}

if(isset($_SESSION['data_error'])){
    $error_msg = $_SESSION['data_error'];
    unset($_SESSION['data_error']);
}


//later on
if(isset($error_msg))echo "<p class='error'>$error_msg</p>"; 
include("forms/form_{$form_no}.html");

This will load form_1.html or form_2.html or form_3.html, depending on the value of the session variable or whether it is set at all.

e.g. form_1.html

<form action="includes/formhandler.php" method="post">
    ...
    <input type="hidden" name="form_no" value="1" />
    ...
</form>

The form handler accepts the data, validates it and may process it (DB etc), then creates some SESSION variables to pass back to the form page.

session_start();

if(isset($_POST['form_no'])){
    $form_no = (int) $_POST['form_no'];
    switch($form_no){
        case 1:
        //... etc...
    }

}else{
    $_SESSION['form_error'] = "A form was not sent, start again";
}
header("Location: ../form.php");

so much thanks for your reply and your ideas...i will try to figure this out...thanks again...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.