Jump to content

Help with redirect header


twilitegxa

Recommended Posts

I have the following script for allowing users to sign up for a newsletter. When the user leaves the textbox blank, it's supposed to redirect them back to the same page, but instead it is displaying a blank page. Can anyone help me with why?

 

 


<?php
//set up a couple of functions
function doDB() {
   global $conn;
   //connect to server and select database; you may need it
   $conn = mysql_connect("localhost", "root", "root")
      or die(mysql_error());
   mysql_select_db("smrpg",$conn) or die(mysql_error());
}


function emailChecker($email) {
   global $conn, $check_result;
   //checl that e-mail is not already in list
   $check = "select id from subscribers where email = '$email'";
   $check_result = mysql_query($check,$conn) or die(mysql_error());
}


//determine if they need to see the form or not
if ($_POST['op'] !="ds") {
//they do, so create form block
$display_block = "
<form method=POST action=\"$_SERVER[php_SELF]\">


<p><strong>Your E-Mail Address:</strong><br>
<input type=text name=\"email\" size=40 maxlength=150></p>


<p><strong>Action:</strong><br>
<input type=radio name=\"action\" value=\"sub\" checked> subscribe
<input type=radio name=\"action\" value=\"unsub\"> unsubscribe


<input type=\"hidden\" name=\"op\" value=\"ds\"></p>


<p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
</form>";


} else if (($_POST['op'] == "ds") && ($_POST['action'] == "sub")) {
   //trying to subscribe; validate email address
   if ($_POST['email'] == "") {
      header("Location: newsletter.php");
      exit;
   }
   //connect to database
   doDB();
   //check that email is in list
   emailChecker($_POST['email']);
   
   //get number of results and do action
   if (mysql_num_rows($check_result) < 1) {
      //add record
      $sql = "insert into subscribers values('', '$_POST[email]')";
      $result = mysql_query($sql,$conn) or die(mysql_error());
      $display_block = "<p>Thanks for signing up!</p>";
   } else {
      //print failure message
      $display_block = "<p>You're already subscribed!</p>";
   }
} else if (($_POST['op'] == "ds") && ($_POST['action'] == "unsub")) {
   //trying to unsubscribe; validate email address
   if ($_POST['email'] == "") {
   header("Location: newsletter.php");
      exit;
   }
   //connect to database
   doDB();
   //check that email is in list
   emailChecker($_POST['email']);
   
   //get number of results and do action
   if (mysql_num_rows($check_result) < 1) {
      //print failure message
      $display_block = "<p>Couldn't find your address!</p>
      <p>No action was taken.</p>";
   } else {
      //unsubscribe the address
      $id = mysql_result($check_result, 0, "id");
      $sql = "delete from subscribers where id = '$id'";
      $result = mysql_query($sql,$conn) or die(mysql_error());
      $display_block = "<p>You're unsubscribed!</p>";
   }
}
?>

 

 

The rest of the code works fine; the user can subscribe, unsubscribe, and be notified if they already have subscribed.

Link to comment
Share on other sites

I guess since I was already echoing the form before this header was told to be run, I was getting a header error. I used this instead:

 

 

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';

 

 

And I have solved my problem, for now. I will later add validation to get around this problem. Sorry for all the confusion. I was using a tutorial book and it's kind of old, so apparently this used to work! Thanks for all the help guys!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.