|
Posted by Tyrone Slothrop on 10/02/05 05:45
On Sat, 01 Oct 2005 18:20:51 +0200, Sebastian Hansen
<sebasti.hansen@gmail.com> wrote:
>Hi!
>
>I have made a simple contact form for my website, with the function to
>stop the php script to make sure it's not sent before all fields are
>filled out.
>
>The script work fine, but if you don't fill out all the fields, the rest
>of the html code on the site gets pretty weird. I have tried the die
>function and the exit() function, but the same result. The text that
>tells that the field needs to be filled out, are showing, but as said,
>the html code on the rest of the site gets pretty misplaced.
>
>I also wonder how I can put a link in the echo"" function, so that it
>will print a fully functional link?
>
>I have posted the code under. I'm very greatful for any help on this
>subject.
>
>** Form.php **
>
>
><form name="form1" method="post" action="contact_form_sendmail.php">
> <br>
> <span class="style1">+</span> Name: <br>
> <input name="name" id="name" size="20" style="float: left">
> <br>
> <br>
> <br>
> <span class="style1">+</span> E-post: <br>
> <input name="mail" id="mail" size="20" style="float: left">
> <br>
> <br>
> <br>
> <span class="style1">+</span> Comment: <br>
> <textarea name="comment" cols="16" rows="3" id="textarea2"></textarea>
>
>
>
> <br>
> <input type="image" src="img/contact/button_contact_send.gif"
>name="Submit" value="Submit" width="48" height="23" alt="send">
></form>
>
>
>
>** contact_form_sendmail.php **
>
><?
>$name = $_POST['name'];
>$mail = $_POST['mail'];
>$comment = $_POST['comment'];
>
>if(!$name)
>{
>echo "<b>Name field has to be filled out</b><br>";
>die;
>}
>
>if(!$mail)
>{
>echo "<b>E-post field has to be filled out</b><br>";
>die;
>}
>
>if(!$comment)
>{
>echo "<b>Comment field has to be filled out</b><br>";
>die;
>}
>
>
>mail("mymail@mymail.com", "$name", "$mail", "$comment");
>
>echo "<b>Comment sent!</b>";
>
>?>
The way I approach these things is more like this:
<?
$req = array ('name','email','comment');
$err_msg = array();
foreach ($_POST as $k=>$v) {
if (in_array ($k, $req)) {
$err_msg[] = ucfirst($k)." is a required field.";
}
}
if (is_empty($err_msg) {
// do your mail here
exit;
}
?>
<!--Place the errors here-->
<? if (!is_empty($err_msg)) {
<div>Your submission had the following errors:</div>
<ul>
<? foreach ($err_msg as $err) { ?>
<li><?=$err?></li>
<? } ?>
</ul>
<? } ?>
<!--And here is your form-->
Just a warning: I did not check this for parse errs.
[Back to original message]
|