FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Contact Form Issues





welshsteve
Hi everyone. In trying to adapt a script for a contact form to suit my needs but am struggling a bit. I got this to work:
Code:

        <h1>Contact Us</h1>
        <p>Please fill out the form below.</p>
        <?php
          $to = 'matchsec@ldbsa.co.uk';
          $subject = 'LDBSA Feedback';
          $contact_submitted = 'Thank you for contacting us. We will be in touch soon.';

          // Do not amend anything below here, unless you know PHP
          function email_is_valid($email) {
            return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
          }
          if (!email_is_valid($to)) {
            echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
          }
          if (isset($_POST['contact_submitted'])) {
            $return = "\r";
            $youremail = trim(htmlspecialchars($_POST['your_email']));
            $yourname = stripslashes(strip_tags($_POST['your_name']));
            $yourmessage = stripslashes(strip_tags($_POST['your_message']));
            $contact_name = "Name: ".$yourname;
            $message_text = "Message: ".$yourmessage;
            $user_answer = trim(htmlspecialchars($_POST['user_answer']));
            $answer = trim(htmlspecialchars($_POST['answer']));
            $message = $contact_name . $return . $message_text;
            $headers = "From: ".$youremail;
            if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
              mail($to,$subject,$message,$headers);
              $yourname = '';
              $youremail = '';
              $yourmessage = '';
              echo '<h4 class="contact">'.$contact_submitted.'</h4>';
            }
            else echo '<h4 class="contact">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</h4>';
          }
          $number_1 = rand(1, 9);
          $number_2 = rand(1, 9);
          $answer = substr(md5($number_1+$number_2),5,10);
        ?>
        <form id="contact" action="contact.php" method="post">
          <div class="form_settings">
            <p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>
            <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="<?php echo $youremail; ?>" /></p>
            <p><span>Message</span><textarea class="contact textarea" rows="5" cols="50" name="your_message"><?php echo $yourmessage; ?></textarea></p>
            <p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
            <p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
            <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
          </div>
        </form>



But when I try to adapt it to include selects and additional fields I get a bit lost. Here's what I have so far. It keeps refreshing telling me I've not entered everything correctly. Is anybody able to shed any light on the problem for me?

Code:

        <h1>Match Score Submission</h1>
        <p>Please fill out the form below.</p>
        <?php
          $to = 'matchsec@ldbsa.co.uk';
          $subject = 'Match Score';
          $contact_submitted = 'Result entered successfully.';

          function email_is_valid($email) {
            return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
          }
          if (!email_is_valid($to)) {
            echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
          }
          if (isset($_POST['contact_submitted'])) {
            $return = "\r";
            $youremail = trim(htmlspecialchars($_POST['your_email']));
            $yourname = stripslashes(strip_tags($_POST['your_name']));
            $division = stripslashes(strip_tags($_POST['divison']));
            $week = stripslashes(strip_tags($_POST['week']));
            $teamh = stripslashes(strip_tags($_POST['teamh']));
            $teama = stripslashes(strip_tags($_POST['teama']));
            $scoreh = stripslashes(strip_tags($_POST['scoreh']));
            $scorea = stripslashes(strip_tags($_POST['scorea']));
            $yourmessage = stripslashes(strip_tags($_POST['other_info']));
            $contact_name = "Name: ".$yourname;
            $message_text = "Message: ".$yourmessage;
            $user_answer = trim(htmlspecialchars($_POST['user_answer']));
            $answer = trim(htmlspecialchars($_POST['answer']));
            $message = $contact_name . $return . $message_text;
            $headers = "From: ".$youremail;
            if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && $division != "" && $week != "" && $teamh != "" && $teama != "" && $scoreh != "" && $scorea != "" && substr(md5($user_answer),5,10) === $answer) {
              mail($to,$subject,$message,$headers);
              $yourname = '';
              $youremail = '';
              $division = '';
              $week = '';
              $teamh = '';
              $teama = '';
              $scoreh = '';
              $scorea = '';
              $yourmessage = '';
              //echo '<p style="color: blue;">'.$contact_submitted.'</p>';
              echo '<h4 class="contact">'.$contact_submitted.'</h4>';
            }
            else echo '<h4 class="contact">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</h4>';
          }
          $number_1 = rand(1, 9);
          $number_2 = rand(1, 9);
          $answer = substr(md5($number_1+$number_2),5,10);
        ?>
        <form id="contact" action="s_submitscore.php" method="post">
          <div class="form_settings">
            <p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>
            <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="<?php echo $youremail; ?>" /></p>
            <p><span>Division</span><select name="division"><option value="Premier">Premier Division</option><option value="A">A Division</option><option value="B">B Division</option></select></p>
            <p><span>Week</span><select name="week" id="week">
               <option value="1">1</option>
               <option value="2">2</option>
               <option value="3">3</option>
               <option value="4">4</option>
               <option value="5">5</option>
               <option value="6">6</option>
               <option value="7">7</option>
               <option value="8">8</option>
               <option value="9">9</option>
               <option value="10">10</option>
         </select></p>
            <table class="blank">
         <tr><th>Home Team</th><th colspan="3">Score</th><th>Away Team</th></tr>
         <tr><td><select name="teamh" id="teamh">
            <option value="Team1">Team1</option>
            <option value="Team2">Team2</option>
            <option value="Team3">Team3</option>
            <option value="Team4">Team4</option>
         </select></td>
            <td><input class="sml" type="text" name="scoreh" id="scoreh" value="0" /></td><td>-</td><td><input class="sml" type="text" name="scorea" id="scorea" value="0" /></td>
            <td><select name="teama" id="teama">
            <option value="Team1">Team1</option>
            <option value="Team2">Team2</option>
            <option value="Team3">Team3</option>
            <option value="Team4">Team4</option>
         </select></td></tr>
         </table>
            <p><span>Other info</span><textarea class="contact textarea" rows="5" cols="50" name="other_info"><?php echo $yourmessage; ?></textarea></p>
            <p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
            <p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
            <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
          </div>
        </form>


If you want to see what's happening live, it's available here - www.ldbsa.co.uk/testing/new/s_submitscore.php
Marcuzzo
ok so I ran your code and changed it a little to display the output.
these are the errors

Code:
Notice: Undefined index: divison in C:\Users\marcom\tools\xampp\htdocs\index.php on line 30
Deprecated: Function eregi() is deprecated in C:\Users\marcom\tools\xampp\htdocs\index.php on line 43
Deprecated: Function eregi() is deprecated in C:\Users\marcom\tools\xampp\htdocs\index.php on line 43


you've got divison instead of division
Code:
$division = stripslashes(strip_tags($_POST['divison']));

should be
Code:
$division = stripslashes(strip_tags($_POST['division']));


you get that message because your condition is false
you're checking and, and, and and and and... everything is ok except that there is no divison ( but division) and that makes your condition return false and hence displaying the message
Code:
if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && $division != "" && $week != "" && $teamh != "" && $teama != "" && $scoreh != "" && $scorea != "" && substr(md5($user_answer),5,10) === $answer)
{
 }
else echo '<h4 class="contact">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</h4>';       


EDIT:
what you could also do:
create a function that checks if the form was submitted:
Code:
function _isFormSubmitted()
{
  return ( (  isset($_POST['contact_submitted']) ) && ($_POST['contact_submitted'] == 'send') );
}


and then use something like this on all the needed fields:
Code:
<p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" />
<?php if ( ( _isFormSubmitted() ) &&  ( empty($yourname) ) )
{
  print("<span style='color:red;'>Please enter your name</span>");
}
?></p>

this way you know what is missing

END EDIT...


also, this...
Code:
<p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>

will give you errors as the variables are not set.
a good trick is to always initialize your variables..


this should work ( uncomment the mail and comment out the print )
Code:
<h1>Match Score Submission</h1>
        <p>Please fill out the form below.</p>
        <?php

      // initialize vars
      $yourname = '';
      $youremail = '';
      $division = '';
      $week = '';
      $teamh = '';
      $teama = '';
      $scoreh = '';
      $scorea = '';
      $yourmessage = '';


          $to = 'matchsec@ldbsa.co.uk';
          $subject = 'Match Score';
          $contact_submitted = 'Result entered successfully.';

          function email_is_valid($email) {
            return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
          }
          if (!email_is_valid($to)) {
            echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
          }
          if (isset($_POST['contact_submitted'])) {
            $return = "\r";
            $youremail = trim(htmlspecialchars($_POST['your_email']));
            $yourname = stripslashes(strip_tags($_POST['your_name']));
            $division = stripslashes(strip_tags($_POST['division']));
            $week = stripslashes(strip_tags($_POST['week']));
            $teamh = stripslashes(strip_tags($_POST['teamh']));
            $teama = stripslashes(strip_tags($_POST['teama']));
            $scoreh = stripslashes(strip_tags($_POST['scoreh']));
            $scorea = stripslashes(strip_tags($_POST['scorea']));
            $yourmessage = stripslashes(strip_tags($_POST['other_info']));
            $contact_name = "Name: ".$yourname;
            $message_text = "Message: ".$yourmessage;
            $user_answer = trim(htmlspecialchars($_POST['user_answer']));
            $answer = trim(htmlspecialchars($_POST['answer']));
            $message = $contact_name . $return . $message_text;
            $headers = "From: ".$youremail;
            if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && $division != "" && $week != "" && $teamh != "" && $teama != "" && $scoreh != "" && $scorea != "" && substr(md5($user_answer),5,10) === $answer) {

           //~ mail($to,$subject,$message,$headers); // !!!!!!commented out for testing!!!!!!
           print("To: $to<br />Subject: $subject<br />Message: $message<br />Headers: $headers");
              $yourname = '';
              $youremail = '';
              $division = '';
              $week = '';
              $teamh = '';
              $teama = '';
              $scoreh = '';
              $scorea = '';
              $yourmessage = '';
              //echo '<p style="color: blue;">'.$contact_submitted.'</p>';
              echo '<h4 class="contact">'.$contact_submitted.'</h4>';
            }
            else echo '<h4 class="contact">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</h4>';
          }
          $number_1 = rand(1, 9);
          $number_2 = rand(1, 9);
          $answer = substr(md5($number_1+$number_2),5,10);
        ?>
        <form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
          <div class="form_settings">
            <p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>
            <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="<?php echo $youremail; ?>" /></p>
            <p><span>Division</span>
         <select name="division">
         <option value="Premier">Premier Division</option><option value="A">A Division</option><option value="B">B Division</option></select></p>
            <p><span>Week</span><select name="week" id="week">
               <option value="1">1</option>
               <option value="2">2</option>
               <option value="3">3</option>
               <option value="4">4</option>
               <option value="5">5</option>
               <option value="6">6</option>
               <option value="7">7</option>
               <option value="8">8</option>
               <option value="9">9</option>
               <option value="10">10</option>
         </select></p>
            <table class="blank">
         <tr><th>Home Team</th><th colspan="3">Score</th><th>Away Team</th></tr>
         <tr><td><select name="teamh" id="teamh">
            <option value="Team1">Team1</option>
            <option value="Team2">Team2</option>
            <option value="Team3">Team3</option>
            <option value="Team4">Team4</option>
         </select></td>
            <td><input class="sml" type="text" name="scoreh" id="scoreh" value="0" /></td><td>-</td><td><input class="sml" type="text" name="scorea" id="scorea" value="0" /></td>
            <td><select name="teama" id="teama">
            <option value="Team1">Team1</option>
            <option value="Team2">Team2</option>
            <option value="Team3">Team3</option>
            <option value="Team4">Team4</option>
         </select></td></tr>
         </table>
            <p><span>Other info</span><textarea class="contact textarea" rows="5" cols="50" name="other_info"><?php echo $yourmessage; ?></textarea></p>
            <p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
            <p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
            <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
          </div>
        </form>
welshsteve
Hey thanks so much for that. I got it working now. Had to make a few adjustments, but got it Very Happy

I now need to add in more fields, but I understand how the thing works now so hopefully it'll be a breeze. I'll post back here when I got the whole thing working.
Related topics
PHP: Feedback form issues.. help =P
contact form
Need a simple Contact form
For those who need a contact form
I am trying to build a contact form using php and flash
contact form with IP feature ?
AJAX and PHP contact form
Contact Form prob
Need a Contact form from HTML/PHP
Need help to make my Flash contact form functioning!
Easy Contact Form with Instructions
Spam protection for contact form?
Contact form or email address?
Wordpress Contact Form 7 not appearing on Custom Theme
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.