Update email form handler also upload to database

Hey!
okay…here’s an existing form handler that I’m using. Currently, this file emails me the details of the form submission.

I need it to adjust it to:

  1. attached this to the database, without compromising the email functionality of the handler.
  2. upload the info from my form to the database.
  3. Add a variable to the confirmation email that includes a confirmation ID that are in sequential order (1001 on the first form submission, 1002 for the second submission, etc).

Can someone help me make the necessary adjustments to accomplish this? I’ve poked around on the internet for help, but haven’t found something includes info about how to get a confirmation # to do what I need it to do. (I have created a database, but have not created any tables in it yet).

[php]<?php
$to = ‘myemail’;
$subject = "New Request from " . strip_tags($_POST[‘name’]) . “” . strip_tags($_POST[‘ConfirmationID’]) . “”;
$url = ‘https://www.mydomain.html’;
$headers .= “From: myemail\r\n”;
$headers .= “CC: myemail\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;
$message .= ‘’;
$message .= "

words here…
more words…

Full Name: " . strip_tags($_POST[‘name’]) . "

Phone Number: " . strip_tags($_POST[‘phone’]) . "
Email: " . strip_tags($_POST[‘mail’]) . "
Address: " . strip_tags($_POST[‘address’]) . "
City, State ZIP: " . strip_tags($_POST[‘city’]) . “

”;

mail($to, $subject, $message, $headers);

$message = “”;
$headers = “”;

$to = $_POST[‘Email’];
$subject = ‘Confirmation’;
$headers .= “From: myemail\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;
$message .= ‘’;
$message .="another message here…you’re confirmation ID is " . strip_tags($_POST[‘ConfirmationID’]) . “”;
mail($to, $subject, $message, $headers);

echo ‘’;
?>

[/php]

Thanks!

//Psuedo Code
// Table should have auto-increment field starting with the confirmation ID number one less than one you want to start at

Submit form
connect to database
insert PDO or mysqli prepared statement data to db
get mysql last insert id (which is your confirmation ID)
send email with the last insert id

Okay…trying this a different way.

[php]<?php
if(isset($_POST[‘reg’])) {
require “dbconn.php”;
if(isset($_POST[‘fname’]) && isset($_POST[‘email’])) {
$fname = strip_tags($_POST[‘fname’]);
$phone = strip_tags($_POST[‘phone’]);
$commail = strip_tags($_POST[‘commail’]);
$address = strip_tags($_POST[‘address’]);
$city = strip_tags($_POST[‘city’]);
if($fname==’’){
header(‘location:./reg.php?error=full name empty’);
exit();
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header(‘location:reg.php?error=email not valid’);
exit();
}
mysql_query(“INSERT INTO partners(fname,phone,commail,address,city) VALUES (’$fname’,’$phone’,’$commail’,’$address’,’$city’)”) or die("".mysql_error());
echo “Registrations successful xxx”; //I want to echo the user ID field from the record that was created here!
}
}
else {
header(‘location:reg.php’);
exit();
}
?>[/php]

On line 19, that’s where I want the confirmation ID to pop up…what syntax do I use to achieve that? just posting the variable didn’t seen to work. Assuming I need some call to action before it.

Also, I created the field in the db as auto-increment, but where do I put the “starting” value…or do I just manually create a “fake” record and then the next “real” submission will be the right #?

If you do the database insert AFTER you send the email, you wont be able to email the Confirmation id but you can still display it.

To set your Auto increment:
ALTER TABLE yourtablenamehere AUTO_INCREMENT = 1000;

After your insert:

$id = mysql_insert_id();
echo “Registrations successful $id”;

One other thing, you/everyone needs to STOP using the old mysql calls and start using PDO or mysqli with prepared statements. Either one is super easy to use.

Alright…somethings going wrong with my script. When I do a test, it appears to process through and I don’t get any errors, then it redirects to a page that can’t be loaded (it doesn’t echo at all). I’ve eliminated the email part of the chain just to see if that was causing the issue, and it’s not that.

I check the database, and there are no records uploaded (browsing returns no results).

Thoughts?

Here’s the complete script.
[php]<?php
if(isset($_POST[‘reg’])) {
require “dbconn.php”;
if(isset($_POST[‘fname’]) && isset($_POST[‘email’])) {
$fname = strip_tags($_POST[‘fname’]);
$phone = strip_tags($_POST[‘phone’]);
$commail = strip_tags($_POST[‘commail’]);
$address = strip_tags($_POST[‘address’]);
$city = strip_tags($_POST[‘city’]);
if($fname==’’){
header(‘location:./reg.php?error=full name empty’);
exit();
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header(‘location:reg.php?error=email not valid’);
exit();
}
mysql_query(“INSERT INTO partners(fname,phone,commail,address,city) VALUES (’$fname’,’$phone’,’$commail’,’$address’,’$city’)”) or die("".mysql_error());
$repid = mysql_insert_id();
echo “Registrations successful $repid”; //I want to echo the user ID field from the record that was created here!
}
}
else {
header(‘location:reg.php’);
exit();
}
?>[/php]

First see what your form is actually posting.

At the start of your script add:

print_r($_POST);

Make sure the form is actually posting the form data and that the data is correct. Better yet, post all your files as an attachment. I can give you the right answer much quicker if I can run your script.

Form from index.php…

[php]<?php
if(isset($_GET[‘error’]))
echo $_GET[‘error’];
?>

uSwipe - Sign Me Up!
<tr>
  <td class="fieldquestion">&nbsp;</td>
  <td class="fieldquestion"><div align="left">Phone Number</div></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><span id="sprytextfield3">
  <div align="center">
    <input name="phone" type="text" class="fieldlg2" id="phone" />
  </div>
  <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
  <td>&nbsp;</td>
</tr>

<tr>
  <td class="fieldquestion">&nbsp;</td>
  <td class="fieldquestion"><div align="left">Email Address<span class="style8"></span></div></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><span id="sprytextfield4">
  <div align="center">
    <input name="commail" type="text" class="fieldlg2" id="commail" />
  </div>
  <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
  <td>&nbsp;</td>
</tr>

<tr>
  <td class="fieldquestion">&nbsp;</td>
  <td class="fieldquestion"><div align="left">Address<span class="style8"></span></div></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><span id="sprytextfield6">
    <div align="center">
      <input name="address" type="text" class="fieldlg2" id="mail4" />
    </div>
    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><span id="sprytextfield7">
    <div align="center">
      <input name="city" type="text" class="fieldlg2" id="mail5" />
    </div>
    <span class="textfieldRequiredMsg">A value is required.</span></span></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td colspan="3" class="style1">words...</td>
  </tr>
<tr>
  <td colspan="3" class="fieldquestion2">more words here</td>
  </tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><div align="center">
    <input type="submit" name="submit" id="submit" value="Register Now" />
  </div></td>
  <td>&nbsp;</td>
</tr>

instructions go here

 
Please complete the below information to register.
     
 
Your Full Name
 
 
A value is required.
 
some words here...
[/php]

that should talk to regproc.php…

[php]<?php
if(isset($_POST[‘reg’])) {
require “dbconn.php”;
if(isset($_POST[‘fname’]) && isset($_POST[‘email’])) {
$fname = strip_tags($_POST[‘fname’]);
$phone = strip_tags($_POST[‘phone’]);
$commail = strip_tags($_POST[‘commail’]);
$address = strip_tags($_POST[‘address’]);
$city = strip_tags($_POST[‘city’]);
if($fname==’’){
header(‘location:./reg.php?error=full name empty’);
exit();
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header(‘location:reg.php?error=email not valid’);
exit();
}
mysql_query(“INSERT INTO partners(fname,phone,commail,address,city) VALUES (’$fname’,’$phone’,’$commail’,’$address’,’$city’)”) or die("".mysql_error());
$repid = mysql_insert_id();
echo “Registrations successful $repid”;
}
}
else {
header(‘location:reg.php’);
exit();
}
?>[/php]

which references dbconn.php…

[php]<?php
$name=“refpartner”;
$pas=“mypasswordhere”;
$dbname=“refpartner”;
$con=mysql_connect(“hostname”,$name,$pas);
mysql_select_db($dbname,$con);
?>[/php]

Ok, two things right off,

Your form name for the email is named commail, but in your script regproc, you are trying to validate the email with the name email. “email” is not being sent, commail is, which is why it is redirecting.

Secondly, you have your redirects set to reg.php which either does not exist at all, or you did not post it.

From regproc:
[php]if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header(‘location:reg.php?error=email not valid’);
exit();
}[/php]

Change $email to $commail

Same thing here:
[php]if(isset($_POST[‘fname’]) && isset($_POST[‘email’])) { [/php]

Right here you are looking for the post reg but you never send it from your form
[php] if(isset($_POST[‘reg’])) {[/php]

Not a script breaker, but there is no sense making a database connection if you have not passed any validation and are ready to use the database. This line is called too soon:
[php]require “dbconn.php”;[/php]

It should be here:
[php] require “dbconn.php”;
mysql_query(“INSERT INTO partners(fname,phone,commail,address,city) VALUES (’$fname’,’$phone’,’$commail’,’$address’,’$city’)”) or die("".mysql_error());
[/php]

This part should not be above the body tag. You will break your html.
[php]if(isset($_GET[‘error’]))
echo $_GET[‘error’];[/php]

Also, the form is a bad design. You should not be using tables to lay it out. Use CSS.

Additionally, you should “collect” all your errors FIRST then show ALL the errors at once, not one at a time.

Nothing is going in the database because the redirect kicks in before you get to that part.

How can I eliminate the redirect page all together? I don’t want a redirect. I tried just deleted the “location” line, but then when I test it, it just hangs on the regproc.php page and doesn’t do anything. Can I just delete that whole ‘else’ section, or does the syntax (if statement) require it?

Also, I updated the necessary line to say commail instead of email, but it’s still not posting to db. Other ideas?

The whole lines that start with header are all the redirects. Your whole setup needs more fixing than I am able to offer right now.

I suggest you step away from your scripts for a minute and study a few form processing tutorials. At their basic level, there really isn’t much to them. You will do much better for yourself to learn how they work first.

If I have more time tommorow I will check up on you.

Sponsor our Newsletter | Privacy Policy | Terms of Service