Hi,

I would like to submit form data in two ways, with the one submit button.

The posting methods i want to use are...

To post to mysql database:

<form action="success.php" method="post" name="form" id="form" onsubmit="return validate(this)">

To post to email:

<form action="http://www.mywebsite.com/cgi-bin/cgiemail/form/form.txt" method="post" onsubmit="return validate(this)">
<input type="hidden" name="success" value="http://www.mywebsite.com/form/success.html"/>

How can I combine two actions in one form?

Thanks.

Try make action="success.php" and in success.php place the following code.

$url = 'http://www.mywebsite.com/cgi-bin/cgiemail/form/form.txt';

//url-ify the data for the POST
foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

That should forward the post to your other action.

Member Avatar for nileshgr

you could also use AJAX to submit second form while submitting the first one.

Hi,

Alternatively you can write code for post to email in your success.php page. so you can do DB entry and email code easily by single page only.

No need for two pages.

Ask me if you need more help.

Try make action="success.php" and in success.php place the following code.

$url = 'http://www.mywebsite.com/cgi-bin/cgiemail/form/form.txt';

//url-ify the data for the POST
foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

That should forward the post to your other action.

Thanks heaps :)
That worked a charm.

One more question... I don't particularly want to display the results, but just have my own thank you/success message instead - how would i do this (usually i just add some html, but if the email fails, they'll still get the same static "success" message).

Hi,

Alternatively you can write code for post to email in your success.php page. so you can do DB entry and email code easily by single page only.

No need for two pages.

Ask me if you need more help.

Hi,
I tried this yesterday... the success page loaded and said "your form was sent successfully" - but I never got the email. Maybe you can tell me where i went wrong?

I've got it working with cURL, but i'm always keen to learn :)

$NameFirst = $_POST['NameFirst'];
$NameLast = $_POST['NameLast'];
$Email = $_POST['Email']; 
$ID1 = $_POST['ID1'];
$Dept= $_POST['Dept'];
$Field = $_POST['Field'];
$Date = $_POST['Day'].'-'.$_POST['Month'].'-'.$_POST['Year'];
 
$to = "$Dept"; 
$subject = "My Form"; 
$message =  
"**** MY FORM **** \n\n
Customer ID: $ID1 \n
Name: $NameFirst $NameLast \n
Field: $Field \n
Date: $Date\n\n
Blah blah";
$headers = "From: $Email"; 
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) 
{print "Your form was sent successfully"; }
else 
{print "We encountered an error sending your form"; }

Line 19 and 20 should be replaced with the following.

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

Also if you want to check the results in your cgi script then it would be as simple as comparing the return value. Eg.

if ($result=='Success the email was sent.') {
//success message
} else {
//fail message
}

Thank you cwarn23 - you are awesome

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.