Jump to content

PHP Form Error


dusteve

Recommended Posts

Hi

 

Have found a basic php script for submitting form and I have included in there a checkbox for people to subscribe to.

 

I have a thank you page as well as an error page (which is where they go if they do no complete the required email field).

 

When I test the form and complete the email field (to go to the thank you page) and click on the subscribe box  I get the form emailed to me as I should.  If I do not complete the email then it works as it should and send me to the error page.

 

If i then complete the form properly without ticking the checkbox then it doesn't go to the error page but throws up a 500 Internal Server Error - yet I still get the email? It's great I get the email but from a site user this isn't good at all!

 

The basic html form script is:

<form method="POST" action="contact.php">

Fields marked (*) are required

 

<p>Email From:* <br>

<input type="text" name="EmailFrom">

<p>Name:<br>

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

<p>Phone Number:<br>

<input type="text" name="PhoneNumber">

<p>Insurance Type:<br>

<input type="text" name="InsuranceType">

<p>Details:<br>

<input type="text" name="Details">

<p>Subscribe:<br>

<input type="checkbox" name="Subscribe" value="Yes">

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

</form>

<p>

 

 

 

The PHP script is (Where it says MY EMAIL ADDRESS is the email where I receive the form):

 

<?php

 

// get posted data into local variables

$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));

$EmailTo = "MY EMAIL ADDRESS";

$Subject = "Insurance Enquiry";

$Name = Trim(stripslashes($_POST['Name']));

$PhoneNumber = Trim(stripslashes($_POST['PhoneNumber']));

$InsuranceType = Trim(stripslashes($_POST['InsuranceType']));

$Details = Trim(stripslashes($_POST['Details']));

$Subscribe = Trim(stripslashes($_POST['Subscribe']));

 

// validation

$validationOK=true;

if (Trim($EmailFrom)=="") $validationOK=false;

if (!$validationOK) {

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

exit;

}

 

// prepare email body text

$Body = "";

$Body .= "Name: ";

$Body .= $Name;

$Body .= "\n";

$Body .= "PhoneNumber: ";

$Body .= $PhoneNumber;

$Body .= "\n";

$Body .= "InsuranceType: ";

$Body .= $InsuranceType;

$Body .= "\n";

$Body .= "Details: ";

$Body .= $Details;

$Body .= "\n";

$Body .= "Subscribe: ";

$Body .= $Subscribe;

$Body .= "\n";

 

// send email

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

 

// redirect to success page

if ($success){

print "<meta http-equiv=\"refresh\" content=\"0;URL=form_thank_you.htm\">";

}

else{

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

}

?>

 

Hope anyone can help me with this. I'm very new to PHP (we're talking a matter of days!). Tried PERL/CGI which I've always used before but having an 'issue' with some new hosts at the moment re the permissions not saving correctly on the cgi-script so I'm having to look elsewhere to get the form processed asap - hence the PHP.

 

 

Regards and thanks

 

Link to comment
https://forums.phpfreaks.com/topic/179974-php-form-error/
Share on other sites

The problem might be this line:

 

$Subscribe = Trim(stripslashes($_POST['Subscribe']));

 

If the box is unchecked, the browser won't send the Subscribe parameter, and PHP will emit an error of level E_NOTICE when you try to access the undefined $_POST index 'Subscribe'. Try this instead:

 

if (isset($_POST['Subscribe']))

    $Subscribe = 'Yes';

else

    $Subscribe = 'No';

Link to comment
https://forums.phpfreaks.com/topic/179974-php-form-error/#findComment-949610
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.