is there a way to trigger the form post action="add_enquiry.php" where i wrote I WANT TO ADD FORM DATA TO MYSQL AND SEND EMAIL

My form used to work without problems, it added data to mysql, the sent that data in nice html email to me, THEN I added CAPTCHA to it and now it does the action even if code is not correct.

My Enquire page:

<?php
ob_start();

include_once "connect_to_mysql.php";

session_start();

    if ($_POST['form_submitted'] != '1') {


?>

some HTML and Javascript here... then on line 83

<form method="post" action="add_enquiry.php">

some more html with dropdwons and text fields... then line 338

           <input type="text" name="code">
      <input type="submit" value="Submit Form" />
<input type="hidden" name="form_submitted" value="1"/>
      </div>
</form>

    <?php } else if ($_POST[form_submitted] == 1) 
	{
		if(md5($_POST['code']) != $_SESSION['key'])
		
		{
		  
		  echo "It seems you entered an invalid Captcha key. Please go back and try again.";
		
		}else{
		
		echo "I WANT TO TRIGGER FORM ACTION HERE TO POST DATA TO MYSQL AND SEND EMAIL";
	          }
		?>

<?php }  
?>
?>
    <div align="center"></div>
    <div align="center"></div>
    <div align="left"></div></td>
    <td width="39">&nbsp;</td>
  </tr>
</table>
</body>

</html>
<? ob_flush(); ?>

Add_enquiry.php

<?php

include_once "connect_to_mysql.php";
echo "connected and ready"

 //This gets all the other information from the form 
		
                  $id=$_POST['id'];
		$fld_name=$_POST['fld_name'];
		$fld_email=$_POST['fld_email'];
		$fld_mobile=$_POST['fld_mobile'];
		$fld_landline=$_POST['fld_landline'];
		$fld_wherehear=$_POST['fld_wherehear'];
		$fld_whereandswer=$_POST['fld_whereandswer'];
		$fld_numadults=$_POST['fld_numadults'];
		$fld_numchildren=$_POST['fld_numchildren'];
		$fld_numinfants=$_POST['fld_numinfants'];
		$fld_departfrom=$_POST['fld_departfrom'];
		$fld_departdate=$_POST['fld_departdate'];
		$fld_destination=$_POST['fld_destination'];
		$fld_returndate=$_POST['fld_returndate'];
		$fld_flightsrequired=$_POST['fld_flightsrequired'];
		$fld_favairline=$_POST['fld_favairline'];
		$fld_class=$_POST['fld_class'];
		$fld_accomodationreq=$_POST['fld_accomodationreq'];	
		$fld_accrating=$_POST['fld_accrating'];		
		$fld_ratingcategory=$_POST['fld_ratingcategory'];
		$fld_transfers=$_POST['fld_transfers'];
		$fld_transfertype=$_POST['fld_transfertype'];		
		$fld_carrental=$_POST['fld_carrental'];
		$fld_vehicletype=$_POST['fld_vehicletype'];
		$fld_whatspend=$_POST['fld_whatspend'];
		$fld_otherrequest=$_POST['fld_otherrequest'];



//Writes the information to the database 
mysql_query("INSERT INTO tbl_enquire (id, fld_name, fld_email, fld_mobile, fld_landline, fld_wherehear, fld_whereandswer, fld_numadults, fld_numchildren, fld_numinfants, fld_departfrom, fld_departdate, fld_destination, fld_returndate, fld_flightsrequired, fld_favairline, fld_class, fld_accomodationreq, fld_accrating, fld_ratingcategory, fld_transfers, fld_transfertype, fld_carrental, fld_vehicletype, fld_whatspend, fld_otherrequest) VALUES('','$fld_name','$fld_email','$fld_mobile','$fld_landline','$fld_wherehear','$fld_whereandswer','$fld_numadults','$fld_numchildren','$fld_numinfants','$fld_departfrom','$fld_departdate','$fld_destination','$fld_returndate','$fld_flightsrequired','$fld_favairline','$fld_class','$fld_accomodationreq','$fld_accrating','$fld_ratingcategory','$fld_transfers','$fld_transfertype','$fld_carrental','$fld_vehicletype','$fld_whatspend','$fld_otherrequest')");

echo "Data added to MYSQL, Now sending email";
echo " <br>";



//E M A I L _ FORM HERE


$from         = "info@mywebsite.co.za";
$to           = "info@thewebsite.co.za";
$subject     = "Enquiry from website";

//begin of HTML message 
$message = ' 
<html>
<head>
</head>

<body>
<table width=534 height=140 border=1 align=center cellpadding=0 cellspacing=0>
  <tr>
    <td><table width=651 align=center cellpadding=0 cellspacing=0>
      <tr bgcolor=#009900>
        <td height=43 colspan=4><div align=center><strong><font color=white size=5>TRAVEL ENQUIRY </font></strong></div></td>
      </tr>
      <tr>
        <td height=30>&nbsp;</td>
        <td colspan=3>&nbsp;</td>
      </tr>
      <tr>
        <td width=197 height=30><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Name &amp; Surname:</font></strong></font></div>
        </div></td>
        <td colspan=3>'. $fld_name .'</td>
      </tr>
      <tr>
        <td height=29><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Email Address: </font></strong></font></div>
        </div></td>
        <td colspan=3>'. $fld_email .'</td>
      </tr>
      <tr>
        <td height=28><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Mobile number: </font></strong></font></div>
        </div></td>
        <td width=162>'. $fld_mobile .'</td>
        <td width=110>&nbsp;</td>
        <td width=180>&nbsp;</td>
      </tr>
      <tr>
        <td height=31><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Landline number: </font></strong></font></div>
        </div></td>
        <td>'. $fld_landline .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=25><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Where dis you hear about us: </font></strong></font></div>
        </div></td>
        <td>'. $fld_wherehear .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=28>
          <div id=found_us_data_text>
            <div align=left><font color=#009900><strong><font size=2>Details</font></strong></font></div>
        </div></td>
        <td colspan=3>'. $fld_whereandswer .'</td>
      </tr>
      <tr>
        <td height=29>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=29><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Number of Adults: </font></strong></font></div>
        </div></td>
        <td>'. $fld_numadults .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=29><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Number of Children: </font></strong></font></div>
        </div></td>
        <td>'. $fld_numchildren .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=27><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Number of Infants: </font></strong></font></div>
        </div></td>
        <td>'. $fld_numinfants .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=27>
          <div align=left><font color=#009900></font></div></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=29><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Depart From: </font></strong></font></div>
        </div></td>
        <td>'. $fld_departfrom .'</td>
        <td><strong><font color=#009900 size=2>Depart Date:</font></strong></td>
        <td>'. $fld_departdate .'</td>
      </tr>
      <tr>
        <td height=32><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Destination</font></strong></font></div>
        </div></td>
        <td>'. $fld_destination .'</td>
        <td><strong><font color=#009900 size=2>Return Date: </font></strong></td>
        <td>'. $fld_returndate .'</td>
      </tr>
      <tr>
        <td><div align=left><font color=#009900></font></div></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=28><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Flights Required: </font></strong></font></div>
        </div></td>
        <td>'. $fld_flightsrequired .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=26><div id=flight_data_text>
            <div align=left><font color=#009900><strong><font size=2>Favourite Airline:</font></strong></font></div>
        </div></td>
        <td>'. $fld_favairline .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=24><div id=flight_data_text2>
            <div align=left><font color=#009900><strong><font size=2>Selected Class : </font></strong></font></div>
        </div></td>
        <td>'. $fld_class .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=18><div align=left><font color=#009900></font></div></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=30><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Accommodation Required: </font></strong></font></div>
        </div></td>
        <td>'. $fld_accomodationreq .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=27><div id=acc_data_text>
            <div align=left><font color=#009900><strong><font color=#009900><strong><font size=2>Selected</font></strong></font> <font size=2>Rating:</font></strong></font></div>
        </div></td>
        <td>'. $fld_accrating .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=30><div id=acc_data_text2>
            <div align=left><font color=#009900><strong><font color=#009900><strong><font size=2>Selected</font></strong></font> <font size=2>Type:</font></strong></font></div>
        </div></td>
        <td>'. $fld_ratingcategory .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=27>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=27><div align=right>
            <div align=left><strong><font color=#009900 size=2>Do you require Tansfers:</font></strong></div>
        </div></td>
        <td>'. $fld_transfers .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=30><div id=trans_data_text>
            <div align=left><strong><font color=#009900><strong><font size=2>Selected</font></strong></font> <font color=#009900 size=2>Type:</font></strong></div>
        </div></td>
        <td>'. $fld_transfertype .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=25>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=25><div align=right>
            <div align=left><font size=2><strong><font color=#009900>Do you Require Car Rental: </font></strong></font></div>
        </div></td>
        <td>'. $fld_carrental .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height=29><div align=left><font color=#009900><strong><font size=2>Selected</font></strong></font> <font color=#009900 size=2><strong>Class:</strong></font></div></td>
        <td>'. $fld_vehicletype .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><div align=right>
            <div align=left><font color=#009900 size=2><strong>What do you idealy want to spend on your holiday: </strong></font></div>
        </div></td>
        <td>'. $fld_whatspend .'</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><div align=left><font color=#009900></font></div></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td valign=top><div align=right>
            <div align=left><font color=#009900><strong><font size=2>Other Requests: </font></strong></font></div>
        </div></td>
        <td valign=top>&nbsp;</td>
        <td valign=top>&nbsp;</td>
        <td valign=top>&nbsp;</td>
      </tr>
      <tr valign=top>
        <td height=136 colspan=4><div align=left>'. $fld_otherrequest .'</div></td>
      </tr>
      <tr bgcolor=#009900>
        <td colspan=4><div align=center><font color=#FFFFFF>www.website.co.za</font></div></td>
      </tr>
    </table></td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>'; 
   
   //end of message 
    $headers  = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 

    //options to send to cc+bcc 
    //$headers .= "Cc: [email]me@website.co.za[/email]"; 
    //$headers .= "Bcc: [email]email@website.co.za[/email]"; 
     
    // now lets send the email. 
    mail($to, $subject, $message, $headers); 


echo "Email has been sent";
echo " <br>";
header( 'Location: index.php' ) ; 

?>

Is there a way to call the action later ?

Thnx

1. session_start(); must be on the first line of coding.
2. where is $_SESSION defined?
3. you have to check user's entered code with session captcha code on add_enquiry.php page.
Because your form action is 'add_enquiry.php' which will directly executed code without checking captcha.

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.