Jump to content

Cant get PHP form script to work Please Help


simon71

Recommended Posts

Hi,

 

Im new to PHP and contact forms so need some help getting my data collection form to work.

 

Im hosting on Fasthost. My form action is '/htdocs/form/sendmail.php'.

 

my script is:

 

<?php
$email_to="form@iter-research.co.uk";
$name=$_POST["Name"];
$email_from=$_POST["form@iter-research.co.uk"];
$message=$_POST["Name,Address,Postcode,Email,Gender,Age,Correspondence"];
$email_subject="Consultation Form";
$headers=
"from:$email_from.\n";
"reply-to:$email_from.\n";

if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
die("The email address entered is invalid.");
$message = "Name: ". $name . "\r\nMessage: " . $message;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, -f$email_from);
if($sent) {
header("Location: http://www.iter-rese...hank_you.html/");

} else {
echo "There has been an error sending your message. Please try later.";
}
}
?>

 

When i hit the submit button the browser just trys to open a new window with my PHP script in.

 

I'm New to PHP and contact forms so if im doing something really basic and stupid i apologise in advance.

 

has anyone got any suggestion??

 

Simon

Link to comment
Share on other sites

Try this. tidied it up a bit. It might be that your "TARGET" is _BLANK (In the FORM tag which is part the form)

<?php
$email_to =      "form@iter-research.co.uk";
$name =          $_POST["Name"];
$email_from =    $_POST["form@iter-research.co.uk"];
$message =       $_POST["Name"] . $_POST["Address"] . $_POST["Postcode"] . $_POST["Email"] . $_POST["Gender"] . $_POST["Age"] . $_POST["Correspondence"];
$email_subject = "Consultation Form";
$headers =       "FROM: $email_from.\n" . "reply-to:$email_from.\n";

if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
   die("The email address entered is invalid.");
} else {
   $message = "Name: ". $name . "\r\nMessage: " . $message;

   ini_set("sendmail_from", $email_from);

   $sent = mail($email_to, $email_subject, $message, $headers);

   if($sent) {
      header("Location: http://www.iter-rese...hank_you.html/");
   } else {
      echo "There has been an error sending your message. Please try later.";
   }
}
?>

$_POST["form@iter-research.co.uk"];

Do you seriously have a form field called that?

Link to comment
Share on other sites

The form Script is:

 

<form action="/htdocs/form/sendmail.php" method="post" name="form1" target="_blank" id="form1">
        <table width="100%" border="0" cellspacing="0" cellpadding="3">
        <tr>
        <td width="38%"><label>
        <div align="right">Name</div>
        </label></td>
        <td width="62%"><input name="Name" type="text" id="Name" tabindex="1" size="50" /></td>
        </tr>
        <tr>
        <td><label>
        <div align="right">Address</div>
        </label></td>
        <td><textarea name="Address" cols="50" id="Address" tabindex="2"></textarea></td>
        </tr>
        <tr>
        <td><label>
        <div align="right">Postcode</div>
        </label></td>
        <td><input name="Postcode" type="text" id="Postcode" tabindex="3" /></td>
        </tr>
        <tr>
        <td><label>
        <div align="right">Email</div>
        </label></td>
        <td><input name="Email" type="text" id="Email" tabindex="4" size="50" /></td>
        </tr>
        <tr>
        <td><label>
        <div align="right">Gender</div>
        </label></td>
        <td><select name="Gender" size="1" id="Gender" tabindex="5">
        <option value="1">Male</option>
        <option value="2">Female</option>
        <option value="0" selected="selected">Please Select</option>
        </select></td>
        </tr>
        <tr>
        <td><label>
        <div align="right">Age</div>
        </label></td>
        <td><select name="Age" size="1" id="Age">
        <option value="0">Please Select</option>
        <option value="1"><16</option>
        <option value="2">17-20</option>
        <option value="3">21-25</option>
        <option value="4">26-30</option>
        <option value="5">31-35</option>
        <option value="6">36-40</option>
        <option value="7">41-45</option>
        <option value="8">46-50</option>
        <option value="9">50></option>
        </select></td>
        </tr>
        <tr>
        <td><label>
        <div align="right">Would you prefer any further correspondence to be in Welsh Or English </div>
        </label></td>
        <td><select name="Correspondence" size="1" id="Correspondence">
        <option value="0">Please Select</option>
        <option value="1">Welsh</option>
        <option value="2">English</option>
        </select>
        <label>
        <input type="submit" name="Submit" value="Submit" />
        <input name="Reset" type="reset" id="Reset" value="Reset" />
        </label></td>
        </tr>
        </table>
    </form>

 

Link to comment
Share on other sites

Oh I understand what you mean. On your form page.. just include something like this..

<?php echo 'It supports PHP'; ?>

 

Then try it out, see if it echo's it.

The filename extension should also be .php

 

Just did this and i got the 'it supports PHP' message back.

 

What next??

Link to comment
Share on other sites

<?php
if(isset($_POST['Submit'])) {
    $email_to =      "form@iter-research.co.uk";
    $name =          $_POST["Name"];
    $email_from =    $_POST["form@iter-research.co.uk"];
    $message =       $_POST["Name"] . $_POST["Address"] . $_POST["Postcode"] . $_POST["Email"] . $_POST["Gender"] . $_POST["Age"] . $_POST["Correspondence"];
    $email_subject = "Consultation Form";
    $headers =       "FROM: $email_from.\n" . "reply-to:$email_from.\n";

    if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
       die("The email address entered is invalid.");
    } else {
       $message = "Name: ". $name . "\r\nMessage: " . $message;

       ini_set("sendmail_from", $email_from);

       $sent = mail($email_to, $email_subject, $message, $headers);

       if(isset($sent)) {
          header("Location: http://www.iter-rese...hank_you.html/");
       } else {
          echo "There has been an error sending your message. Please try later.";
       }
   }
} else {
    echo 'You never clicked the submit button!';
}
?>

Link to comment
Share on other sites

Change..

    if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
       die("The email address entered is invalid.");
    } else {

To this..

$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"."\.([a-z]{2,}){1}$";
if (!eregi($regex,($email_from))) {
       die("The email address entered is invalid.");
} else {

Link to comment
Share on other sites

code now reads:

<?php
if(isset($_POST['Submit'])) {
    $email_to =      "form@iter-research.co.uk";
    $name =          $_POST["Name"];
    $email_from =    $_POST["form@iter-research.co.uk"];
    $message =       $_POST["Name"] . $_POST["Address"] . $_POST["Postcode"] . $_POST["Email"] . $_POST["Gender"] . $_POST["Age"] . $_POST["Correspondence"];
    $email_subject = "Consultation Form";
    $headers =       "FROM: $email_from.\n" . "reply-to:$email_from.\n";

    $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"."\.([a-z]{2,}){1}$";
if (!eregi($regex,($email_from))) {
       die("The email address entered is invalid.");
} else {
       $message = "Name: ". $name . "\r\nMessage: " . $message;

       ini_set("sendmail_from", $email_from);

       $sent = mail($email_to, $email_subject, $message, $headers);

       if(isset($sent)) {
          header("Location: http://www.iter-rese...hank_you.html/");
       } else {
          echo "There has been an error sending your message. Please try later.";
       }
   }
} else {
    echo 'You never clicked the submit button!';
}
?>

 

still get email address is invalid

Link to comment
Share on other sites

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.