Problem just started with code on website

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Spikenaylor
Forum Newbie
Posts: 2
Joined: Thu Oct 27, 2011 8:55 am

Problem just started with code on website

Post by Spikenaylor »

Hi there.

I have no experience in PHP but can get around and work some things out.

I put up an email form PHP script on my website and evrything worked fine, webpage Form was sumbitted and recieved at the email end, but all of a sudden it has stopped working, I keep getting a error:
'The Subject you entered does not appear to be valid'
Ie. no text entered into the subject field. Text has been entered but the form is not seeing the text or it isnt being passed into the PHp script.

Entire code is below.
Can anyone see why this is so, as I said it worked originally and now for some reason it does not.
I have reuploaded a copy of the original PHP code, but still no luck.

Many thanks for looking.

Code: Select all

<div id="content">
  <h1> Contact Us Form Confirmation</h1>
      <?php
ini_set("include_path", '/home/holywell/php:' . ini_get("include_path")  ); 
if(isset($_POST['email'])) {
    
    // EDIT THE 2 LINES BELOW AS REQUIRED
     $email_to = "enquires@holywellsociety.org.uk";
   // $email_subject = "Please provide information.";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
		die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
		!isset($_POST['comments']) ||
		!isset($_POST['subject'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    $email_subject = "For Holywell Society:- ".$_POST['subject']; // required
	
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($subject) < 2) {
    $error_message .= 'The Subject you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers, "-f $email_from");  
?>
 
<!-- include your own success html here -->
<div align="center"><img src="../../images/contact-us-thank-you.jpg" alt="Contact Us Thank you" width="520" height="346" border="2" />  </div>
<p class="contenttext">Thank you for contacting us. We will be in touch with you very soon.</p>
 
<?php
}
?>

</div>


Below is the code for the Form on the webpage:

Code: Select all

<div id="EmailForm">
<form name="contactform" method="post" action="php/email_form.php">
<table width="450px" align="center" id="ContactFormTable">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input name="first_name"  type="text" class="FormBorder" size="30" maxlength="50" >
 </td>
</tr> 
<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input name="last_name"  type="text" class="FormBorder" size="30" maxlength="50">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input name="email"  type="text" class="FormBorder" size="30" maxlength="80">
 </td>
 
</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input name="telephone"  type="text" class="FormBorder" size="30" maxlength="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="subject">Message Subject *</label>
 </td>
  <td valign="top">
  <input name="subject"  type="text" class="FormBorder" size="40" maxlength="40">
 </td>
 </tr>
 
<tr>
 <td valign="top">
  <label for="comments">Message/Question *</label>
  <p>&nbsp;</p>
  <p>* (Required)</p></td>
 <td valign="top">
  <textarea  name="comments" cols="25" rows="6" class="FormBorder" maxlength="1000"></textarea>
 </td>
 
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" class="control" value="Send" /><input type="reset" class="control" value="Reset" />
 </td>
</tr>
</table>
</form>
</div>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Problem just started with code on website

Post by Celauran »

You assign $_POST['subject'] to $email_subject, but the rest of the script is looking for $subject.
Spikenaylor
Forum Newbie
Posts: 2
Joined: Thu Oct 27, 2011 8:55 am

Re: Problem just started with code on website

Post by Spikenaylor »

Many Thanks
Obvious programming error by yours truly. DOH.

Another question is how can i place a failure image.jpg in the Died Routine.

The script displays a image for success, But I would like one for failure.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Problem just started with code on website

Post by Celauran »

Spikenaylor wrote:Another question is how can i place a failure image.jpg in the Died Routine.

The script displays a image for success, But I would like one for failure.
You could echo an img tag inside the function.
Post Reply