I'm running into a problem with my PHP and I'm hoping you can help me out.
In essence, the client wants a single form, and a single submit button, but then he wants the PHP to send information to two places: 1.) to send the "comments" message directly to his email inbox but also 2.) have the email address and hidden variables sent along to Constant Contact (which is the company that does his newsletters).
Here is the form via the HTML:
<form action="SendToConstantContact.php" name="myform" method="post">
Your email*:<br/>
<input type="text" name="ea" size="39" value="" style="font-size:10pt;"><br/>
Your message (optional):<br/>
<textarea name="comments" cols="30" rows="3" style="font-size:10pt;"></textarea><br/>
<input type="hidden" name="llr" value="z4u4kndab">
<input type="hidden" name="m" value="1103180583929">
<input type="hidden" name="p" value="oi">
<input type="submit" value="Submit" />
</form>
And here is the PHP so far. I have the 1st objective working---it posts to this PHP and the PHP sends an email directly to the inbox with the message content. What can I add to this PHP code to also send the variables along to Constant Contact?
<?php
$emailSubject = 'Customer Has a Question!';
$webMaster = 'barry@page3design.com';
$ea = $_POST['ea'];
$comments = $_POST ['comments'];
$llr = $_POST ['llr'];
$m = $_POST ['m'];
$p = $_POST ['p'];
$body = <<<EOD
<br><hr><br>
Email: $ea <br>
Comments: $comments <br>
<br>
And below are the four variables sent to Constant Contact:<br>
Email: $ea <br>
LLR: $llr <br>
M: $m <br>
P: $p <br>
<br>
These comments were sent from the test.html page of the JW Secure site. <br>
The visitor has checked the box that would send their information to Constant Contact.<br>
EOD;
$headers = "From: $ea
";
$headers .= "Content-type: text/html
";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<META http-equiv="refresh" content="0;URL=http://www.jwsecure.com/contact/thank-you/">
EOD;
echo "$theResults";
?>
I've looked around on this site and others, but I have not found any examples of just sending information along in your PHP file. It seems like it would be relatively simple, but I am stumped. Everything I try causes an error. If it was an HTML form directly posting to ConstantContact, the action would be: "action="http://visitor.r20.constantcontact.com/d.jsp" ...but I am not a PHP-person (obviously), so help with the syntax is very appreciated!
Thank you in advance, Barry
You can accomplish it by either using cURL or Jquery.
CURL is a way in which you can target a URL from your code to get a html response from it.cURL means client URL which allows you to connect with other URLS and use the responses as you want.
Check this link:-
Here is the basic structure you need to change if you want to use jquery
. You can submit one from through ajax and then on success you can submit the other form. i have only included one example of how the form data is processed to the ajax call. If don't want to manually code all the variables you can use a jquery plugin like ajaxform
<form id='myform' action="SendToConstantContact.php" name="myform" method="post">
Your email*:<br/>
<input type="text" name="ea" size="39" value="" style="font-size:10pt;"><br/>
Your message (optional):<br/>
<textarea name="comments" cols="30" rows="3" style="font-size:10pt;"></textarea><br/>
<input type="hidden" name="llr" value="z4u4kndab">
<input type="hidden" name="m" value="1103180583929">
<input type="hidden" name="p" value="oi">
<input type="button" id='submit' value="Submit" />
</form>
<script type='text/javascript'>
$(document).ready(function (f) {
$('#submit').click(function (e) {
e.preventDefault();
var data = {"llr" : $("input[name=llr]").val()….};
var options = {
data: data,
type: "post",
url: "http://visitor.r20.constantcontact.com/d.jsp",
success: function (e) {
$('#myform').submit();
}
};
$.ajax(options);
};
}
</script>
just use curl to post your data with PHP:
small example, not tested... see PHP documentation for more curl_setopt
$ch = curl_init();
$path = "http://visitor.r20.constantcontact.com/d.jsp";
curl_setopt( $ch, CURLOPT_URL, $path );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postarray );
curl_exec ( $ch );
curl_close ( $ch );
unset( $ch );
my experience: try less options first, then add more, don't start of with millions of settings and expect it to work :)
To get the information over to ConstantContact you need to use the PHP curl extension AJAX won't work as it is on a different domain.
Something like the following should get you started
$data = array('ea' => $_POST['ea'], 'llr' => $_POST['llr'], ...);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://visitor.r20.constantcontact.com/d.jsp");
curl_setopt($ch, CULROPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Stops the result from being sent to the browser
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch); //$result now contains the response
curl_close($ch);
<?php
if($_POST['Send']!='')
{
$subject = "subject";
$message = "content";
$from = "enter from ";
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$to="test@gmail.com";
$from="User";
$headers .= "From:" . $from;
$response=mail($to,$subject,$message,$headers);
if($response)
{
$msg="show msg.";
}
}
?>
hope it will help you