...

View Full Version : Basic Form PHP



claire_t
08-13-2012, 03:17 PM
Hi all.

I have just written my first bit of PHP code for a basic contact form on a site that I am building. I basically follow a tutorial off the interweb.

Does the code (HTML and PHP) look right or have I made any mistakes?

PHP


<?php
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$company = $_REQUEST['company'] ;
$query = $_REQUEST['query'] ;

mail( "info@noiz-electronics.co.uk", "Contact request",
$message, "From: $email" );
header( "Location: http://www.noiz-electronics.com/thankyou.html" );
?>


HTML


<div id="formwrapper">

<form method="post" action="sendmail.php">

<label>Name</label>
<input name="name" type="text" size="60" />

<label>Telephone</label>
<input name="phone" type="text" size="60" />

<label>Email</label>
<input name="email" type="text" size="60" />

<label>Company Name</label>
<input name="company" type="text" size="60" />

<label>Company Type</label>
<input name="type" type="text" size="60" />

<label>Your Query</label>
<textarea name="query" cols="60" rows="6"></textarea>
<p/>

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


</form>
</div>


Now from what I can tell from my research I just upload the sendmail.php file to the server at the same level as the contact.html page and that should work right? As long as the server allows php?

Sorry I know these are probably silly questions but this is my first attempt at PHP and I really want to get it working for this site.

Thanks in advance to you lovely PHP gods!
:thumbsup:

tangoforce
08-13-2012, 03:47 PM
Your call to mail() will return a boolean result (true / false) that indicates whether the mail program accepted it.

You should test for that by doing if (mail()) {//something} like this:



if (mail($to, $Subject, $ETC))
{
//Do something
}
else
{
//Do something else
}

claire_t
08-13-2012, 04:12 PM
Thank you for your reply tangoforce.

Would it be possible for you to elaborate on what your saying? or point me in direction to find out. I'm not to sure what you mean.

I have seen other php snippets with the if/else in them but I wasnt sure what they ment.

EDIT
Ok so I have come up with this. Now I'm guessing this will work as if no email or query is given the form will just reset and if the email is there it will submit. is that right?



<?php
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$company = $_REQUEST['company'] ;
$query = $_REQUEST['query'] ;


if (empty($email) || empty($query)) {
header( "Location: http://www.noiz-electronics.com/contact.html" );
}
else {

mail( "info@noiz-electronics.co.uk", "Contact request",
$message, "From: $email" );
header( "Location: http://www.noiz-electronics.com/thankyou.html" );
}
?>


Thanks again

rerryn
08-13-2012, 05:53 PM
I can see a few issues here, but overall it's a good start.
Your if/else syntax looks alright to me, but if the email or query is missing and it redirects, it won't tell the user why it redirected.
You could make it tell the user why by making contact.html into contact.php, just change the extension - the HTML can stay, and then send a variable to it like this :

header("Location: http://www.noiz-electronics.com/contact.php?noemail=1");
Naturally you would have to put the corresponding code into the contact.php e.g.

<div id="formwrapper">
<?php if( @$_GET['noemail'] == 1 ){ echo "<b>No email entered</b>"; } ?>
<form method="post" action="sendmail.php">


The php tags are important here, and the @ symbol will suppress the error that would show if the value was empty. GET values are the ones sent in the URL bar hence using GET instead of REQUEST (on that note you could use POST quite safely in sendmail).

Next you take in a total of 5 variables (name, phone, email, company and query) but only make use of 1 of them - namely email. Then you reference another variable, message, which doesn't exist at this point. My suggestion to make the message variable would be the following :

else {
mail( "info@noiz-electronics.co.uk", "Contact request", $message, "From: $email" );
$message = "Name: $name\n";
$message .= "Phone: $phone\n";
$message .= "Company: $company\n";
$message .= "Query:\n$query";
To elaborate a little but on the things used in that snippet...
The .= sign indicates add to the end of the variable. The \n indicates a newline, as this is email it will be sent in plaintext so \n is used rather than <br>, if you set it up to use HTML email then <br> would work too, but that's a whole other can of worms.

Another suggestion would be to use if/else constructs to only add name, phone and company if they actually exist as they aren't required.

And finally, in the form where you have all the <label> tags, make them into <label for="NAMEHERE"> where you replace NAMEHERE with the name of the <input> tag they belong to. E.g:

<label>Telephone</label>
<input name="phone" type="text" size="60" />
would become

<label for="phone">Telephone</label>
<input name="phone" type="text" size="60" />

I hope this makes sense, good luck!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum