Okay...
What I want seems really complicated to me but it's probablky not so hard...
Okay. I have a HTML Form...
Someone types in their email, name, etc and chooses something from a dropdown menu or radio buttons...
That ^ I can do...
Then, a PHP Script sends an email to the email address provided showing all the other details the person put in with a link to confirm it.
When they click that link two things happen:
An email is sent to me with all the persons info,
And All their info is stored in a MySQL Database.
How the hell do I do all that?
Comments
perhaps if i get some spare time ill give it a go
what fields do you want the database to have, otherwords what data are you going to collect altogether?
The Royal Ram
1. Create a html form using some edit fields and a Post button.
2. Name all the edit fields - e.g. the Email field is named "email".
3. For the form, <form name=[YOUR_FORM_NAME] action=[YOUR_PHP_FILE - e.g. return.php] method=post>
4. When the user clicks on the button, [YOUR_PHP_FILE - e.g. return.php] will be called
5. Create a PHP file - e.g. return.php
6. In the PHP file, we need to do a few things
6.a. Check to see if all the mandatory fields are filled in
[Assume your form in your html file has 3 edit fields - name, age, email]
E.g.
if($email==""){
echo "<link href=../styles.css rel=stylesheet type=text/css>
<center>
<table width=300 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=30 align=center bgcolor=#FFFFFF>
<br>Sorry, please enter your email.<br>
Please <a href=[WHERE YOU WANT TO REDIRECT THE USER TO]>click here</a> to try again.<br><br>
</td>
</tr>
</table>
</center>";
exit();
}
6.b. If all fields are filled in properly, continue processing...
[You need to supply $db_host,$db_usr,$db_pwd,$db_name]
[Assume your table has only 3 fields - field1, field2, field3]
$id_link=mysql_connect($db_host,$db_usr,$db_pwd);
mysql_select_db($db_name);
$q="INSERT INTO [YOUR_TABLE_NAME] (field1,field2,field3) VALUES('$name','$age','$email')";
$r=mysql_query($q);
if(!$r) {
$err=mysql_error();
echo $err;
exit();
}
else {
&msg = "[THE CONTENT OF YOUR EMAIL]"
&sender = "[YOUR EMAIL'S RECIPIENT WILL SEE THIS AS THE SENDER]"
mail("$email","[YOUR_EMAIL_TITLE]","$msg","From: $sender");
}
This is the form I would use:
[code:1:3d4bc2ba4a]
<html>
<head>
<title>Form...</title>
</head>
<body>
<form name="input" action="process.php" method="post">
First Name: <input type="text" name="firstname"><br />
Last Name<input type="text" name="lastname"><br />
eMail:<input type="text" name="email1"><br />
eMail again (to confirm):<input type="text" name="email2"><br />
Payment:<br />
PayPal:<input type="radio" name="sex" value="PayPal"><br />
MasterCard:<input type="radio" name="sex" value="MasterCard"><br />
<input type ="submit" value ="Submit Now!">
</form>
</body>
</html>
[/code:1:3d4bc2ba4a]
Can you try and build the code around that?
1. You've got the form - OK.
2. When the user clicks on the "Submit Now" button, process.php will get called.
3. In, process.php, put the code snippets I posted earlier to check that the user has entered values for the mandatory fields
E.g.
if($email==""){
echo "<link href=../styles.css rel=stylesheet type=text/css>
<center>
<table width=300 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=30 align=center bgcolor=#FFFFFF>
<br>Sorry, please enter your email.<br>
Please <a href=[WHERE YOU WANT TO REDIRECT THE USER TO]>click here</a> to try again.<br><br>
</td>
</tr>
</table>
</center>";
exit();
}
4. In process.php, put the code snippets for inserting data into the database. If not successful, display an error. If successful, send out an email.
$id_link=mysql_connect($db_host,$db_usr,$db_pwd);
mysql_select_db($db_name);
$q="INSERT INTO [YOUR_TABLE_NAME] (field1,field2,field3) VALUES('$name','$age','$email')";
$r=mysql_query($q);
if(!$r) {
$err=mysql_error();
echo $err;
exit();
}
else {
&msg = "[THE CONTENT OF YOUR EMAIL]"
&sender = "[YOUR EMAIL'S RECIPIENT WILL SEE THIS AS THE SENDER]"
mail("$email","[YOUR_EMAIL_TITLE]","$msg","From: $sender");
}
In summary, you need 2 files - you've already got the html file OK. Create another file with a PHP extension and insert the code I posted.
****************
cut the code below
****************
<?php
include_once("config.php"); // if you have a config file
// one check for each field
if($email==""){
echo "<link href=../styles.css rel=stylesheet type=text/css>
<center>
<table width=300 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=30 align=center bgcolor=#FFFFFF>
<br>Sorry, please enter your email.<br>
Please <a href=[WHERE YOU WANT TO REDIRECT THE USER TO]>click here</a> to try again.<br><br>
</td>
</tr>
</table>
</center>";
exit();
}
// more checks if you have multiple mandatory fields
//...
//...
$id_link=mysql_connect($db_host,$db_usr,$db_pwd);
mysql_select_db($db_name);
$q="INSERT INTO [YOUR_TABLE_NAME] (field1,field2,field3) VALUES('$name','$age','$email')";
$r=mysql_query($q);
if(!$r) {
$err=mysql_error();
echo $err;
exit();
}
else {
&msg = "[THE CONTENT OF YOUR EMAIL]"
&sender = "[YOUR EMAIL'S RECIPIENT WILL SEE THIS AS THE SENDER]"
mail("$email","[YOUR_EMAIL_TITLE]","$msg","From: $sender");
}
?>
<html>
// put html code here - this is what the user will see after the PHP portions have been processed
// assuming that there were no errors
// if there were errors the exit(); + echo() functions would have kicked in
</html>
Sorry to double-post.
To make things clearer - there are 2 files
File 1 - html that contains the form
File 2 - php - that contains 2 sections
Section 1 - PHP - the code enclosed by <?php ?>
Section 2 - HTML - after the PHP portions have executed, the user will see the html portion.
:idea:
I did everything you said but I'm waiting for my DNS's to change...
See for yourself at www.kaziaz.com/form.html
That will output it using PHP. notice that theres a slash before the html ". This stops php processng them
The Royal Ram
Please <a href=\"form.html\">click here</a> to try again.<br><br>
Line 37 is: I tried taking out the & and got this:
It's not &.. use $. Replace line 37 with the line below. Take note of the $ and the ;
$msg="Please confirm...";
Line 2 is: I don't have a config file so I deleted the line...
Now it says: even though I am entering my email adress correctly...
You say you entered an email address in your form in the html file. Did you name the email field with the same name as the email variable in the PHP file?
The problem should be trivial. Either you post the relevant sections of the code in this thread or send it to me via email - I'd be glad to have a look at it for you.
form.html:
process.php:
In my form I have email1 and email2
So I guess I need something like:
if $email1 == $email2 send email.file to the $email1
else print "Please check your emails..."
Obviously I'm new to PHP... lol
In your html file you got 2 fields with the names email1 and email2. These fields get converted into variables in your PHP file - $email1 and $email2.
As such, when you perform mandatory field checking, you need to specify the conditions:
if $email1==""{
complain to the user
exit();
}
if $email2==""{
complain to the user
exit();
}
if $email1==$email2 {
send email
}
else {
complain to the user
exit();
}
Your mistake is that you used $email. There is no such field in you HTML file and as such PHP created a new variable which is initialized to "".
So what exactly do I add to the php file, and where exactly do I add it?
1. I've removed the line for config.php since you mentioned that you do not have one
2. I've only done checking for emails - you may want to extend the checking for you other input fields in the form - e.g. name, etc.
3. There is a more efficient way of coding the checking but the one I've set out below should be the easiest to understand.
******************
copy below
******************
<?php
if($email1==""){
echo "<link href=../styles.css rel=stylesheet type=text/css>
<center>
<table width=300 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=30 align=center bgcolor=#FFFFFF>
<br>Sorry, please fill in both email fields.<br>
Please <a href=\"form.html\">click here</a> to try again.<br><br>
</td>
</tr>
</table>
</center>";
exit();
}
if($email2==""){
echo "<link href=../styles.css rel=stylesheet type=text/css>
<center>
<table width=300 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=30 align=center bgcolor=#FFFFFF>
<br>Sorry, please fill in both email fields.<br>
Please <a href=\"form.html\">click here</a> to try again.<br><br>
</td>
</tr>
</table>
</center>";
exit();
}
if($email1==$email2){
$id_link=mysql_connect($db_host,$db_usr,$db_pwd);
mysql_select_db($db_name);
$q="INSERT INTO xxx (field1,field2,field3) VALUES('$name','$age','$email')";
$r=mysql_query($q);
if(!$r) {
$err=mysql_error();
echo $err;
exit();
}
else {
&msg = "Please confirm...";
&sender = "SENDER999";
mail("$email","Title999","$msg","From: $sender");
}
}
else {
echo "<link href=../styles.css rel=stylesheet type=text/css>
<center>
<table width=300 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=30 align=center bgcolor=#FFFFFF>
<br>Sorry, please ensure that both email values are the same.<br>
Please <a href=\"form.html\">click here</a> to try again.<br><br>
</td>
</tr>
</table>
</center>";
exit();
}
?>
<html>
<head><title>Process---</title></head>
<body>
<h2>I like Candy!</h2>
<br /><br />
// put html code here - this is what the user will see after the PHP portions have been processed
// assuming that there were no errors
// if there were errors the exit(); + echo() functions would have kicked in
</body>
</html>
I can guess what most of it should be:
What do I need to do with the stuff in bold?
when posting code post it using the code tags.
Example:
The Royal Ram
2. $db_host is the name of your host - e.g. bimmercenter.com
So you can either:
or
3. You need to create a table in your MySQL database. E.g. your database's name is CBG_DB and your table's name is CBG_TBL.
So you can either:
or
4. field1, field2, field3 etc. are the fields you create in your table. If you created a table called CBG_TBL with 3 fields email, firstname, lastname, then:
$email, $firstname and $lastname need to be input fields of your form.
I'll test all that now!