Simple php for contact form, a couple of simple q's

Hi again

This is some code for a simple contact us page.


<?php
// contact email script

//contact subject

$subjecttitle = "$subject";

$message = "$details";

$name = "$name";  //do I need to this here as its in the header...?

$mail_from = "$customer_email"

$header = "from: $name <$mail_from>";

//send to my address

$to = "my email";

$send_message = mail($to,$subjecttitle,$message,$header);

//check mail went through ok

if (isset)($send_message){
    echo "Thankyou for your message. 
}

else{
    echo "Mmm there seems to have been a problem somewhere along the line. Please press the back button and try and send it again. 
}
 
//if the problem does carry on find out how can I send a different message the second time it doesn't work

?>

The variables at the top of the code are equal to variables of similar names. These similar name variables are actually the names of the different fields in the HTML. I have placed them in variables aswell but was wondering could I just put them as strings…?

e.g


$subjecttitle = "subject";

$message = "details";

$name = "name";

$mail_from = "customer_email"


To make this form better I guess I have to validate if the correct characters were used for the diffefrent type of fields. I have been reading of something called “clean string”, would this be good to use on this form too…?

If anyone can advise me what’s missing for this form so I can go away and make it fully functional and ready to add to my site I really would be grateful.

The code youve got there wont get the variables out of the HTML form after submission.

To get variables from the form you need to use


$variable_name = $_POST['name_in_form'];

Yes you need to make sure that the characters you pass to the mail function have been cleansed to stop people hijacking your contact form.

Thanks

Ok Ill get to work and post the edited new code hoepfully done correctly :slight_smile:

Anything else to think about while Im writing it…?

You might want to terminate that string with a " and ; on the end or your script will start moaning about unterminated/unexpected strings.

Yeh just noticed that thanks man

At the moment Im planning what parts to write next for this. So far Ive come up with incorporating a captcha, an array in an if statement to check everything has been entered in the boxes, and cleaning the strings.

Any other good ideas I could add to the script…?

At the moment its looking v.messy and not making much sense but Ill post it as soon as Ive written it all.

Well really it depends on what you’re doing. Even some of the best scripts available can’t use each and every idea out there. If its just a contact form then what you have sounds good so far.