Parab
Parab

Reputation: 71

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.

I would like to have such that second form should be always in disable mode.

and Once the first form submit button is pressed and validated second should get activated to enter the data in it.

Is there anything in PHP which can help me on this

Upvotes: 1

Views: 1030

Answers (5)

Roger
Roger

Reputation: 1693

<?php
    if(isset($_POST['next'])) {
        if($_POST['name']!="") {
            $disabled = "";
            $val = $_POST['name'];
        } else {
            $disabled = " disabled='disabled'";
            $val="";
        }
    } else {
        $disabled = " disabled='disabled'";
        $val="";
    }

?>

<html>
    <head>
    <title></title>
    </head>
    <body>
        <form id="frm1" name="frm1" method="POST" action="">
            <label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
            <input type="submit" name="next" id="next_frm" value="Next"/>
        </form>
        <form name="frm2" id="frm2" method="POST" action="">
            <label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
            <input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
        </form>
    </body>
</html>

This is somewhat you were looking for ,I hope

Upvotes: 0

Php Geek
Php Geek

Reputation: 1107

Thats simple just use if else condition.

// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed     
if(isset($_POST['submit']))
{
    //Display your form 2.
}

else
{
     //Display your form1.
 }

Upvotes: 0

Moseleyi
Moseleyi

Reputation: 2859

You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.

So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.

More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side

So in reality you will have 3 forms, but one would be "fake"

Upvotes: 0

BenM
BenM

Reputation: 53198

The logic below should help you out as a starting point:

<form method="post">
    <input type="text" name="name" />
    <input type="submit" name="form1" value="Proceed" />
</form>

<form method="post">
    <input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
    <input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>

Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.

Upvotes: 1

Andrej  Bestuzhev
Andrej Bestuzhev

Reputation: 674

You have 2 ways:

1) send validation of first form using ajax, and, if you receive 'true', enable second form.

2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;

Upvotes: 2

Related Questions