PDA

View Full Version : Contact form using php



biomike
12-10-2008, 05:04 PM
I want to add a contact form on to my website that enables new enquires to submit an enquiry.

i only need name, email and a comments box

thanks

michael

hmsnacker123
12-11-2008, 10:28 AM
As you did not specify how you would like to view the comments etc. i have done it with the mail function and mysql:



<?php
// Thomas Golding (hmsnacker123) (C) 2008

if(!$_POST['submit']){
echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\">\n";
echo "<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
echo "<tr><td>Name:&nbsp;</td><td><input type=\"text\" name=\"name\" /></td></tr>\n";
echo "<tr><td>Email:&nbsp;</td><td><input type=\"text\" name=\"email\" /></td></tr>\n";
echo "<tr><td>Comments:&nbsp;</td><td><textarea name=\"comments\" cols=\"40\" rows=\"5\"></textarea></td></tr>\n";
echo "<tr><td>Type:</td><td><select name=\"type\"><option value=\"1\">mail()</option><option value=\"2\">mysql</option></select></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Submit\" /></td></tr>\n";
echo "</form>\n";
echo "</table>";
}else {
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$type = $_POST['type'];

if($name && $email && $comments && $type){
if($type == 1){
//If you choose the mail function
$to = "youremail@yourserver.com";
$subject = "Contact form";
$message = "You have comments off ".$name.", His/Her email is ".$email.", The comments are: ".$comments."";
mail($to, $subject, $message);
}
if($type == 2){
$conn = mysql_connect('localhost','username','password');
mysql_select_db('dbname', $conn);
$sql = "INSERT INTO table_name (`name`,`email`,`comments`) VALUES ('$name','$email','$comments')";
$res = mysql_query($sql) or die(mysql_error());
}

}else {
echo "Please supply all the fields.";
}



}


?>


If the method is other than that in the script, Please tell me and I will modify the script to your requirements.

maneetpuri
12-11-2008, 12:22 PM
I want to add a contact form on to my website that enables new enquires to submit an enquiry.

i only need name, email and a comments box

thanks

michael

Hi,

You will have to first create a form with these fields and then download formmail.php and link to this form. If you will type ‘download formmail.php’ on Google you will get this file along with the instructions for how to use it.

Cheers,

~Maneet Puri
Lexolution IT Services
Web Development Company (http://www.lexolutionit.com)

prasanthmj
12-12-2008, 07:33 PM
The following pages might help
Email Form 'HowTo's (http://www.html-form-guide.com/email-form/)
PHP Form to email (http://www.html-form-guide.com/email-form/php-form-to-email.html)

biomike
12-13-2008, 01:22 PM
Thanks hmsnacker123

It's not working. I am not receiving the email with the details. Also when you submit the form you then go to a blank page plus i was doing it in firefox and the blank page appeared on IE.

I have created the following form in my html document


<!-- STEP 1: Put the full URL to formmail.php on your website in the 'action' value. -->
<form method="post" action="http://www.bioenergyhealing.org.uk/formmail.php" name="QuestionForm">
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER">
<!-- STEP 2: Put your email address in the 'recipients' value.
Note that you also have to allow this email address in the
$TARGET_EMAIL setting within formmail.php!
-->
<input type="hidden" name="recipients" value="msc@bioenergyhealing.org.uk" />
<!-- STEP 3: Specify required fields in the 'required' value -->
<input type="hidden" name="required" value="email:Your email address,name:Your name" />
<!-- STEP 4: Put your subject line in the 'subject' value. -->
<input type="hidden" name="subject" value="Question FormMail" />

<font class=content>
<p style="margin-top: 0; margin-bottom: 0" align="center"><strong><font face="Arial" size="2">Directions:</font></strong><font face="Arial" size="2">
Please type in your question below, then click the&nbsp;</font></p>
<p style="margin-top: 0; margin-bottom: 0" align="center"><font face="Arial" size="2">
<strong>“Here's my question, Michael!”</strong> button below.</font></p>
</font>

<table border="0" cellspacing="5%">
<tr>
<td valign="top" colspan="2">

<font class=content>
<textarea name="mesg" rows="10" cols="50"></textarea>
</font>
</td>
</tr>
<tr>

<font class=content>
<td>
<p><font size="3" face="Arial">Please enter your name:</font></p>
</td>
<td><font size="3" face="Arial"><input type="text" name="name" />
</font>
</td>
</font>
</tr>
<tr>

<font class=content>
<td>
<p><font size="3" face="Arial">Please enter your email address:</font></p>
</td>
<td><font size="3" face="Arial"><input type="text" name="email" />
</font>
</td>
</font>
</tr>
<tr>
<td colspan="2" align="center">
<table border="1" bordercolor="#000000" bgcolor="#EC4D00">
<tr>
<td bgcolor="#EC4D00" bordercolor="#EC4D00">

<font class=content>
<input type="submit" value="Here's my question Michael!" />
</font>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>

I have updated your php code as follows called formmail.php.


<?php
// Thomas Golding (hmsnacker123) (C) 2008

if(!$_POST['submit']){
echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\">\n";
echo "<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
echo "<tr><td>Name:&nbsp;</td><td><input type=\"text\" name=\"name\" /></td></tr>\n";
echo "<tr><td>Email:&nbsp;</td><td><input type=\"text\" name=\"email\" /></td></tr>\n";
echo "<tr><td>Question:&nbsp;</td><td><textarea name=\"mesg\" cols=\"40\"

rows=\"5\"></textarea></td></tr>\n";
echo "<tr><td>Type:</td><td><select name=\"type\"><option value=\"1\">mail()</option><option

value=\"2\">mysql</option></select></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Here's my

question Michael!\" /></td></tr>\n";
echo "</form>\n";
echo "</table>";
}else {
$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['question'];
$type = $_POST['type'];

if($name && $email && $question && $type){
if($type == 1){
//If you choose the mail function
$to = "msc@bioenergyhealing.org.uk";
$subject = "question form";
$message = "You have questions ".$name.", His/Her email is ".$email.", The question are:

".$question."";
mail($to, $subject, $message);
}

}


?>

Thanks Michael

hmsnacker123
12-13-2008, 03:53 PM
OK:

You might not have php mail set up.. use google to help you with this,

as for the script, Try this out and tell me if it helps you:



<?php
// Thomas Golding (hmsnacker123) (C) 2008

ob_start();

if(!$_POST['submit']){

if(isset($_GET['msg']))
echo $_GET['msg'];

echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\">\n";
echo "<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
echo "<tr><td>Name:&nbsp;</td><td><input type=\"text\" name=\"name\" /></td></tr>\n";
echo "<tr><td>Email:&nbsp;</td><td><input type=\"text\" name=\"email\" /></td></tr>\n";
echo "<tr><td>Question:&nbsp;</td><td><textarea name=\"mesg\" cols=\"40\" rows=\"5\"></textarea></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Here's my question Michael!\" /></td></tr>\n";
echo "</form>\n";
echo "</table>";
}else {
$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['mesg'];

if($name && $email && $question){
//If you choose the mail function
$to = "msc@bioenergyhealing.org.uk";
$subject = "question form";
$message = "You have questions ".$name.", His/Her email is ".$email.", The question are:".$question."";
mail($to, $subject, $message);
}
}

ob_end_flush();

?>

biomike
12-13-2008, 04:17 PM
Were getting there. I am know getting the email with the question. However the form is on the php page when i need it on the html page http://www.bioenergyhealing.org.uk/ask_a_question3. This way the visitor doesn't have to go to another page to submit.

any suggestions

hmsnacker123
12-13-2008, 10:27 PM
Ok well on the HTML page which is http://www.bioenergyhealing.org.uk/ask_a_question3 (I Think), I have copied the HTML for the form out of the source and changed a couple of things:


<form method="post" action="http://www.bioenergyhealing.org.uk/formmail.php" name="QuestionForm">
<font class=content>
<p style="margin-top: 0; margin-bottom: 0" align="center"><strong><font face="Arial" size="2">Directions:</font></strong><font face="Arial" size="2">

Please type in your question below, then click the&nbsp;</font></p>
<p style="margin-top: 0; margin-bottom: 0" align="center"><font face="Arial" size="2">
<strong>“Here's my question, Michael!”</strong> button below.</font></p>
</font>

<table border="0" cellspacing="5%">
<tr>

<td valign="top" colspan="2">

<font class=content>
<textarea name="mesg" rows="10" cols="50"></textarea>
</font>
</td>
</tr>
<tr>

<font class=content>
<td>

<p><font size="3" face="Arial">Please enter your name:</font></p>
</td>
<td><font size="3" face="Arial"><input type="text" name="name" />
</font>
</td>
</font>
</tr>
<tr>

<font class=content>
<td>
<p><font size="3" face="Arial">Please enter your email address:</font></p>
</td>
<td><font size="3" face="Arial"><input type="text" name="email" />
</font>
</td>
</font>

</tr>
<tr>
<td colspan="2" align="center">
<table border="1" bordercolor="#000000" bgcolor="#EC4D00">
<tr>
<td bgcolor="#EC4D00" bordercolor="#EC4D00">

<font class=content>
<input type="submit" name="submit" value="Here's my question Michael!" />
</font>

</td>
</tr>
</table>
</td>
</tr>
</table>
</form>



and in formail.php put:



<?php
// Thomas Golding (hmsnacker123) (C) 2008
ob_start();
if($_POST['submit']){

$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['mesg'];

if($name && $email && $question){
//If you choose the mail function
$to = "msc@bioenergyhealing.org.uk";
$subject = "Question FormMail";
$message = "You have questions ".$name.", His/Her email is ".$email.", The question are:".$question."";
mail($to, $subject, $message);
header("Location: yoursuccesspage.html");

}else {
echo "Please supply all the fields";
}
}else {
echo "Improper use of this file.";
}
ob_end_flush();

?>


Hope that helps.

biomike
12-15-2008, 11:31 PM
thanks hmsnacker123 for all your help it works.

hmsnacker123
12-16-2008, 02:58 PM
Thats fine, Please thank me :)

biomike
12-17-2008, 10:57 AM
Thanks

Snookerman
12-17-2008, 11:10 AM
Thats fine, Please thank me :)

:D lol

owt200x
12-17-2008, 06:54 PM
I could integrate my form processing code into your form
check out my form demo at http://www.owt200x.us/contactform

hmsnacker123
12-26-2008, 03:25 AM
Thanks

Meh.. lol