PHP Help?

WillNicholls

Suspended / Banned
Messages
2,004
Name
Will
Edit My Images
No
Hi all,

I'm asking here as I can't seem to work this out anywhere else. Are there are PHP wizzes here?

I have a php file working to submit an email address to my mail chimp newsletter list when someone fills in a form and hits submit.
The form works, and it submits.

However, it just displays "got it, you're on the list" on a blank page when it succeeds.

I would like it to redirect to a new webpage URL which is the confirmation page. I think the following is the code that needs altering, but I don't know how. Thanks in advance:


if ($data->error) {
$arrResult = array ('response'=>'error','message'=>$data->error);
} else {
$arrResult = array ('Got it, you have been added to our email list.');
}
 
I'm not a great php programmer, but I think you could use the php header() function.

if ($data->error) {
$arrResult = array ('response'=>'error','message'=>$data->error);
} else {
header ('Location: ./confirmationpage.php');
exit ();
}

Doing it this way, you would need to put the 'Got it, you have been added to our email list.' onto the confirmation web page, else the subscriber won't see the message.

Edit: Actually that's not going to work there. This probably isn't the bit where you want to put the header as it will just redirect to the new page, but without processing the form submission. The bit of code you clipped is just error checking the values of the submitted form and posting them to the array $arrResult. There will be a bit of code further down that will write the form details to mailchimp, and then display the results of $arrResult to the users screen, this is where you need to put the header ('Location: ./confirmationpage.php'); exit (); bit of code
 
Last edited:
I'm not a great php programmer, but I think you could use the php header() function.

if ($data->error) {
$arrResult = array ('response'=>'error','message'=>$data->error);
} else {
header ('Location: ./confirmationpage.php');
exit ();
}

Doing it this way, you would need to put the 'Got it, you have been added to our email list.' onto the confirmation web page, else the subscriber won't see the message.

Edit: Actually that's not going to work there. This probably isn't the bit where you want to put the header as it will just redirect to the new page, but without processing the form submission. The bit of code you clipped is just error checking the values of the submitted form and posting them to the array $arrResult. There will be a bit of code further down that will write the form details to mailchimp, and then display the results of $arrResult to the users screen, this is where you need to put the header ('Location: ./confirmationpage.php'); exit (); bit of code

I have no idea what to do ! So here is the whole code (not too long):


<?php
// Credits: https://gist.github.com/mfkp/1488819

session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');

$apiKey = 'XXX REMOVED FOR SECURITY'; - // How get your Mailchimp API KEY - http://kb.mailchimp.com/article/where-can-i-find-my-api-key
$listId = 'XXX REMOVED FOR SECURITY'; - // How to get your Mailchimp LIST ID - http://kb.mailchimp.com/article/how-can-i-find-my-list-id
$submit_url = "http://us3.api.mailchimp.com/1.3/?method=listSubscribe"; - // Replace us2 with your actual datacenter

$double_optin = false;
$send_welcome = false;
$email_type = 'html';
$email = $_POST['email'];
$merge_vars = array( 'YNAME' => $_POST['yname'] );

$data = array(
'email_address' => $email,
'apikey' => $apiKey,
'id' => $listId,
'double_optin' => $double_optin,
'send_welcome' => $send_welcome,
'merge_vars' => $merge_vars,
'email_type' => $email_type
);

$payload = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));

$result = curl_exec($ch);
curl_close ($ch);

$data = json_decode($result);

if ($data->error) {
$arrResult = array ('response'=>'error','message'=>$data->error);
} else {
$arrResult = array ('Got it, you have been added to our email list.');
}

echo json_encode($arrResult);
 
ah, right so it's actually JSON, which I've never used, but now seeing all the code I think you can safely put the

{
header ('Location: ./confirmationpage.php');
exit ();
}

in place of the $arrResult = array ('Got it.... line where I initially suggested

so you end up with this:

if ($data->error) {
$arrResult = array ('response'=>'error','message'=>$data->error);
} else {
header ('Location: ./confirmationpage.php');
exit ();
}

echo json_encode($arrResult);

what will happen is that if there is an error, then a message will be put into the $arrResult array and then the echo will display this to the visitor, but if there is no error, then the redirect will run and the visitor will go to your confirmation page.

Or you could always expand this current script and include your HTML code, incorporating the echo json_encode($arrResult);, to save the visitor being redirected to another page.
 
Last edited:
Hooray that works
ah, right so it's actually JSON, which I've never used, but now seeing all the code I think you can safely put the

{
header ('Location: ./confirmationpage.php');
exit ();
}

in place of the $arrResult = array ('Got it.... line where I initially suggested

so you end up with this:

if ($data->error) {
$arrResult = array ('response'=>'error','message'=>$data->error);
} else {
header ('Location: ./confirmationpage.php');
exit ();
}

echo json_encode($arrResult);

what will happen is that if there is an error, then a message will be put into the $arrResult array and then the echo will display this to the visitor, but if there is no error, then the redirect will run and the visitor will go to your confirmation page.

Or you could always expand this current script and include your HTML code, incorporating the echo json_encode($arrResult);, to save the visitor being redirected to another page.

Hooray that works!
Thanks a bunch!
 
Glad it worked.

If you did want to do the confirmation on the same page as the script, you could revert back to your original piece of coding, and then replace the last line - echo json_encode($arrResult); with this:

<!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=iso-8859-1" />
<title>Contact Us Form Confirmation</title>
</head>
<body>
The content of the document......

<?php
echo json_encode($arrResult);
?>
</body>
</html>
 
Back
Top