Page 1 of 1
newby questions -send mail and redirect from html fomr
Posted: Wed Jun 24, 2009 10:32 am
by Lorib
Hello,
I'm a total newby seeking some help. I created a form to collect contact info and send it to a mySQL db. That part works but I'd like the form to also send an email to the sender and me (so I know the form was completed) and redirect the customer to a thank you page. I don't need an html email just something that says "Thanks for your request, you entered..."
Eventually I'd also like to have a webform that will allow me to edit and delete entry's (like if someone's phone number changes)
Script is attached.
I appreciate any help I can get,
Thanks
Re: newby questions -send mail and redirect from html fomr
Posted: Wed Jun 24, 2009 12:29 pm
by iBroughtCookies
You know, I never got this to work because I was trying to set up PHP mail on my own server.
However, if you already have the whole mail program set up,
this would be a good start:
http://www.php.net/manual/en/function.mail.php
(P.S. I can't find your attachment so I'm going to assume it's not there, but I'm new so I could just NOT know where it is

)
Re: newby questions -send mail and redirect from html fomr
Posted: Wed Jun 24, 2009 1:22 pm
by Eric!
Please take the time to learn how to filter your fields from both SQL injection and email injection.
You can get some ideas from the pieces of code I posted here
viewtopic.php?f=1&t=101854
get the form contact.html
and the script _mail.php
For SQLi, there are lots of functions you can get off the web.
On your admin page, you need to make a login for yourself to protect the page. This way no one else can see it. Are you using a host with Cpanel or something like that? You might be able just to use phpMyAdmin to manage your database and skip having your own form to do it.
Re: newby questions -send mail and redirect from html fomr
Posted: Thu Jun 25, 2009 7:17 am
by Lorib
Hello,
Thank you both for your replies. I'll repost the code.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>contact</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<p>Please, enter your contact information, someone will contact you promptly.</p>
<?php
if (isset($_POST['submit'])) {
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$output_form = 'no';
if (empty($first) || empty($last) || empty($email) || empty($phone)|| empty($company)) {
// We know at least one of the input fields is blank
echo 'Please fill out all of the required information.<br />';
$output_form = 'yes';
}
}
else {
$output_form = 'yes';
}
if (!empty($first) && !empty($last) && !empty($email) && !empty($phone)&& !empty($company)) {
$dbc = mysqli_connect('server', 'username', 'password', 'database')
or die('Error connecting to MySQL server.');
$query = "INSERT INTO contacts (first, last, email, phone, company) VALUES ('$first', '$last', '$email', '$phone','$company')";
mysqli_query($dbc, $query)
or die ('I am sorry, a problem occured. Data not inserted. Please click your back button then try entering the information in a different format (hint:it may be the phone number)');
echo 'Thank you';
mysqli_close($dbc);
}
if ($output_form == 'yes') {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="first">First name:</label>
<input type="text" id="first" name="first" /><br />
<label for="last">Last name:</label>
<input type="text" id="last" name="last" /><br />
<label for="email">Email:</label>
<input type="text" id="email" name="email" /><br />
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" /><br />
<label for="company">Comany:</label>
<input type="text" id="company" name="company" /><br />
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="redirect" value="thankyou.htm">
</form>
<?php
}
?>
</body>
</html>
I'd be happy to make any changes to the DB via PHPmyAdmin but it won't always be me doing it so I need to have the ability for someone else with proper credentials to go in an edit or delete an entry.
I think the links you've provided should help with the email issue, it seems like I just have to set up mail() in the above code.
The redirect doesn't function at all, no error, it just doesn't redirect. Can someone help me with that?
Thanks again,
Lori
Re: newby questions -send mail and redirect from html fomr
Posted: Thu Jun 25, 2009 5:55 pm
by Eric!
Are you talking about line 61? That won't do anything because it is just an input field and you aren't reading the value of it anywhere in your code. If you are done with the mail page and want to send them somewhere else, try
Code: Select all
header("Location: http://domain/thanks.php");
Also don't forget to look up SQL injection too to protect your database in addition to filtering the inputs for email injection.
Re: newby questions -send mail and redirect from html fomr
Posted: Fri Jun 26, 2009 10:28 am
by Lorib
Thanks Eric,
Where would I put that in my code?