1

I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?

NB. I hope this makes much sense?

html code

<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

php script

<?php 
     if (isset($_POST['submitForm'])) { 

     echo('<script>alert("Form Submitted")</script>');

     }
?>
4
  • 1
    What do you means with "it submits all the forms" ? The three forms that i see are the same except their name which is not send to php. And your test in PHP check in $_POST array instead of $_GET.
    – Leto
    Oct 21, 2011 at 12:41
  • I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!
    – Jay Smoke
    Oct 21, 2011 at 12:49
  • tried to answer. I HOPE I got your vague question right at last Oct 21, 2011 at 12:57
  • Without supplying a name attribute to each input nothing is submitted let alone all three forms. As a result, I can't reproduce your claim that all three forms are being submitted with the code you provided.
    – Herbert
    Oct 21, 2011 at 12:59

5 Answers 5

7

when I click on submit for any particular form, it submits all the forms.

this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)

however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.

Also note that none of your text fields being sent to the server as they have no names.

I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!

well, it seems you want to ask how to distinguish these forms.

add a hidden field into each

<input type="hidden" name="step" value="1" />

and then in PHP

if ($_POST['step'] == 1) {
  //first form
}
if ($_POST['step'] == 2) {
  //second
}
4
  • 1
    am not using html to submit the form col. If you read my post properly and went through my codes you will realize I am using PHP to process the form...so when a form is submitted, php process the form but in doing this, it processes all the other forms as well! Thats what I mean
    – Jay Smoke
    Oct 21, 2011 at 12:46
  • "Submit one form out of many using php" is the exact title of your question, dude. Blame yourself. As for your forms - post here a relevant reproduceable code to prove your words. Oct 21, 2011 at 12:49
  • Hello Col...would this mean I would have to manually add the steps for each form? i.e. if I have 10 forms, I will have to add the ifs statements 10 times? if so, then it's gonna be a problem since the forms are generated dynamically from the db!
    – Jay Smoke
    Oct 21, 2011 at 15:56
  • 1
    @Jay if your forms being generated dynamically, then you can add any fiels dynamically as well. Care to learn programming a bit? Oct 22, 2011 at 12:44
3

This submits one form of many to php. Copy, paste, test, and study.

<?php
     if (isset($_POST['submitForm'])) { 

    print_r($_POST);

     }

?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:

Array
(
    [A] => e
    [B] => f
    [C] => g
    [D] => h
    [submitForm] => Submit Form
)
3
  • how is this different from mine @Herbert?
    – Jay Smoke
    Oct 21, 2011 at 15:48
  • I just fixed the problem???? It still doesn't make sense but I moved the php script to the top of all the form and now it works? I guess the script was also been replicated with each form since each form had a copy of the script! I dunno but it's working now! Thanx
    – Jay Smoke
    Oct 21, 2011 at 16:21
  • 1
    Sorry @Jay. I didn't see you comments until just now. I think you answered your own question there. The main difference is that all the input fields have names. Apparently, the second difference (not evident by your original question) is that the code is at the top. Anyway, I'm glad you got it working.
    – Herbert
    Oct 21, 2011 at 16:47
2

@Jay

Actually its not hard.

Once you supply form names, your work is done. the DOM does the rest.

write one php block to do your functions (create/update/retrieve/delete) Whichever button is clicked, by default it submits only the elements enclosed together with it.


if(!empty($_POST)){
    if(isset($_POST['submit'])){
        print "<pre>";
        var_dump($_POST); // write your code here as you would 
        print "<pre>";
    }
}

try this with your form above.

1

I know this is an old post but here's how I solve this very problem. All you need to do is make sure the submit buttons in each form have different names. Eg:

<form action="" name="form1" method="post">
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="Submit" value="Submit Form" name="submitForm1" />
</form>

<form action="" name="form2" method="post">
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="Submit" value="Submit Form" name="submitForm2" />
</form>

<form action="" name="form3" method="post">
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="text" value="" />
    <input type="Submit" value="Submit Form" name="submitForm3" />
</form>

Then, you simply check which form's submit button was pressed.

<?php 
if (isset($_POST['submitForm1'])) { 
  echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) { 
  echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) { 
  echo('<script>alert("Form 3 Submitted")</script>');
}
?>
0

If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this. Here, I used while($n<10) for 10 dynamic forms. You can also use tag as below if you need separate form names.

<form action="" name="form<?=$n?>" method="post">

This will create separate form names such as form1, form2, etc but not necessary here.

    <?php
if (isset($_POST['submitForm'])) { 
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
}
$n=0;
while($n<10) {
    $n++;
    ?>
    <form action="" name="form1" method="post">
      <input type="text" value="" name="A" />
      <input type="text" value="" name="B" />
      <input type="text" value="" name="C" />
      <input type="text" value="" name="D" />
      <input type="Submit" value="Submit Form" name="submitForm" />
    </form>
    <?php
}
?>

Sample page with output when I click row 5..

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.