3

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:

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

Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.

Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.

Is this possible or am i trying do to the impossible?

the forms are as follows;

form 1:

<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
 <tr>
 <td>
 <input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
 <input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
 <input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
 <hr />
 </td>
 </tr>
 <tr>
 <td>
 betalingswijze
 <input type="radio" name="betalingswijze" value="paypal"> Paypal
 <input type="radio" name="betalingswijze" value="mastercard"> Mastercard
 <input type="radio" name="betalingswijze" value="visa"> Visa
 <hr />
 </td>
 <tr>
 <td>
    <img src="toshiba.jpg" alt=" " />
 </td>
 </tr>
 <tr>
 <td>
    Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
    <input type="hidden" name="toshibaproduct" value="001" />
    <input type="hidden" name="toshibamerk" value="toshiba" />
    <input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
    Operating system <select name="toshibaos" value="Toshiba">
    <option value="xp">Windows XP</option>
    <option value="vista">Windows Vista</option>
    <option value="linux">Linux</option>
    </select>
    Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
    <input type="hidden" name="toshibaprijs" value="999.99" />

    <input type="image" src="bestel.jpg" border=0 value="bestellen" />
    <hr />

 <tr>
 <td>
    <img src="acer.jpg" alt=" " />
 </td>
 </tr>
 <tr>
 <td>
    Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>    
    <input type="hidden" name="acerproduct" value="002" />
    <input type="hidden" name="acermerk" value="acer" />
    <input type="hidden" name="acermodel" value="Aspire 5735Z" />
    Operating system <select name="aceros" value="Acer">
    <option value="xp">Windows XP</option>
    <option value="vista">Windows Vista</option>
    <option value="linux">Linux</option>
    </select>
    Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
    <input type="hidden" name="acerprijs" value="529.99" />

    <input type="image" src="bestel.jpg" border=0 value="bestellen" />
    <hr />
    </td><!--Shopping Cart End-->
</tr>
</form>

Form 2

<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
    <b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
    <input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
    <input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
    <input type="text size="34" name="woonplaats">
    Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
    <input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>

Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.

Greetings,

Lennart

3
  • Yes, it is possible. Can you add the php code that manage the form submission? Commented Dec 20, 2015 at 13:50
  • Thank you for your response. I've editted hem in. Commented Dec 20, 2015 at 14:01
  • you only need one form and filter the data. Commented Dec 20, 2015 at 14:36

3 Answers 3

6

The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters. If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.

Consider these simple forms:

<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="form" value="A">
    <button type="submit">Form A</button>
</form>

<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="form" value="B">
    <button type="submit">Form B</button>
</form>

To manage the different submission, you had to check the value of the hidden field:

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

    switch ($_POST['form']) {
        case "A":
            echo "submitted A";
            break;

        case "B":
            echo "submitted B";
            break;

        default:
            echo "What are you doing?";
    } 
} 

You can't submit two separate forms at the same time, because each submission represent a different request to the server.

You may merge manually the fields of the two forms, or use Javascript to do this for you. Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.

As you caan see here you can do simply via jQuery:

var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);

Where url is the action params of the form

Sign up to request clarification or add additional context in comments.

4 Comments

No need to check for isset($_POST), because it's a SUPER-global, and will always be set :-)
Thank you! But will this post both forms? looks like it will only post "A" or "B". I need both forms to be posted at the same time. Or must i then join them into one form (which, now that i say it, sounds logic). thing is my studybook requires me to use 2 forms and output them both at once on the same page.
Forms are still separated modules. If you want to post both, you need to merge fields in a single form (with a single submit button). ...or you can create a javascript function that merges the fields of the two forms and performs a single submission.
Thanks Lipsyor! i will make it a single form. i don't have enough experience with javascript to merge both fields.
0

Your form should be like this.
First form

 <form method="post" >
      ... 
   <input type="hidden" name="form1submission" value="yes" >
    <input type="submit" name="submit" value="Submit" >
  </form>

Second form

  <form method="post" >
      ... 
   <input type="hidden" name="form2submission" value="yes" >
    <input type="submit" name="submit" value="Submit" >
  </form>

And your php for first form.

  if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
        // first form code. 
   }

And second form php code.

  if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
        // second form code. 
   }

That's it.

2 Comments

Thank you for your response! will this post both forms, or just one of them?
This will shop one form at a time. Submitting both forms together is not possible without js. If you need to submit both forms together us jQuery or raw js functions to submit both together.
0

DUPLICATE POST

Yes you can!

First, add the proper action to all forms, secondly, give them an unique name

HTML Side

Don't forget to add the http method what you want (GET or POST)

<form method="post">
  <input type="hidden" name="orderform" />
  <!-- rest of form goes here -->
</form>

<form method="post">
  <input type="hidden" name="klant_gegevens" />
  <!-- rest of form goes here -->
</form>

Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"

Note:

Avoid using space for field names, it can make some problem to match them...

PHP Side

getting input values, by filtering on received form name

if (isset($_POST["orderform"])) {
    // The first form was submitted
}

if (isset($_POST["klant_gegevens"])) {
    // The second form was submitted
}

Note:

Use print_r() or var_dump(), to debug the content of exchanged values

5 Comments

Checking if a form-name is not empty won't work. It'd be better to name the submit-names and check for that (or other specific values to the form). Also, a note on usage of $_SERVER['PHP_SELF'];, read "Big Note on PHP Form Security" on w3schools.
You are good, but it's a reflex. Testing one or some specific values are not sufficient at this step (same as whole field names - too long). At this time, the only thing needed is know if a form has been submited. Conditional come after inside that. that why the condition's scope are empty ! (i know for php_self, i hope his exercise not impose that)
My point is that you can't check for <form name="something" in PHP, it's not something that's posted over $_POST. Values inside a form is submitted, and you can check for those names.
vote minus please, i can't edit that (sry 10h of stackoverflow... time to bed :D)
hmmm, i've improve, but i suggest the deletion, cause of duplicate, that why i talking about to vote... to avoiding beginners reading erroneous code!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.