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>&nbsp;</td>
      <td>&nbsp;</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>&nbsp;</td>
      <td>&nbsp;</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>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td align=right>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td align=right>&nbsp;</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>&nbsp;</td>
    </tr>
    <tr> 
      <td align=right>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</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>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td align=right><b>Peak Operating Current (Amps)</b></td>
      <td><input type=text name=POC size=25></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td align=right>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</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>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</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>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td align=right>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</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="&lt;body  bgcolor=&quot;#484589&quot; text=&quot;#FFFFFF&quot; link=&quot;#FFFF00&quot; alink=&quot;#FFFFFF&quot; vlink=&quot;#FF7F00&quot;&gt;">
      <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"; }
?>

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 :(

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.

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

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.

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

Website lag, double posted.

hey if my form is this... can u help me making a php,asp,sql,jsp file that makes a new file and stores only the email and the password in a file... so that i can use it for signing it again.....

please reply and do me this favour...

<form action="register.php" method="post"> NAME: <input type="text" name="NAME" /><br /> AGE: <input type="text" name="AGE" /><br /> EMAIL ID: <input type="text" name="EMAIL ID" id="email" /><br /> PASSWORD: <input type="password" name="PASSWORD" id="pass" /><br /> CONFIRM PASSWORD: <input type="password" name="PASSWORD" /><br /> <input type="radio" name="sex" value="male" /> MALE <input type="radio" name="sex" value="female" /> FEMALE<br /> <input type="submit" name="submit" value="REGISTER"> </form>

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in D:\xampp\htdocs\Improvise_Online_System\Website_Bootstrap\requestexec.php on line 23
We encountered an error sending your mail . Please Help me to solve this.
-Ryan Mark

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.