Jump to content

Recommended Posts

Hi,

 

I have the following form on my page which works fine.

 

 if($_POST){
        $to = 'me@me.com';
        $subject = 'Signup';
        $from_name = $_POST['name'];
    $company = $_POST['company'];
        $from_email = $_POST['email'];
    $telephone = $_POST['telephone'];
       $message = "New Signup Detials" . "\r\n\r\n" . "Name :" .  $_POST['name'] . "\r\n\r\n" . "Email: " .  $_POST['email'] . "\r\n\r\n" . "Telephone: " . $_POST['telephone']";
        $robotest = $_POST['robotest'];
        if($robotest)
            $error = "You are a gutless robot.";
        else{
            if($from_name && $from_email && $message){
                $header = "From: $from_name <$from_email>";
                if(mail($to, $subject, $message, $header))
                    $success = "You are human and your message was sent!";
                else
                    $error = "You are human but there was a problem sending the e-mail.";
            }else
                $error = "All fields are required.";
        }
        if($error)
            echo '<div class="msg error">'.$error.'</div>';
        elseif($success)
            header ("location: contactthanks.php");
    }
  
 

 

I have also have another form on my page with the following:

 

if($_POST){
        $to_s = 'me@me.com';
        $subject_s = 'Support Request';
        $from_name_s = $_POST['name_s'];
    $company_s = $_POST['company_s'];
        $from_email_s = $_POST['email_s'];
    $telephone_s = $_POST['telephone_s'];
       $message_s = "New Signup Detials" . "\r\n\r\n" . "Name :" .  $_POST['name_s'] . "\r\n\r\n" . "Email: " .  $_POST['email_s'] . "\r\n\r\n" . "Telephone: " . $_POST['telephone_s'] . "\r\n\r\n" .  "Comments: " .  $_POST['message_s'];
        $robotest_s = $_POST['robotest_s'];
        if($robotest_s)
            $error = "You are a gutless robot.";
        else{
            if($from_name_s && $from_email_s && $message_s){
                $header_s = "From: $from_name_s <$from_email_s>";
                if(mail($to_s, $subject_s, $message_s, $header_s))
                    $success = "You are human and your message was sent!";
                else
                    $error = "You are human but there was a problem sending the e-mail.";
            }else
                $error = "All fields are required.";
        }
        if($error)
            echo '<div class="msg error">'.$error.'</div>';
        elseif($success)
            header ("location: contactthanks.php");
    }

 

However, I am getting a duplicate "All fields are required" message appear and the form is not submitting. Any ideas how can the code similar for each form?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/296984-two-forms-on-one-page/
Share on other sites

Have you checked to see if $from_name_s, $from_email_s, and $message_s contain expected values? Note that you can use var_dump() to check. For example

$from_name_s = $_POST['name_s'];
var_dump($from_name_s);

If you're still unable to figure out the issue, it may help if you post the form code.

They all seem to have values.

 

This is form code:

 

Form 1:

<form method="post" action="" id="signup">
              <fieldset>
              <label>Name:</label>
              <input name="name" type="text" id="name" />
              <label>Company:</label>
              <input name="company" type="text" id="company" />
              <label>E-mail:</label>
              <input name="email" type="text" id="email" />
              <label>Telephone:</label>
              <input name="telephone" type="text" id="telephone" />
              <!-- The following field is for robots only, invisible to humans: -->
              <p class="robotic" id="pot">
                <label>If you're human leave this blank:</label>
                <input name="robotest" type="text" id="robotest" class="robotest" />
              </p>
              <input type="submit" value="Send Message" class="submit" />
              </fieldset>
            </form>

 

Form 2:

 

            <form method="post" action="">
              <fieldset>
              <label>Name:</label>
              <input name="name_s" type="text" id="name_s" />
              <label>Company:</label>
              <input name="company_s" type="text" id="company_s" />
              <label>E-mail:</label>
              <input name="email_s" type="text" id="email_s" />
              <label>Telephone:</label>
              <input name="telephone_s" type="text" id="telephone_s" />
              <label>Message:</label>
              <textarea name="message_s" id="message_s"></textarea>
              <!-- The following field is for robots only, invisible to humans: -->
              <p class="robotic" id="pot">
                <label>If you're human leave this blank:</label>
                <input name="robotest_s" type="text" id="robotest_s" class="robotest" />
              </p>
              <input type="submit" value="Send Message" class="submit" />
              </fieldset>
            </form>
 

Ah, I wonder if the error is coming from the code that's supposed to process the first form. Try adding some debugging statements after the "All fields are required" string is saved to $error in both blocks of code. For example

}else
    $error = "All fields are required.";
    echo '<div>form 1 submitted</div>';
 
And
}else
    $error = "All fields are required.";
    echo '<div>form 2 submitted</div>';

As far as I can tell, the script that processes the form submissions doesn't check to see which form is submitted before executing the corresponding code.

 

each different form processing code needs to test for a unique field name in the form, such as a hidden field, so that only the form processing code that corresponds to the submitted form will run.

// post method form processing code
if($_SERVER['REQUEST_METHOD'] == 'POST'){

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

        // all the form processing code for form 1
    }

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

        // all the form processing code for form 2
    }

    // any other form processing code

    // if there are no errors, you should do a header() redirect to the exact same url of this page so that the browser won't try to resubmit the form data
    // to pass any 'success'/'thank you' message to be displayed when the page gets displayed after this redirect, pass it/them in $_SESSION variables
    // you should also exit;/die; after any header redirect to prevent the remainder of the code on the page from running

    // if there are errors, don't redirect and let the code on this page display the error messages and redisplay the form

} // end of post method form processing code

// any get request - display code starts here...

the above code should set error messages in an array - $errors['form1_name'][] = 'some message'; or $errors['form2_name'][] = 'some message'; at any point to detect if there are errors, you can just test if the $errors array is empty or not. to display the errors for any form, just test for and loop over the $errors['form1_name'] array elements.

Edited by mac_gyver

In order to process multiple forms on a single page you need to tell php how to distinguish between the different postings.  In your case it's pretty easy.

1. You need to give the submit buttons an actual name="" attribute and make them different for each form.

2. Don't ever just use if($_POST).  $_POST is always present cause it's a php superglobal.  So everytime the page is loaded, this code is run.

3. Use

if(isset($_POST['submit-button-name-for-the-form-posting']))

Obviously change the "submit-button-name-for-the-form-posting" to the actual name of the submit button

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.