Log in

View Full Version : Forms coming through blank



jhatter
03-15-2010, 11:53 AM
Hi,

I'm trying to create a website 'Contact' form using PHP for the first time. I found a free form online that I have adapted for my own purposes.

So to verify, I have my contact.html file and another called contactengine.php.

When I upload these files to my server and test it online, I get the form message, but the info I typed-in does not show - it is blank.

Please can anyone make any suggestions why this maybe?

Thanks in anticipation of your replies!


<div id="formcontainer">
<form id="comments_form" action="contactengine.php">
<fieldset>
<legend>Contact:</legend>
<p>
<label for="Name">Name: </label>
<input type="text" name="Name" id="Name" />
</p>

<p>
<label for="Email">Email Address: </label>
<input type="text" name="Email" id="Email" />
</p>

<p>
<label for="Message">Message:</label>
<textarea name="Message" rows="10" cols="20" id="Message">
</textarea>
</p>

<div>
<input type="submit" name="Submit" id="Submit" value="Submit" class="btn" />
</div>

</fieldset>

</form>
</div>

<?php

$EmailFrom = "johnhathwayatwaitrose.com";
$EmailTo = "johnatjohnhathway.co.uk";
$Subject = "A Message From Your Website";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));

// validation
$validationOK=true;
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 .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$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=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

fileserverdirect
03-15-2010, 11:38 PM
Before you test the php code, try adding this to your <form> tag:


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

If that doesn't work, comment out your redirection script and just put echo $Name; somewhere just to check if it is going through...

Schmoopy
03-15-2010, 11:52 PM
By default the form will come through in the $_GET array, but it's best to go with the post option. You could also try using the $_REQUEST array if you still don't get any data, as this should give the greatest chance of pulling something through.

I imagine changing the method to "post" will solve this though.

jhatter
03-16-2010, 10:05 AM
Hi,

Thanks for your replies.

I changed it to the following and now it works OK


<form method="Post" action="contactengine.php">


But..... I generated a second form utilising this one once we got it working, I created two new fields - one of them works OK but the other is still coming through blank.

The 'Journal' field comes through blank.

One interesting thing: the 'Tel' field was originally called 'Telephone' and it didn't work, but then I shortened it to 'Tel' and it worked OK!?!?

Do you guys have any suggestions please?


<div id="formcontainer">
<form method="Post" action="pressengine.php">

<fieldset>
<legend>For press and media requests, please fill in the the form below.</legend>
<p>
<label for="Name">Name: </label>
<input type="text" name="Name" id="Name" />
</p>

<p>
<label for="Journal">Journal: </label>
<input type="text" name="Journal" id="Journal" />
</p>

<p>
<label for="Email">Email Address: </label>
<input type="text" name="Email" id="Email" />
</p>

<p>
<label for="Tel">Tel:</label>
<input type="text" name="Tel" id="Tel" />
</p>

<p>
<label for="Message">Message:</label>
<textarea name="Message" rows="10" cols="20" id="Message">
</textarea>
</p>

<div>
<input type="Submit" name="Submit" id="Submit" value="Submit" class="btn" />
</div>

</fieldset>

</form>
</div>

pressengine.php:

<?php

$EmailFrom = "johnhathway@waitrose.com";
$EmailTo = "john@johnhathway.co.uk";
$Subject = "A Press Enquiry";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Journal']));
$Email = Trim(stripslashes($_POST['Email']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));

// validation
$validationOK=true;
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 .= "Journal: ";
$Body .= $Journal;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$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=pressthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Schmoopy
03-16-2010, 10:34 AM
This is caused by a simple mistake where you have changed the same variable twice. Look closely here:



$Email = Trim(stripslashes($_POST['Journal']));
$Email = Trim(stripslashes($_POST['Email']));


$_POST['Journal'] is being assigned to the $email variable, and then the $email variable is changed to hold a different value on the next line, that is why you don't see the $Journal variable coming through.

Change to:



$Journal = Trim(stripslashes($_POST['Journal']));
$Email = Trim(stripslashes($_POST['Email']));


And you should be sorted.

You may also want to set your error reporting to a higher level, as it would have warned you about this:



Notice: Undefined variable: Journal in "YOUR FILE HERE" on line X

jhatter
03-16-2010, 12:39 PM
Thanks Schmoopy, it's good to have people like you around. Everything is working now!