943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2642
  • PHP RSS
Sep 1st, 2010
0

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

Expand Post »
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
*******************************
PHP Syntax (Toggle Plain Text)
  1. <form method=POST action=formdata.php>
  2.  
  3.  
  4. <table width="640" border=0 align="center">
  5. <tr>
  6. <td align=right><b>First Name</b></td>
  7. <td><input type=text name=FName size=25></td>
  8. <td><div align="right"><b>Telephone</b></div></td>
  9. <td><input type=text name=Tel size=25></td>
  10. </tr>
  11. <tr>
  12. <td align=right><b>Last Name</b></td>
  13. <td><input type=text name=LName size=25></td>
  14. <td><div align="right"><b>Fax</b></div></td>
  15. <td><input type=text name=Fax size=25></td>
  16. </tr>
  17. <tr>
  18. <td align=right><b>Title</b></td>
  19. <td><input type=text name=Title size=25></td>
  20. <td><div align="right"><b>Email</b></div></td>
  21. <td><input type=text name=Email size=50></td>
  22. </tr>
  23. <tr>
  24. <td align=right><b>Company</b></td>
  25. <td><input type=text name=Comp size=25></td>
  26. <td>&nbsp;</td>
  27. <td>&nbsp;</td>
  28. </tr>
  29. <tr>
  30. <td align=right><b>Address</b></td>
  31. <td><input type=text name=Addr size=25></td>
  32. <td><div align="right"><b>Estimated Annual Volume</b></div></td>
  33. <td><input type=text name=EAV size=25></td>
  34. </tr>
  35. <tr>
  36. <td align=right><b>City</b></td>
  37. <td><input type=text name=City size=25></td>
  38. <td>&nbsp;</td>
  39. <td>&nbsp;</td>
  40. </tr>
  41. <tr>
  42. <td align=right><b>State/Province</b></td>
  43. <td><input type=text name=SProv size=25></td>
  44. <td><div align="right"><b>Application</b></div></td>
  45. <td><input type=text name=Appl size=25></td>
  46. </tr>
  47. <tr>
  48. <td align=right><b>Country</b></td>
  49. <td><input type=text name=Ctry size=25></td>
  50. <td><div align="right"><b>Type of System</b></div></td>
  51. <td><input type=text name=Syst size=25></td>
  52. </tr>
  53. <tr>
  54. <td align=right><b>Zip/Postal Code</b></td>
  55. <td><input type=text name=ZPC size=25></td>
  56. <td>&nbsp;</td>
  57. <td>&nbsp;</td>
  58. </tr>
  59. <tr>
  60. <td align=right>&nbsp;</td>
  61. <td>&nbsp;</td>
  62. <td>&nbsp;</td>
  63. <td>&nbsp;</td>
  64. </tr>
  65. <tr>
  66. <td align=right>&nbsp;</td>
  67. <td><div align="right"><strong><font color="#FFFF00" face="Arial, Helvetica, sans-serif">COIL
  68. DESIGN</font></strong></div></td>
  69. <td><font color="#FFFF00" face="Arial, Helvetica, sans-serif"><strong>PARAMETERS</strong></font></td>
  70. <td>&nbsp;</td>
  71. </tr>
  72. <tr>
  73. <td align=right>&nbsp;</td>
  74. <td>&nbsp;</td>
  75. <td>&nbsp;</td>
  76. <td>&nbsp;</td>
  77. </tr>
  78. <tr>
  79. <td align=right><b>Primary Resistance (ohms)</b></td>
  80. <td><input type=text name=Pres size=25></td>
  81. <td><div align="right"><b>Primary Inductance (mH)</b></div></td>
  82. <td><input type=text name=Pind size=25></td>
  83. </tr>
  84. <tr>
  85. <td align=right><b>Secondary Resistance (ohms)</b></td>
  86. <td><input type=text name=Sres size=25></td>
  87. <td><div align="right"><b>Secondary Inductance (H)</b></div></td>
  88. <td><input type=text name=Sind size=25></td>
  89. </tr>
  90. <tr>
  91. <td align=right>&nbsp;</td>
  92. <td>&nbsp;</td>
  93. <td>&nbsp;</td>
  94. <td>&nbsp;</td>
  95. </tr>
  96. <tr>
  97. <td align=right><b>Peak Operating Current (Amps)</b></td>
  98. <td><input type=text name=POC size=25></td>
  99. <td>&nbsp;</td>
  100. <td>&nbsp;</td>
  101. </tr>
  102. <tr>
  103. <td align=right>&nbsp;</td>
  104. <td>&nbsp;</td>
  105. <td>&nbsp;</td>
  106. <td>&nbsp;</td>
  107. </tr>
  108. <tr>
  109. <td align=right><b>Output Energy (mJ)</b></td>
  110. <td><input type=text name=Egy size=25></td>
  111. <td><div align="right"><b>Output Voltage (kV)</b></div></td>
  112. <td><input type=text name=Volt size=25></td>
  113. </tr>
  114. <tr>
  115. <td align=right>&nbsp;</td>
  116. <td>&nbsp;</td>
  117. <td>&nbsp;</td>
  118. <td>&nbsp;</td>
  119. </tr>
  120. <tr>
  121. <td align=right><b># HV Towers per Coil</b></td>
  122. <td><input type=text name=TPC size=25></td>
  123. <td><div align="right"><b># of Coils per Package</b></div></td>
  124. <td><input type=text name=CPP size=25></td>
  125. </tr>
  126. <tr>
  127. <td align=right>&nbsp;</td>
  128. <td>&nbsp;</td>
  129. <td>&nbsp;</td>
  130. <td>&nbsp;</td>
  131. </tr>
  132. <tr>
  133. <td align=right>&nbsp;</td>
  134. <td>&nbsp;</td>
  135. <td>&nbsp;</td>
  136. <td>&nbsp;</td>
  137. </tr>
  138. <tr>
  139. <th colspan=4><b>Please enter any additional information here:</b></th>
  140. </tr>
  141. <tr>
  142. <th colspan=4><textarea name=Mess cols=50 rows=10 id="Message"></textarea></th>
  143. </tr>
  144. </table>
  145. </dl>
  146. <div align="center">
  147. <p>
  148. <input type=hidden name=BodyTag value="&lt;body bgcolor=&quot;#484589&quot; text=&quot;#FFFFFF&quot; link=&quot;#FFFF00&quot; alink=&quot;#FFFFFF&quot; vlink=&quot;#FF7F00&quot;&gt;">
  149. <input type=hidden name=FA value=SendMail>
  150. </p>
  151. <p><font color="#FFFF00" face="Arial, Helvetica, sans-serif"><strong>PLEASE MAKE SURE ALL
  152. INFORMATION<br>
  153. IS CORRECT BEFORE SUBMITTING</strong></font></p>
  154. <p>
  155. <input type=submit value="Submit Form">
  156. </p>
  157. </div>
  158. </form>

THE FILE THAT PROCESSES THE FORM DATA (formdata.php)
***********************************************
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $con = mysql_connect("localhost","XXX","XXX");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7.  
  8. mysql_select_db("customform", $con);
  9.  
  10. $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)
  11. 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]')";
  12.  
  13. if (!mysql_query($sql,$con))
  14. {
  15. die('Error: ' . mysql_error());
  16. }
  17. echo "Your Information Was Successfully Posted";
  18.  
  19. mysql_close($con);
  20.  
  21. $to = "recipient email address here";
  22. $subject = "Custom Form";
  23. $email = $_POST['Email'] ;
  24. $message = $_POST['Comp'] ;
  25. $headers = "From: $Email";
  26. $sent = mail($to, $subject, $message, $headers) ;
  27. if($sent)
  28. {print "Your mail was sent successfully"; }
  29. else
  30. {print "We encountered an error sending your mail"; }
  31. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lightningrod66 is offline Offline
19 posts
since Oct 2007
Sep 6th, 2010
0
Re: Store form data into mysql database, then email same form data
Since I have gotten no replies yet, it makes me think I wasn't clear enough on what the problem is. Let me see if I can be more direct. Going by the codes that I have posted, a user fills out the form then all that information gets stored into the database perfectly (with the exception of the first two fields "Fname" and "Lname" for some odd reason). I get the "SUCCESS" message for the database storage. But I get the error for sending the mail. Maybe I just have a misplaced quotation mark or something, but I can't see it. PHP is set up correctly on the server, as I have been able to send test messages via php. The problem lies in the code - somewhere. I would appreciate it if someone would at least comment on what could be the issue. Having no replies at all is a little disheartening
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lightningrod66 is offline Offline
19 posts
since Oct 2007
Sep 7th, 2010
0
Re: Store form data into mysql database, then email same form data
Sorry for not noticing this earlier. I try to go back to the unanswered threads but as I'm new to this site and that the php forum tends to be VERY active, it's hard sometimes to go back through and look at them all... especially when once some get their answers, they abandon the thread, leaving it unsolved.

Anyway, to address your 2 issues, fname and lname is not being stored because the form name is FName and LName while the query is trying to use Fname and Lname. Submitted variables are case sensitive, therefor Fname and Lname will be empty strings when submitted. If you ever have any problems submitting information you know to be there, check the spelling or try using print_r(), it's usually the cause.

As for the email issue, it's the same problem as the Fname and Lname . You're trying to send it to $Email [ln 25] but you're assigning it to $email [ln 23]

[Links]
http://php.net/manual/en/function.print-r.php
http://php.net/manual/en/function.mail.php

[Suggestion]
If this email is merely a copy of the information already being tracked, why not send it from a predefined address? That way it removes the possibility of the user accidentally mistyping it or trying to maliciously crash the script. The email address would still be added into the database, but a "safe" email would be sent to the sales department with all the needed information anyway. Just a thought anyway.
Last edited by Nyight; Sep 7th, 2010 at 4:02 am.
Reputation Points: 23
Solved Threads: 21
Junior Poster in Training
Nyight is offline Offline
99 posts
since Aug 2010
Sep 7th, 2010
0
Re: Store form data into mysql database, then email same form data
Click to Expand / Collapse  Quote originally posted by Nyight ...
Sorry for not noticing this earlier. I try to go back to the unanswered threads but as I'm new to this site and that the php forum tends to be VERY active, it's hard sometimes to go back through and look at them all... especially when once some get their answers, they abandon the thread, leaving it unsolved.

Anyway, to address your 2 issues, fname and lname is not being stored because the form name is FName and LName while the query is trying to use Fname and Lname. Submitted variables are case sensitive, therefor Fname and Lname will be empty strings when submitted. If you ever have any problems submitting information you know to be there, check the spelling or try using print_r(), it's usually the cause.

As for the email issue, it's the same problem as the Fname and Lname . You're trying to send it to $Email [ln 25] but you're assigning it to $email [ln 23]

[Links]
http://php.net/manual/en/function.print-r.php
http://php.net/manual/en/function.mail.php

[Suggestion]
If this email is merely a copy of the information already being tracked, why not send it from a predefined address? That way it removes the possibility of the user accidentally mistyping it or trying to maliciously crash the script. The email address would still be added into the database, but a "safe" email would be sent to the sales department with all the needed information anyway. Just a thought anyway.
Thanks Nyight. Another example of why it is good to have others look over your code. I must have looked at this a gazillion times and didn't see those errors! I will make the changes and see what happens. If it fixes my problems, I will mark thread as solved. On another note, does it matter if the
PHP Syntax (Toggle Plain Text)
  1. mysql_close($con);
is before or after the email section?? I saw a couple other email form processing scripts and they had the close after the email section. Just curious. I am pretty new to the php/mysql world so I am still learning the basics Again, thanks for the tips and I will see what happens.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lightningrod66 is offline Offline
19 posts
since Oct 2007
Sep 7th, 2010
0
Re: Store form data into mysql database, then email same form data
You're welcome and you don't really need it as it's closed automatically after the script finishes executing. But if you want to use it, you can place it anywhere after the final query is executed.

[Links]
http://php.net/manual/en/function.mysql-close.php
Last edited by Nyight; Sep 7th, 2010 at 2:11 pm.
Reputation Points: 23
Solved Threads: 21
Junior Poster in Training
Nyight is offline Offline
99 posts
since Aug 2010
Sep 7th, 2010
0
Re: Store form data into mysql database, then email same form data
Website lag, double posted.
Last edited by Nyight; Sep 7th, 2010 at 2:11 pm.
Reputation Points: 23
Solved Threads: 21
Junior Poster in Training
Nyight is offline Offline
99 posts
since Aug 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: link in echo
Next Thread in PHP Forum Timeline: PHP - Two arrays in a form - How to post?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC