• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

PHP Contact Form issue

Participant ,
Sep 20, 2017 Sep 20, 2017

Copy link to clipboard

Copied

I have been trying for ages to create a Contact Form and recently discovered a followable tutorial on Youtube. I know that they cannot be 100% trusted.

Can someone please check out the code below and tell me why I am not receiving emails when I do a test. I have crossed out my email address for obvious reasons and this I have checked and correct.

I like the way this looks - www.ultimate-top-trumps.co.uk/contact.php and would like to keep it as close as possible. I do have a CSS file but its not this hence I have pasted in the php script page.

I have used Dreamweaver CS6 and using a Mac.

Any help would be greatly appreciated!

<?php

// Set email variables

$email_to = 'xxxxxxx';

$email_subject = 'Form submission';

// Set required fields

$required_fields = array('fullname','email','comment');

// set error messages

$error_messages = array(

  'fullname' => 'Please enter a Name to proceed.',

  'email' => 'Please enter a valid Email Address to continue.',

  'comment' => 'Please enter your Message to continue.'

);

// Set form status

$form_complete = FALSE;

// configure validation array

$validation = array();

// check form submittal

if(!empty($_POST)) {

  // Sanitise POST array

  foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

  // Loop into required fields and make sure they match our needs

  foreach($required_fields as $field) {

  // the field has been submitted?

  if(!array_key_exists($field, $_POST)) array_push($validation, $field);

  // check there is information in the field?

  if($_POST[$field] == '') array_push($validation, $field);

  // validate the email address supplied

  if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);

  }

  // basic validation result

  if(count($validation) == 0) {

  // Prepare our content string

  $email_content = 'New Website Comment: ' . "\n\n";

  // simple email content

  foreach($_POST as $key => $value) {

  if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";

  }

  // if validation passed ok then send the email

  mail($email_to, $email_subject, $email_content);

  // Update form switch

  $form_complete = TRUE;

  }

}

function validate_email_address($email = FALSE) {

  return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;

}

function remove_email_injection($field = FALSE) {

   return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<!-- Contact Form Designed by James Brand @ dreamweavertutorial.co.uk -->

<!-- Covered under creative commons license - http://dreamweavertutorial.co.uk/permissions/contact-form-permissions.htm -->

  <title>Contact Form</title>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <link href="contact/css/contactform.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools-yui-compressed.js"></script>

    <script type="text/javascript" src="contact/validation/validation.js"></script>

  <script type="text/javascript">

  var nameError = '<?php echo $error_messages['fullname']; ?>';

  var emailError = '<?php echo $error_messages['email']; ?>';

  var commentError = '<?php echo $error_messages['comment']; ?>';

  </script>

</head>

<body onload="MM_preloadImages('contact.images/x.png')">

<div id="formwrap">

<h2> We appreciate your feedback.</h2>

<div id="form">

<?php if($form_complete === FALSE): ?>

<form action="contact.php" method="post" id="comments_form">

  <div class="row">

    <div class="label">Your Name</div> <!--end .label -->

    <div class="input">

    <input type=="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>

"/><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>

    </div><!-- end .input -->

    <div class="context">e.g. John Smith or Jane Doe</div><!-- end .context -->

    </div><!--end .row -->

   

    <br>

   

    <div class="row">

    <div class="label">Your Email Address</div> <!--end .label -->

    <div class="input">

    <input type=="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>"/><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>

    </div><!-- end .input -->

    <div class="context">We will never share your details</div><!-- end .context -->

    </div><!--end .row -->

   

    <br>

  <div class="row">

    <div class="label">Your Message</div> <!--end .label -->

    <div class="iput2">

    <textarea id="comment" name="comment" class="mess"><?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?>

</textarea><?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>

    </div><!-- end .input -->

    </div><!--end .row -->

    <div class="Submit">

      <p>

        <input type ="submit" id="submit" name="submit" value="Send Message"/>

      </p>

   

    </div><!-- end .submit -->

   

    </form>

<?php else: ?>

<p style="font-size:18px; font-family:Verdana, Geneva, sans-serif; font-weight:bold; color:#000; margin-left:25px;">Thank you for your Message!</p>

<script type="text/javascript">

setTimeout ('ourRedirect()', 5000)

function ourRedirect(){

  location.href='index.html'

}

</script>

<?php endif; ?>

</div><!--end of form --></div>

<p> </p>

<p> </p>

</body>

</html>

Views

4.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 26, 2017 Sep 26, 2017

nigelh70638339  wrote

I asked 123 last week and they told me that the code is wrong and basically, tough, just as you said osgood.

There's nothing wrong with the code, tell 123 you have tested it along with other php mail scripts on several different php servers where the code works. If it works for markw39954149 and myself that's conclusive proof.

The problem is you are trying to run php on a Windows server which is not its native environment, that's a problem, especailly if the host refuses to ac

...

Votes

Translate

Translate
LEGEND ,
Sep 22, 2017 Sep 22, 2017

Copy link to clipboard

Copied

Take the php code you posted in your previous response above and  insert it in a COMPLETELY BLANK Dreamweaver document then copy the form code below and paste it AFTER the php code and save it as feedback_form.php

Upload it to your server and browse out to it.. If your server is acting like it should do the form should appear and stay there until you click the 'submit' button. Fill in the form fields and click the submit button. You should get the information from the form sent to your email address and then the page should redirect to index.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Form Test</title>

</head>

<body>

<form name="feedback_form" action="feedback_form.php" method="post">

<div>

<label for="Name">Name</label>

<input type="text" name="Name" id="Name">

</div>

<div>

<label for="Email">Email</label>

<input type="text" name="Email" id="Email">

</div>

<div>

<label for="Tel">Telephone</label>

<input type="text" name="Tel" id="Tele">

</div>

<div>

<label for="Message">Message</label>

<input type="text" name="Message" id="Message">

</div>

<div>

<input type="submit" name="submit" value="Submit">

</div>

</form>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 23, 2017 Sep 23, 2017

Copy link to clipboard

Copied

All done but no email. It did redirect after firstly showing empty form fields briefly.

Just noticed that my last post shows my email address twice (my mistake) could someone please delete these?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2017 Sep 23, 2017

Copy link to clipboard

Copied

The only way to establish if this is a server error or if you are not following the set up correctly is to test the script on a dedicated php server not a Windows server which has possibly been configured incorrectly to attempt to try and run php scripts.

Windows is not a native environment on which to run php. I have had issues in the past where a website is hosted on a Windows box and the provider says its configured to run php but a few simple test confirmed it wasnt.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 23, 2017 Sep 23, 2017

Copy link to clipboard

Copied

OK, what would you recommend that I do? Sorry if I sound a bit green but I am already on the verge of my abilities.

I do have a forum powered by YaBB and I find that every time that I access the page it takes forever even when I was using Windows. 123 just say that it is my coding that is at fault and that they cannot help with this. I used to be with 1&1 and the forum worked fine there.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

I tried with your code and executed in my server. I got mail. I thought you have mail server issues. Check once again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

markw39954149  wrote

I tried with your code and executed in my server. I got mail. I thought you have mail server issues. Check once again.

Thanks for confirming. I concur with you, the OP's server is not set up correctly to process php.

The problem is if the host gets a whiff of thinking you dont know what you are doing they will tell you it's your code at fault, when quite often it turns out to be their server configurations. Testing on other php enabled servers with positive results usually confirms where the fault lies. Once you point this out to the host either they can fix the issue or can't. If they are unwilling to help then find another host. Unfortunately its 'pile them hire, sell them cheap attitude' - some hosts don't care and can't be bothered, its volume sales units which make them money not sorting out server issues.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

OK, but I am a complete novice at php and only really just got to grips with css. Is there anything in the code that you can see that might be the problem?

I asked 123 last week and they told me that the code is wrong and basically, tough, just as you said osgood.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

nigelh70638339  wrote

I asked 123 last week and they told me that the code is wrong and basically, tough, just as you said osgood.

There's nothing wrong with the code, tell 123 you have tested it along with other php mail scripts on several different php servers where the code works. If it works for markw39954149 and myself that's conclusive proof.

The problem is you are trying to run php on a Windows server which is not its native environment, that's a problem, especailly if the host refuses to accept its there rsponsiblity, assuming you have done everything correct at your end and the server supports the php mail function.

Hosts will never accept anything unless you have  proof and then if they still refuse to do anything about it that leaves little choice other than to up-sticks and move. Do you really think the host will spend time trying to work out what is wrong when you are a small customer paying probably £4 a month for hosting, they are not bothered at all..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

I have sent another ticket to 123 telling them that the code has ben tested on other servers with no issues.

What hosts can you recommend please?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 30, 2017 Sep 30, 2017

Copy link to clipboard

Copied

Had a reply back from the technical dept and they have told me that I need to add

ini_set('SMTP','intmail');

to my code. I am now waiting for them to tell me where in my contact form it goes.

Thanks osgood and Mark for your help, I will let you know once it has been sorted. Hopefully this will help others with the same issues.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2017 Sep 30, 2017

Copy link to clipboard

Copied

Unfortunately l cant help you as l have never used stmp to process a form but by the looks of the reply that is what you are going to need to do. Its a different and more complex  process to using the php mail function and its obvious now the server you are on does not support the php mail function.

If a host cannot provide the workflow l need generally l'll find one that will.

I hope you can eventually get this resolved one way or another.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 30, 2017 Sep 30, 2017

Copy link to clipboard

Copied

I dont like the sound of more complex to be honest.

Is there a Linux host that you can recommend? I have had a look for Linux hosts and they seem about the same but I would prefer one that is used by users here as I trust your opinions farther that a load of sales rubbish.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2017 Sep 30, 2017

Copy link to clipboard

Copied

SMTP is an email server.  And it's often required with Windows hosting plans.  To authenticate sending emails, you must enter your server's email address and password in the script.  Which means you will need to create an email account on your host if you haven't already.

PHPMailer is a form processing script that supports SMTP.  You can download it from GitHub.

https://github.com/PHPMailer/PHPMailer/archive/master.zip

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 02, 2017 Oct 02, 2017

Copy link to clipboard

Copied

I have tried to place the aforementioned code just after the <?php an before the // Set email variables lines as 123 said do but still nothing works.

They did mention that I am trying to use php on a windows server just as some have said here. So I am going to change my host to 1&1. I dont want to have to download more script as you mention as an option Nancy and besides I find that 123 seems to have a lot of slow time, timing out even on their homepage. Other sites generally work fine.

Thank you to everyone who has helped  especially osgood and Mark with my coding, at least I know that that works properly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 02, 2017 Oct 02, 2017

Copy link to clipboard

Copied

nigelh70638339  wrote


Thank you to everyone who has helped  especially osgood and Mark with my coding, at least I know that that works properly.

No problem.

Let us know how it goes once you have switched hosts.

Just make sure to ask them if the 'php mail function' is supported before you committ to purchasing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

I have finally switched hosts and the form is now sending messages and everything is now working correctly.

I cannot thank you all enough for the help and advice that you have given me with this. I could not have done it really without your help in checking the code  etc, especially osgood and Mark!

Thank you!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

nigelh70638339  wrote

I have finally switched hosts and the form is now sending messages and everything is now working correctly.

I cannot thank you all enough for the help and advice that you have given me with this. I could not have done it really without your help in checking the code  etc, especially osgood and Mark!

Thank you!!

Excellent news! Glad everything is resolved and thanks for taking the time to update us on the situation.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

Who is Mark?

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

LATEST

Nevermind.  I now see Mark's post.  It got lost in this long thread.

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines