Jump to content

Store form data into mysql database, then email same form data


Recommended Posts

I have a form on our website that a user can fill out for custom product.  I want the form data to be 1) stored into a mysql database AND after storing said data, 2) email the same data to our sales department.

 

1)  The form data DOES get stored into mysql database (except for the first two fields, for some weird reason)

 

2) I added a "mail" section to the php file that stores the data into the database, but it is not working correctly.  I have stripped the email portion down to sending just one of the fields in the "message" to make it easier for troubleshooting

 

I have included here, both the form section of the html file, and the formdata.php file that processes the data for your  analysis.  I am relatively new to php so there are going to be some issues with security, but I can work on those after I get the store & email process to work correctly.  Please review my code and see if anyone can be of assistance.  I looked through the forums and couldn't find another issue that was the same as mine.  If I just overlooked, please tell me the thread post #.  Thanks :)

 

THE FORM WHICH COLLECTS THE DATA

*******************************

<form method=POST action=formdata.php>

                  
  <table width="640" border=0 align="center">
    <tr> 
      <td align=right><b>First Name</b></td>
      <td><input type=text name=FName size=25></td>
      <td><div align="right"><b>Telephone</b></div></td>
      <td><input type=text name=Tel size=25></td>
    </tr>
    <tr> 
      <td align=right><b>Last Name</b></td>
      <td><input type=text name=LName size=25></td>
      <td><div align="right"><b>Fax</b></div></td>
      <td><input type=text name=Fax size=25></td>
    </tr>
    <tr> 
      <td align=right><b>Title</b></td>
      <td><input type=text name=Title size=25></td>
      <td><div align="right"><b>Email</b></div></td>
      <td><input type=text name=Email size=50></td>
    </tr>
    <tr> 
      <td align=right><b>Company</b></td>
      <td><input type=text name=Comp size=25></td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right><b>Address</b></td>
      <td><input type=text name=Addr size=25></td>
      <td><div align="right"><b>Estimated Annual Volume</b></div></td>
      <td><input type=text name=EAV size=25></td>
    </tr>
    <tr> 
      <td align=right><b>City</b></td>
      <td><input type=text name=City size=25></td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right><b>State/Province</b></td>
      <td><input type=text name=SProv size=25></td>
      <td><div align="right"><b>Application</b></div></td>
      <td><input type=text name=Appl size=25></td>
    </tr>
    <tr> 
      <td align=right><b>Country</b></td>
      <td><input type=text name=Ctry size=25></td>
      <td><div align="right"><b>Type of System</b></div></td>
      <td><input type=text name=Syst size=25></td>
    </tr>
    <tr> 
      <td align=right><b>Zip/Postal Code</b></td>
      <td><input type=text name=ZPC size=25></td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td><div align="right"><strong><font color="#FFFF00" face="Arial, Helvetica, sans-serif">COIL 
          DESIGN</font></strong></div></td>
      <td><font color="#FFFF00" face="Arial, Helvetica, sans-serif"><strong>PARAMETERS</strong></font></td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right><b>Primary Resistance (ohms)</b></td>
      <td><input type=text name=Pres size=25></td>
      <td><div align="right"><b>Primary Inductance (mH)</b></div></td>
      <td><input type=text name=Pind size=25></td>
    </tr>
    <tr> 
      <td align=right><b>Secondary Resistance (ohms)</b></td>
      <td><input type=text name=Sres size=25></td>
      <td><div align="right"><b>Secondary Inductance (H)</b></div></td>
      <td><input type=text name=Sind size=25></td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right><b>Peak Operating Current (Amps)</b></td>
      <td><input type=text name=POC size=25></td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right><b>Output Energy (mJ)</b></td>
      <td><input type=text name=Egy size=25></td>
      <td><div align="right"><b>Output Voltage (kV)</b></div></td>
      <td><input type=text name=Volt size=25></td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right><b># HV Towers per Coil</b></td>
      <td><input type=text name=TPC size=25></td>
      <td><div align="right"><b># of Coils per Package</b></div></td>
      <td><input type=text name=CPP size=25></td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td align=right> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <th colspan=4><b>Please enter any additional information here:</b></th>
    </tr>
    <tr> 
      <th colspan=4><textarea name=Mess cols=50 rows=10 id="Message"></textarea></th>
    </tr>
  </table>
  </dl>
  <div align="center">
    <p>
      <input type=hidden name=BodyTag value="<body  bgcolor="#484589" text="#FFFFFF" link="#FFFF00" alink="#FFFFFF" vlink="#FF7F00">">
      <input type=hidden name=FA value=SendMail>
    </p>
    <p><font color="#FFFF00" face="Arial, Helvetica, sans-serif"><strong>PLEASE MAKE SURE ALL 
      INFORMATION<br>
      IS CORRECT BEFORE SUBMITTING</strong></font></p>
    <p> 
      <input type=submit value="Submit Form">
    </p>
  </div>
</form>

 

THE FILE THAT PROCESSES THE FORM DATA (formdata.php)

***********************************************

<?php
$con = mysql_connect("localhost","XXX","XXX");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("customform", $con);

$sql="INSERT INTO formdata (Fname, Lname, Title, Comp, Addr, City, SProv, Ctry, ZPC, Tel, Fax, Email, EAV, Appl, Syst, Pres, Pind, Sres, Sind, POC, Egy, Volt, TPC, CPP, Mess)
VALUES ('$_POST[Fname]','$_POST[Lname]','$_POST[Title]','$_POST[Comp]','$_POST[Addr]','$_POST[City]','$_POST[sProv]','$_POST[Ctry]','$_POST[ZPC]','$_POST[Tel]','$_POST[Fax]','$_POST[Email]','$_POST[EAV]','$_POST[Appl]','$_POST[syst]','$_POST[Pres]','$_POST[Pind]','$_POST[sres]','$_POST[sind]','$_POST[POC]','$_POST[Egy]','$_POST[Volt]','$_POST[TPC]','$_POST[CPP]','$_POST[Mess]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your Information Was Successfully Posted";

mysql_close($con);

$to = "recipient email address here"; 
$subject = "Custom Form"; 
$email = $_POST['Email'] ; 
$message = $_POST['Comp'] ; 
$headers = "From: $Email"; 
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) 
{print "Your mail was sent successfully"; }
else 
{print "We encountered an error sending your mail"; }
?>

To make it a lil easier to troubleshoot and secure, set all your post items to variables like so..

$Fname = $_POST['Fname'];

//since you are inserting into a database I recommend at least adding slashes to each var your going to insert like so.
$Fname = addslashes($_POST['Fname']);

 

Once you do that, you can echo all your variables on the PHP script and see what's actually going on. Let me know if that helps at all.

a few basic questions.... which platform is your server?... *NIX (any unix flavor) or Windows? ...

 

- if *NIX... did you test that sendmail is installed/configured and working?

- if Windows... did you configure your php.ini with the proper SMTP server?

To make it a lil easier to troubleshoot and secure, set all your post items to variables like so..

$Fname = $_POST['Fname'];

//since you are inserting into a database I recommend at least adding slashes to each var your going to insert like so.
$Fname = addslashes($_POST['Fname']);

 

Once you do that, you can echo all your variables on the PHP script and see what's actually going on. Let me know if that helps at all.

 

So you are saying that I should declare ALL my form post items as variables in this manner *$Fname = addslashes($_POST['Fname']);* for better security?  OK.  I will work on this later this evening and get back to you.  Thanks.

a few basic questions.... which platform is your server?... *NIX (any unix flavor) or Windows? ...

 

- if *NIX... did you test that sendmail is installed/configured and working?

- if Windows... did you configure your php.ini with the proper SMTP server?

 

Our server is Windows Server 2003 with IIS 7.  Yes, I know that the mail works because I tested it.  I think I just have typical newbie mistakes in my code, but since I am new to php, I haven't yet figured out what those are :)

Yes for security, but you can also use these a little easier for verifying all the data is being passed properly.

 

I haven't had a chance to make these changes yet, but I will work it in this evening.  Any idea about why the email doesn't send??

I always use mail headers when sending mail through PHP. I am not familiar with IIS, but with apache I've done something like this before.

 

$to = 'you@example.com';
$subject = "Subject";
$headers = "From: me@example.com\r\nReply-To: me@example.com";
$message = "Hi This is a test message";
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";

 

Let me know if that helps..

 

This issue was resolved on another forum, but I figured I should post the solution here as well for anyone else who runs into this same problem.  It all amounts to case-sensitivity, whereas in one section of code I have capital letters in the variable name, and lower-case in another section.  I looked at this at least 2 dozen times and didn't catch it.  This is where having another set of eyes (or many sets hehe) can spot a problem that is right in front of our nose :)  Thanks to all who replied with tips and/or suggestions!

 

 

The actual forum post link is here:

 

http://www.daniweb.com/forums/post1326332.html#post1326332

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.