Forum Moderators: coopster

Message Too Old, No Replies

Submitting a form to itself

Page doesn't load, but form submits anyway?

         

livetoride

4:46 am on Jul 22, 2007 (gmt 0)



Hi,
I'm new to PHP and I am having trouble submitting the form to itself. The actual form works fine, but once I tried to add the validation if statements, something went wrong. The images don't load on the page, and the form submits anyway. I'm including the code, most of the PHP is at the top, the rest is in the form area,can someone help?

<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Content-type: image/gif");
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$typeofwork = $_REQUEST['typeofwork'];
$message = $_REQUEST['message'];
$reference = $_REQUEST['reference'];

/*I was trying to use this variable for when people hit the submit button to stop the form from executing automatically, but I don't know what exactly to use*/

$submit = $_POST['submit'];
$autoreply = "Thanks for contacting VALUREDESiGN!\nA reponse will be made within 24-48 hours.";
$bodycontent = "$name\n$phone\n$email\n$typeofwork\n$message\n$reference";

if (isset($submit)){
}

if (ereg("^[[:alpha:]]+$", $name)) {
echo "Please enter your name.\n";
}

if (ereg("^([^@]+)@([a-z\-]+\.)+([a-z]{2,4})$", $email)) {
echo "Invalid email, please try again.\n";
}

if (ereg("^[[:alpha:]]+[[:digit:]]*$", $message)) {
echo "Please enter a message.";
?>
<html>
/*Here is where I submit it to itself, is this right?*/

<form action="contactphp.php" method="post">
<table width="350" align="left" valign="top" cellspacing="2">
<tr><td colspan="2"><span class="headertxt">CONTACT</span>
<hr align="left" width="250" size="1px"></td></tr>
<tr>
<td width="150" valign="top" align="right" class="formlabeltxt">Name *</td>
<td width="200" valign="top" align="left"><input type="text" name="name" class="formtxt" size="30" style="width:200"></td>
</tr>
<tr>
<td width="150" valign="top" align="right" class="formlabeltxt">Phone&nbsp;</td>
<td width="200" valign="top" align="left"><input type="text" name="phone" class="formtxt" size="12" style="width:200"></td>
</tr>
<tr>
<td width="150" valign="top" align="right" class="formlabeltxt">Email *</td>
<td width="200" valign="top" align="left"><input type="text" name="email" class="formtxt" size="30" style="width:200"></td>
</tr>
<tr>
<td width="150" valign="top" align="right" class="formlabeltxt">Type of work needed&nbsp;</td>
<td width="200" valign="top" align="left"><select name="typeofwork" class="formtxt" style="width:80px;">
<option value="print">Print</option>
<option value="web">Web</option>
<option value="both">Both</option>
</select></td>
</tr>
<tr>
<td width="150" valign="top" align="right" class="formlabeltxt">Message *</td>
<td width="200" valign="top" align="left"><textarea name="message" class="formtxt" style="width:200; height:80"></textarea></td>
</tr>
<tr>
<td width="150" valign="middle" align="right" class="formlabeltxt">How did you hear of VALUREDESiGN?</td>
<td width="200" valign="middle" align="left"><select name="reference" class="formtxt">
<option value="bizcard">Business card</option>
<option value="search">Internet search</option>
<option value="client">Already a client</option>
<option value="myspace">MySpace</option>
<option value="mouth">Word of mouth</option>
<option value="other">Other</option>
</select></td>
</tr>
<tr>
<td width="150" valign="middle" align="right"><font size="1" face="Arial">* denotes required</font></td>
<td width="200" valign="middle" align="center"><input type="reset" name="reset" value="Clear">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Submit"></td>
</tr>
</table></form></blockquote></td>

/*Where I send the mail, is there something I can put here to stop it from sending automatically?*/
<?php
}
else {
mail("me@example.com", "Contact Form Submission", $bodycontent, "From: me");
mail($email, "Auto-Reply", $autoreply, "From: VALUREDESiGN");
header("Location: http://www.example.com/thanks.html");
}
?>
<td background=<?php echo "images/index_03.gif"?> valign="top" align="left"><span class="updateheadertxt">HOURS & INFO</span>
<hr align="left" width="100" size="1px">
<span class="updatebodytxt">C: 845.551.7138<br>
Hours: 10am - 6pm// M&ndash;F<br>
<br>
E: <a href="mailto:me@example.com">email</a></span></td>
<td>
<img src="images/spacer.gif" width="1" height="151" alt=""></td>
</tr>
<tr>
<td align="left" valign="top" background=<?php echo "images/index_04.gif"?>><span class="updateheadertxt">FORMS</span>
<hr align="left" width="100" size="1px">
<span class="updatebodytxt">Ready to get started?<br>
<a href="npf.pdf">print</a> a new project form<br>
<br>
click here to view and print my contract</span></td>
<td>
<img src="images/spacer.gif" width="1" height="152" alt=""></td>
</tr>
<tr>
<td colspan="2" background=<?php echo "images/index_05.gif"?>></td>
<td>
<img src="images/spacer.gif" width="1" height="27" alt=""></td>
</tr>
<tr>
<td colspan="2" width="641" height="15" align="center" valign="bottom">

</html>

[edited by: eelixduppy at 6:45 am (utc) on July 23, 2007]
[edit reason] use example.com, please [/edit]

Habtom

6:09 am on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are almost there, but the following part needs a little bit of modification.

if (isset($submit)){
$message_error = "";

if (ereg("^[[:alpha:]]+$", $name)) {
$message_error .= "Please enter your name.\n";
}

if (ereg("^([^@]+)@([a-z\-]+\.)+([a-z]{2,4})$", $email)) {
$message_error .= "Invalid email, please try again.\n";
}

if (ereg("^[[:alpha:]]+[[:digit:]]*$", $message)) {
$message_error .= "Please enter a message.";
}

if ($message_error <> "") {
exit();
}

}

Changes:

1. The submit if condition should extend down to all the other conditions.
2. This part:if (ereg("^[[:alpha:]]+[[:digit:]]*$", $message)) {
$message_error .= "Please enter a message.";
wasn't closed by the brackets.
3. You need to actually exit it somehow for the form not to continue submitting. That is what I did there.

Habtom

livetoride

2:00 pm on Jul 22, 2007 (gmt 0)



Thanks, I'm gonna try this, hopefully it works.

livetoride

2:18 pm on Jul 22, 2007 (gmt 0)



Thanks for the help with the if statements, they work now and the page displays correctly. The only thing is after the form has been submitted, my Location header won't work. Is there another way to redirect to a thanks page without putting a header so far down in the script. It tells me I can't modify the headers.

justgowithit

3:50 pm on Jul 22, 2007 (gmt 0)

10+ Year Member



You can't adjust headers after output has already been sent to the browser. You need to utilize output control functions [us.php.net] to achieve the redirect result that your after.

livetoride

4:23 pm on Jul 22, 2007 (gmt 0)



thanks again!