Forum Moderators: coopster

Message Too Old, No Replies

One Submit Button to Do 2 jobs

One Submit button to do 2 different jobs

         

shaiqbashir

5:20 am on May 23, 2010 (gmt 0)

10+ Year Member



Dear

I have a web form in php. It is basically a two page form. One page it asks for Name, Email and City and then a button needs to be pressed. Then it is redirected to the second page where it asks for more information. When all the information is entered on the second page and submit is pressed, the information is stored in a mysql database.

Now i have a constantcontact account. I want to integrate its adding Subscriber feature in this form. What i want is this that when the user enters his Name, Email and City on the first page of my webform, that "email" should also sent to the constantcontact list along with storing it in my sql database.

My Webform index.php coding is as follows:

 <div class="form_box">
<div class="form_box-1">
<p>City</p>
<input class="form-1" type="text" name="city" id="city" value="<? echo $_SESSION['city']?>" />
</div>
<div class="form_box-1">
<p>Email</p>
<input class="form-1" type="text" name="email" id="email" value="<? echo $_SESSION['email']?>" />
</div>
<div class="form_box-1">
<p>How soon are you<br /> looking to buy?</p>
<select name="to_buy" id="to_buy" style="margin:6px;">
<option value="Immediately" <? echo ($_SESSION['to_buy']=="Immediately")?"selected=\"selected\"":""?>>Immediately</option>
<option value="30-90 days" <? echo ($_SESSION['to_buy']=="30-90 days")?"selected=\"selected\"":""?>>30-90 days</option>
<option value="3-6 months" <? echo ($_SESSION['to_buy']=="3-6 months")?"selected=\"selected\"":""?>>3-6 months</option>
<option value="6-12 months" <? echo ($_SESSION['to_buy']=="6-12 months")?"selected=\"selected\"":""?>>6-12 months</option>
</select>
</div>
<div class="form_box-1">

<input type="button" class="buton-1" value=" " onclick="callAjax()" />
</div>


and the constant contact API coding is :

<?php include_once('header.php');?>
<div align="center">
<?php echo $message?>
<h2>Enter Contact Email Address</h2>
<form method="post" action="">
E-mail address: <input type="text" name="src_mail" />
&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Sign Up" />
</form>
</div>
<?php include_once('footer.php'); ?>



Im getting confused that how to use one submit button in my webform to do both operations. Can you please guid me through this

Thanks in advance

Readie

11:35 am on May 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, on the page that you post to, you'd do something like this:

if(count($_POST)) {
$errors = array();
if(!isset($_POST['something_one']) || empty($_POST['something_one'])) {
$errors[] = 'Something one is not set.';
}
if(!isset($_POST['something_two']) || empty($_POST['something_two'])) {
$errors[] = 'Something two is not set.';
}
// Other validation here
// Other validation here
// Other validation here
if(!count($errors)) {
$sql = ''; // SQL INSERT statement
mysql_query($sql);
if(mysql_error()) {
$errors[] = 'An unexpected database error occured.';
} else {
// Build your mail variables here
if(mail($to, $subject, $message, $headers, $parameters)) {
echo 'Success!';
} else {
$errors[] = 'An un expected error occured.';
$sql = ''; // SQL DELETE statement
mysql_query($sql);
}
}
}
if($count = count($errors)) {
echo '<ul>'
for($i = 0; $i < $count; $i++) {
echo '<li>' . $errors[$i] . '</li>';
}
echo '</ul>';
}
}
So you validate all your form inputs, and only if there are no errors do you execute a SQL insert. If that does not fail, you then run your mail() function.

If the mail function fails, then you delete what you just inserted.

shaiqbashir

2:48 pm on May 23, 2010 (gmt 0)

10+ Year Member



Dear Readie

Thanks for your quick help.

Can you explain your coding a bit. I think my issue was about the page where i have submit buttons. Not the one which has validations. However, i consider myself novice, so can you please explain your answer.

Best Regards

Shaiq Bashir

rocknbil

5:05 pm on May 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and the constant contact API coding is :


This one's fairly easy.

Submit form as you normally do, do whatever actions on your server that need to be performed.

Within that same recipient script, post the data via cURL to constant contact. Be sure to store the response from the curl into a variable in the event of any error.

Return your response, or redirect, as is common with PHP.

Dig up the documentation on PHP.net for curl (pcntl_curl().)

So you'll be doing

- accept and cleanse input
- store to your database
- post the data to constant contact, your "second action"
- return to the "more information" page.