Forum Moderators: coopster

Message Too Old, No Replies

multiple form actions using php

multiple form actions php

         

trekk82

2:23 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



I am using automated newsletter subscription software from Campaign Monitor (it's similar to MailChimp). To submit the form, Campaign Monitor requires us to use a specific form action (similar to a web address) and the post method. This is fine and it works well. However, I also want to email the subscription details directly to me, and Campaign Monitor doesn't make any provisions for this.

The solution would be to add some code on the signup side, so that as well as submitting the form to Campaign Monitor, it sends it as an email to me. I would like to do this using PHP. The problem is I am not sure how!?

Currently, the code with the Campaign Monitor snippets looks like this:


<form action="http://mywebsite.createsend.com/t/r/s/hubt/" method="post">
<div>
<label for="name">Name:</label><br /><input type="text" name="cm-name" id="name" /><br />
<label for="hubt-hubt">Email:</label><br /><input type="text" name="cm-hubt-hubt" id="hubt-hubt" /><br />
<input type="submit" value="Subscribe" />
</div>
</form>

When I am not using Campaign Monitor I usually use the following PHP snippets to submit forms directly to my email:

This goes on the first line of my web page:


<?php
if (array_key_exists('submit', $_POST)) {
// mail processing script
// initialize variables
$to = 'info@mysite.com';
$subject = 'Membership Application';
// build the message
$message .= 'Name: '.$_POST['name']."\n\n";
$message .= 'Email: '.$_POST['email']."\n\n";
$additionalHeaders = "From: My Website<info@mysite.com>\r\n";
// send the email
mail($to, $subject, $message, $additionalHeaders);
}
?>

And this goes in the form section:


<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<div>
<label><input name="name" type="text" class="textareas" id="name" /></label><br />
<label><input name="email" type="text" class="textareas" id="email" /></label><br />
<input type="submit" value="submit" />
</div>
</form>

What I really need help with is how I can put these two together so I can submit the form to Campaign Monitor, as well as send it as an email to me.

All help is greatly appreciated!

BradleyT

5:23 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Basically what you want to do is have the form page post to itself so you have access to all the fields. You then create a function to post the data to campaign monitor and another function to e-mail you and another function to save the info to a db (if you wish).

The easiest way to post to campaign monitor would be using CURL. Do you have CURL available? If not, there's also a way to post using fputs.

I use both methods to do exactly what you're doing - post to an API and e-mail someone (plus I then save everything to the DB).

Let me know if you want the curl or non-curl solution.

trekk82

5:30 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Thank-you! I think curl is available so lets try that :-)

BradleyT

5:45 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Keep your code where you send the e-mail to info@mysite and then add this below it -

// The POST URL and parameters
$request = 'http://mywebsite.createsend.com/t/r/s/hubt';

$postargs = '';
$postargs .= 'cm-name=' . $_POST['cm-name'] . '&cm-hubt-hubt=' . $_POST['cm-hubt-hubt'];

// Get the curl session object
$session = curl_init($request);

// Set the POST options.
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Do the POST and then close the session
$response = curl_exec($session);
curl_close($session);

// Get HTTP Status code from the response
$status_code = array();

// Check for errors
if ($status_code[0] == 199) //var_dump your status code to see what it contains - 199 is specific to my app
{
//success message
}
else
{
//fail message
}

trekk82

1:59 am on Aug 21, 2009 (gmt 0)

10+ Year Member



Thanks BradleyT!

I think I did this correctly; I have pasted below my code - unfortunately I can't get it to work. The email comes through to me, but the subscriber doesn't get added to Campaign Monitor. The Fail message also shows up when the page loads, which makes me think I have done something wrong? Perhaps the status codes? Silly question, but how do I var dump my status code?


<?php
if (array_key_exists('submit', $_POST)) {
// mail processing script
// initialize variables
$to = 'info@website.com';
$subject = 'Newsletter';
// build the message
$message .= 'Name: '.$_POST['cm-name']."\n\n";
$message .= 'Telephone: '.$_POST['cm-hubt-hubt']."\n\n";
$additionalHeaders = "From: My Company<info@mywebsite.com>\r\n";
// send the email
mail($to, $subject, $message, $additionalHeaders);
}
// The POST URL and parameters
$request = 'http://mywebsite.createsend.com/t/r/s/juujhj/';
$postargs = '';
$postargs .= 'cm-name=' . $_POST['cm-name'] . '&cm-hubt-hubt=' . $_POST['cm-hubt-hubt'];
// Get the curl session object
$session = curl_init($request);
// Set the POST options.
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Do the POST and then close the session
$response = curl_exec($session);
curl_close($session);
// Get HTTP Status code from the response
$status_code = array();
// Check for errors
if ($status_code[0] == 199) //var_dump your status code to see what it contains - 199 is specific to my app
{
//success message
echo "This worked";
}
else
{
//fail message
echo "This does not work";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<div>
<label><input name="cm-name" type="text" class="textareas" id="cm-name" /></label><br />
<label><input name="cm-hubt-hubt" type="text" class="textareas" id="cm-hubt-hubt" /></label><br />
<input type="submit" name="submit" id="submit" value="Subscribe" />
</div>
</form>
</body>
</html>

BradleyT

3:23 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



Well you want to do all of that inside your check to see if the form was submitted - so move that first } down to the bottom of the php.

curl_close($session);
var_dump($response);

That will show you what (if anything) is in $response.

trekk82

4:12 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



Thanks for sticking with this Bradley :-) The response I get is: string(2508) "

The odd thing is that when I include the var_dump the form is at least *trying* to work because I get notification from Campaign Monitor that the email address entered is invalid (but it's not!)

BradleyT

9:31 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



I would try adding this right before it starts the CURL stuff so you can see if it's capturing the values properly.

echo($postargs . "<br>");
break;
$session = curl_init($request);

trekk82

2:38 pm on Aug 24, 2009 (gmt 0)

10+ Year Member



Thanks for your help and patience Bradley :-) I really need to brush up on my php skills!