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>

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 =]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.