Jump to content


Photo

Help with Form


  • Please log in to reply
4 replies to this topic

#1 chris_davidsonuk

chris_davidsonuk
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 01:11 AM

On one of my pages I have 2 forms. I use  if ($_POST) { to activate the form. The problem is that both forms submit when either of the forms are submitted. Is there any way I can make it so that each form is triggered seperately? You can see what I mean at: http://www.wemustdes...com/contact.php

 <?php

        if ($_POST)
        {
            $contact_name = $_POST['contact_name'];
            $contact_mail = $_POST['contact_mail'];
			$contact_phone = $_POST['contact_phone'];
			$contact_enquiry = $_POST['contact_enquiry'];
          


            $message = "Message from $contact_name \n\nE-Mail:$contact_mail\n\n$contact_phone \n\nTelephone:\n\n$contact_phone\n\nEnquiry:\n\n$contact_enquiry";

            
            mail("chris@emustdesign.com", "Contact Form Submitted", $message);
            
            echo "
<fieldset>
<legend>Enquiry Form Submitted</legend>
<br />
<span class='required'>Thankyou for your enquiry. I will get back to you as soon as possible.</span>
<br />
- Chris Davidson
<br />
<br />
</fieldset>"
;
			
        }
        else
        {
   $ledgend = "Enquiry Form <span class= 'required'>(Required *)</span> "; 
		       
	 ?>
	 
  <form name="" method="post" action="<?= $PHP_SELF ?>">
  <fieldset>
  <legend><? echo "$ledgend" ?></legend>
<br />

<label for="contact_name">Name <span class= "required">*</span></label>
<input type="text" value="" name="contact_name"  id="contact_name"/>
<br />

<label for="contact_mail">Email Address <span class= "required">*</span></label>
<input type="text"  value="" name="contact_mail"  id="contact_mail"/>
<br />

<label for="contact_phone">Telephone:</label>
<input type="text"  value="" name="contact_phone"  id="contact_phone"/><br />

<label for="contact_enquiry">Enquiry <span class= "required">*</span></label>
<textarea name="contact_enquiry"  id="contact_enquiry"></textarea><br />
<br />
    <input type="submit" title="Submit callback request" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" name="Submit" value="Submit" />
	</fieldset>
  </form>
           <?php

        }

        ?>

---------------------------------------
[a href="http://www.studioeight.net" target="_blank"]www.studioeight.net[/a]

#2 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 22 July 2006 - 01:18 AM

Try changing if ($_POST)
{

to

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

#3 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 01:20 AM

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


The reson that this works is because instead of checking for something that should always be there, you are checking if the submit button has been pressed.

edit: what i mean by this, to be more clear is,
$_POST is there. Period.
$_POST['Submit'] is only there if the submit button has been pressed.
Thus, checking for $_POST is useless.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#4 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 22 July 2006 - 01:27 AM

Ok then

if(isset($_POST['submit'] && !empty($_POST['fieldname']))
{

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 July 2006 - 03:28 PM

Another way to do this is to give the submit buttons two different names.

This short script illustrates this:
<?php
if (isset($_POST['submit']) || isset($_POST['submit1'])) echo '<pre>' . print_r($_POST,true) . '</pre>';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>two forms</title>
</head>
<body>
<form method="post">
<input type="hidden" name="inform" value="1">
<input type="submit" name="submit" value="Send Form 1">
</form>
<br>
<form method="post">
<input type="hidden" name="inform" value="2">
<input type="submit" name="submit" value="Send Form 2">
</form>
</body>
</html>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users