Jump to content

[SOLVED] Redirect depending on 1st letter of field input


tqla

Recommended Posts

Hi. The code below is a form that looks at a promotional code and sees if it exists in a database. If it does they move on to a survey. There are only 2 types of 10 digit promotional codes - codes that start with 'L' and codes that start with 'S'. Right now they both go to 'survey.php'. How can I make 'L' codes go to 'l-survey.php and 'S' codes go to 's-survey.php'?

 

<?php 
require_once('db/db.php');
session_start();  

if (isset($_POST['submit'])) {

    $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
        header("Location: survey.php");
      } else {
        echo "<font color=\"#FF0000\">The Promotional code that you have entered does not exists or has already been redeemed.</FONT><BR><BR>";
      }
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>
    <input type="reset" name="Reset" value="Reset">
    <input type="submit" name="submit" value="submit">
</p>
</form>

Link to comment
Share on other sites

Try this:

 

<?php 
require_once('db/db.php');
session_start();  

if (isset($_POST['submit'])) {

    $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
            $row = mysql_fetch_assoc($result);
            
               if (substr($row['code'], 0, 1) == 'L'){
              header("Location: l-survey.php");
        } else if (substr($row['code'], 0, 1) == 'S'){
              header("Location: s-survey.php");
        }
            
      } else {
        echo "<font color=\"#FF0000\">The Promotional code that you have entered does not exists or has already been
                 redeemed.</FONT><BR><BR>";
      }
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>
    <input type="reset" name="Reset" value="Reset">
    <input type="submit" name="submit" value="submit">
</p>
</form>

?>

 

EDIT: I just edited the code, so if you just tried it and it didn't work...try it again =]

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.