Jump to content

Recommended Posts

Hi guys,

At themmoment i have a very simple mail form on my "contact us" page, it's built around 2 pages [b]contactus.php[/b] & [b]contatcsend.php[/b]

Contactus.php simply contains a form as follows

[code]     <form action="contactsend.php" method="POST">
         
              <div align="center">
                <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable">
                  <tr>
                    <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td>
  </tr>
                  <tr>
                    <td><p align="left"><b>Company name:</b></p></td>
                    <td><input name="company" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Name:</b></p></td>
                    <td><input name="name" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Email:</b></p></td>
                    <td><input name="email" type ="text" size="50" maxlength="50" /></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Contact Number:</b></p></td>
                    <td><input name="contact" type ="text" size="50" maxlength="25" /></td>
                  </tr>
                  <tr>
                    <td width="150"><p align="left"><b>Your Messsage:</b></p></td>
                    <td><textarea name="message" cols="50" rows="8"></textarea></td>
                  </tr>
                  <tr>
                    <td class="bottomleft">&nbsp;</td>
                    <td><input name="submit" type="submit" value="Send Message" />                    </td>
                  </tr>
                </table>
                </p>
</div>
    </form>[/code]

as you can see this is just an HTML form and its "action" is contactsend.php

Which containts the following php code

[code] <?

//contact form
$company=$_POST['company'];
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$message=$_POST['message'];

$ip = $_SERVER['REMOTE_ADDR'];

$time = time();
$date_message = date("r", $time);

$subject="New Enquiry";

$email="deleted@mydescretion.co.uk";

//format the message
$msg .= "IP: $ip\n";
$msg .= "$date: $date_message\n";
$msg .= "$subject\n";
$msg .= "Company: $_POST[company]\n";
$msg .= "Name: $_POST[name]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[message]\n";

mail($email,$subject,$msg);
echo("Your message has been sent, We will contact you with a reply as soon as possible.");
?>[/code]

As you can see this script simply formats the message and pulls the IP address / generates the time and date.


I would like to develop this form to do 2 things in the short term.

1) check all the fields have content
2) check the email address is of the form x@x.x

Ive been playing with some ifelse statements that i found..... but im not really sure where to put them or how to make them work.

I'm feeling a bit lost on this to be honest, and have spent too long on it now, i need to get this understood and ready to go. Can anyone help me with this please?
Link to comment
https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/
Share on other sites

Well, you could combine your pages into one file (say contact.php):

[code]
<?php

if(isset($_POST['submit'])){ //has the form been submitted?
  //check all of the inputs
  if(!empty($_POST['company'])){ //if the company field of the form is not empty
      //process the info
  }

  else{
      //company field hasn't been filled -- error
  }
.
.
.

} //end of if(isset(...

else{ //if it hasn't been submitted, display the form
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
.
.
.
<?php
} //end of the else block
?>
[/code]

A script can refer to itself (which is what the echo statement in the form does), so you can have a script both display a form and process it upon submission, with the basic outline I showed above.  I've also shown how to check if an input field is empty or not (only works in textfields, I believe).

For your e-mail structure, you'll probably have to use regular expressions.  I'm not very familiar with PHP's regex functionality, so I'll leave that for someone else to handle. ;)
I think your gonna have to dumb this down a little for me please.

I now have the following

[code] <form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">

         
              <div align="center">
                <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable">
                  <tr>
                    <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td>
  </tr>
                  <tr>
                    <td><p align="left"><b>Company name:</b></p></td>
                    <td><input name="company" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Name:</b></p></td>
                    <td><input name="name" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Email:</b></p></td>
                    <td><input name="email" type ="text" size="50" maxlength="50" /></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Contact Number:</b></p></td>
                    <td><input name="contact" type ="text" size="50" maxlength="25" /></td>
                  </tr>
                  <tr>
                    <td width="150"><p align="left"><b>Your Messsage:</b></p></td>
                    <td><textarea name="message" cols="50" rows="8"></textarea></td>
                  </tr>
                  <tr>
                    <td class="bottomleft">&nbsp;</td>
                    <td><input name="submit" type="submit" value="Send Message" />                    </td>
                  </tr>
                </table>
                </p>
</div>
    </form>

<?

//contact form
$company=$_POST['company'];
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$message=$_POST['message'];

$ip = $_SERVER['REMOTE_ADDR'];

$time = time();
$date_message = date("r", $time);

$subject="New Enquiry";

$email="me@mine.com";

//format the message
$msg .= "IP: $ip\n";
$msg .= "$date: $date_message\n";
$msg .= "$subject\n";
$msg .= "Company: $_POST[company]\n";
$msg .= "Name: $_POST[name]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[message]\n";

mail($email,$subject,$msg);
echo("Your message has been sent, We will contact you with a reply as soon as possible.");
?>
[/code]

All on the same page.......... the form still submits when the button is pressed ... but, on by virtue of the page refreshing.

The form auto submits itself as soon as the page is loaded ......... I'm afraid i still dont understand how to check a field is filled in or not.

Sorry if im being stupid    ??? :'(
Well, right now you have the order backwards.  The most logical thing to do is process the form if it's been submitted.  If it [b]hasn't[/b] been submitted, then you output the form.  I've rewritten it into a form that should work below:

[code]
<?php
$errorMessage = ''; //initialize our error message

if(isset($_POST['submit'])){ //if the form has been submitted...
  if(!empty($_POST['company'])){ //if company isn't empty
      $company = $_POST['company']; //set the local variable to what's been posted
      $comp = TRUE; //set a boolean for future use
  }

  else{ //company IS empty
      $errorMessage .= "Please input your company name!<br />"; //add to our error message
      $comp = FALSE; //set a boolean for future use
  }

  if(!empty($_POST['name'])){
      $name = $_POST['name'];
      $n = TRUE;
  }

  else{
      $errorMessage .= "Please input your name!<br />";
      $n = FALSE;
  }

  if(!empty($_POST['email'])){
      $email = $_POST['email'];
      $e = TRUE;
  }

  else{
      $errorMessage .= "Please input your e-mail address!<br />";
      $e = FALSE;
  }

  if(!empty($_POST['contact'])){
      $contact = $_POST['contact'];
      $cont = TRUE;
  }

  else{
      $errorMessage .= "Please input your contact number!<br />";
      $cont = FALSE;
  }

  if(!empty($_POST['message'])){
      $message = $_POST['message'];
      $mess = TRUE;
  }

  else{
      $errorMessage .= "Please input your message!";
      $mess = FALSE;
  }

  if($comp && $n && $e && $cont && $mess){ //if everything's been filled out correctly, send the mail
      $ip = $_SERVER['REMOTE_ADDR'];
      $time = time();
      $date_message = date("r", $time);
      $subject="New Enquiry";
      $email="me@mine.com";

      //format the message
      $msg .= "IP: $ip\n";
      $msg .= "$date: $date_message\n";
      $msg .= "$subject\n";
      $msg .= "Company: $company\n";
      $msg .= "Name: $name\n";
      $msg .= "Email: $email\n";
      $msg .= "Message: $message\n";

      mail($email,$subject,$msg);
      echo "Your message has been sent.  We will contact you with a reply as soon as possible.";
  }

  else{ //something is wrong, so print our error message
      echo "<span style="color: red;">Could not send e-mail for the following reasons:<br />$errorMessage";
  }

else{ //the form HASN'T been submitted yet
  ?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <div align="center">
                <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable">
                  <tr>
                    <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Company name:</b></p></td>
                    <td><input name="company" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Name:</b></p></td>
                    <td><input name="name" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Email:</b></p></td>
                    <td><input name="email" type ="text" size="50" maxlength="50" /></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Contact Number:</b></p></td>
                    <td><input name="contact" type ="text" size="50" maxlength="25" /></td>
                  </tr>
                  <tr>
                    <td width="150"><p align="left"><b>Your Messsage:</b></p></td>
                    <td><textarea name="message" cols="50" rows="8"></textarea></td>
                  </tr>
                  <tr>
                    <td class="bottomleft">&nbsp;</td>
                    <td><input name="submit" type="submit" value="Send Message" /></td>
                  </tr>
                </table>
                </p>
        </div>
      </form>

  <?php
} //ending bracket for our else-conditional
?>
[/code]
Wow

Just wow ... thanks your amazing .

Ive spent all morning going over it, and im pretty down with the logic behind how it works now....

However i am getting a persistant error that i cant figure out..... can anyone shed some light on what it means

[code]
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in path/path/path/www/contactus.php on line 133[/code]

Line 133 happens to be:

[code]  else{ //something is wrong, so print our error message  [b]132[/b]
      echo "<span style="color: red;">Could not send e-mail for the following reasons:<br />$errorMessage";  [b]133[/b]
  }  [b]134[/b][/code]

Any idea's?
[code]
<?php
$errorMessage = ''; //initialize our error message

if(isset($_POST['submit'])){ //if the form has been submitted...
  if(!empty($_POST['company'])){ //if company isn't empty
      $company = $_POST['company']; //set the local variable to what's been posted
      $comp = TRUE; //set a boolean for future use
  }

  else{ //company IS empty
      $errorMessage .= "Please input your company name!<br />"; //add to our error message
      $comp = FALSE; //set a boolean for future use
  }

  if(!empty($_POST['name'])){
      $name = $_POST['name'];
      $n = TRUE;
  }

  else{
      $errorMessage .= "Please input your name!<br />";
      $n = FALSE;
  }

  if(!empty($_POST['email'])){
      $email = $_POST['email'];
      $e = TRUE;
  }

  else{
      $errorMessage .= "Please input your e-mail address!<br />";
      $e = FALSE;
  }

  if(!empty($_POST['contact'])){
      $contact = $_POST['contact'];
      $cont = TRUE;
  }

  else{
      $errorMessage .= "Please input your contact number!<br />";
      $cont = FALSE;
  }

  if(!empty($_POST['message'])){
      $message = $_POST['message'];
      $mess = TRUE;
  }

  else{
      $errorMessage .= "Please input your message!";
      $mess = FALSE;
  }

  if($comp && $n && $e && $cont && $mess){ //if everything's been filled out correctly, send the mail
      $ip = $_SERVER['REMOTE_ADDR'];
      $time = time();
      $date_message = date("r", $time);
      $subject="New Enquiry";
      $email="me@mine.com";

      //format the message
      $msg .= "IP: $ip\n";
      $msg .= "$date: $date_message\n";
      $msg .= "$subject\n";
      $msg .= "Company: $company\n";
      $msg .= "Name: $name\n";
      $msg .= "Email: $email\n";
      $msg .= "Message: $message\n";

      mail($email,$subject,$msg);
      echo "Your message has been sent.  We will contact you with a reply as soon as possible.";
  }

  else{ //something is wrong, so print our error message
      echo "<span style='color: red;'>Could not send e-mail for the following reasons:<br />$errorMessage";
  }

else{ //the form HASN'T been submitted yet
  ?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <div align="center">
                <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable">
                  <tr>
                    <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Company name:</b></p></td>
                    <td><input name="company" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Name:</b></p></td>
                    <td><input name="name" type ="text" size="50" maxlength="30" />
                        <div align="center"></div></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Your Email:</b></p></td>
                    <td><input name="email" type ="text" size="50" maxlength="50" /></td>
                  </tr>
                  <tr>
                    <td><p align="left"><b>Contact Number:</b></p></td>
                    <td><input name="contact" type ="text" size="50" maxlength="25" /></td>
                  </tr>
                  <tr>
                    <td width="150"><p align="left"><b>Your Messsage:</b></p></td>
                    <td><textarea name="message" cols="50" rows="8"></textarea></td>
                  </tr>
                  <tr>
                    <td class="bottomleft">&nbsp;</td>
                    <td><input name="submit" type="submit" value="Send Message" /></td>
                  </tr>
                </table>
                </p>
        </div>
      </form>

  <?php
} //ending bracket for our else-conditional
?>

[/code]
Thanks, i should have noticed those "..." tbh.......

I'm now getting this error:

[code]Parse error: parse error, unexpected T_ELSE in /path/path/path/path/www/contactus.php on line 137[/code]

I dont really want someone to just fix it for me....... but id be really please if someone could explain what the error means, and help me figure it out for myself.

Ive checked all the Brace's and they all appear to close ... ive also checked that there are now else statements without If staements and vis a vis .....

Thanks in advance
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.