2

i am trying to redirect a page according to the text value using switch and button click, its working fine in localhost but not working after hosting. please help to overcome this problem.. looking forward to it. thanks.

form coding

<form id="accounForm" class="form-horizontal" method="post" action="redirect.php" >

<INPUT type="hidden" VALUE ="<?php echo( htmlspecialchars( $row['jewellery_name'] ) ); ?>"  name="textboxdata" >
 <INPUT TYPE = "Submit" class="btn btn-blue" Name = "submit" VALUE = "Add Another">



</form>

redirect.php

  <?php
  session_start();

  ob_start();
$textboxdata = $_POST['textboxdata'];

if (isset($textboxdata)) { 
switch ($textboxdata)
{
case COL:
  header("Location: new_item_5piececollar.php");
  exit();
      case BAB:
  header("Location: new_item_babybangle.php");
 exit();
  case BRR:
  header("Location: new_item_babyring.php");
  exit();
      case BAL:
  header("Location: new_item_balla.php");
 exit();
case BRC:
  header("Location: new_item_bracelet.php");
  exit();
      case BUT:
  header("Location: new_item_button.php");
 exit();
  case CNA:
  header("Location: new_item_chain.php");
  exit();
      case CHK:
  header("Location: new_item_choker.php");
 exit();
case CHR:
  header("Location: new_item_chur.php");
  exit();
      case CHU:
  header("Location: new_item_churi.php");
 exit();
  case DUL:
  header("Location: new_item_dull.php");
  exit();
      case FBA:
  header("Location: new_item_fullballaakshi.php");
 exit();
case GRR:
  header("Location: new_item_gentsring.php");
  exit();
      case HNK:
  header("Location: new_item_helenecklace.php");
 exit();
  case SOB:
  header("Location: new_item_hsocketbouti.php");
  exit();
      case JHU:
  header("Location: new_item_jhumka.php");
 exit();
case KAN:
  header("Location: new_item_kaan.php");
  exit();
      case LRR:
  header("Location: new_item_ladiesring.php");
 exit();
  case LKT:
  header("Location: new_item_locket.php");
  exit();
      case NKL:
  header("Location: new_item_longnecklace.php");
 exit();
case MAK:
  header("Location: new_item_makhri.php");
  exit();
      case MGS:
  header("Location: new_item_mangalsutra.php");
 exit();
  case MAN:
  header("Location: new_item_mantasha.php");
  exit();
      case NOA:
  header("Location: new_item_noah.php");
 exit();
case NOT:
  header("Location: new_item_noth.php");
  exit();
      case PDT:
  header("Location: new_item_pendent.php");
 exit();
  case POL:
  header("Location: new_item_pola.php");
  exit();
      case SAC:
  header("Location: new_item_sankha.php");
 exit();
case TAN:
  header("Location: new_item_tana.php");
  exit();
      case TCK:
  header("Location: new_item_tickli.php");
 exit();
  case TOP:
  header("Location: new_item_tops.php");
  exit();
  case NCK:
  header("Location: new_item_entry.php");
  exit();

      case WRI:
  header("Location: new_item_wrislet.php");
 exit();
  default:
  echo "Enter a number please.";
}
}
ob_end_flush();

?>     
Iam Srkfan
  • 197
  • 9
  • You're buffering your output but never actually retrieving it therefore `echo "Enter a number please.";` will not actually do anything. So if your value in `$_POST['textboxdata']` doesn't match any of your switch cases, nothing will happen... and that's assuming those 2 whitespace characters at the start your example don't exist in the actual file. – CD001 Jun 26 '15 at 10:07
  • $_POST['textboxdata'] will always match the data that for sure. – Iam Srkfan Jun 26 '15 at 10:15
  • @Iam Srkfan Use break; insted of exit()' or use javascript code to redirect it like – Rahul Saxena Jun 26 '15 at 10:16
  • @RahulSaxena it's not being used as a switch/case break but to terminate the script as a `header('Location...)` redirect has been invoked - which is entirely correct. – CD001 Jun 26 '15 at 10:19
  • ... *facepalm* I've just noticed... none of your cases are in quote marks, `case CNA:` that's actually looking for a constant called `CNA` **not** the string 'CNA' - warning level error trapping would have picked that up instantly. – CD001 Jun 26 '15 at 10:20
  • @RahulSaxena no. not working with break as well – Iam Srkfan Jun 26 '15 at 10:21
  • @CD001 ok sir...checking – Iam Srkfan Jun 26 '15 at 10:22
  • @CD001 not working as well :( – Iam Srkfan Jun 26 '15 at 10:34
  • As a side remark, note that using an associative array with keys being the expected string would really reduce the amount of lines of your code... – Laurent S. Jun 26 '15 at 11:30
  • @Bartdude can u give an example ?? – Iam Srkfan Jun 26 '15 at 11:41
  • I just made an answer which is then not a real answer to this question and will probably get downvoted or deleted, but I didn't see where else I could have put this ... – Laurent S. Jun 26 '15 at 11:59

4 Answers4

2

remove the white space before openning the tag

AakkiRock
  • 288
  • 1
  • 4
  • 14
1

There are a few minor issues:

  1. You don't need output buffering
  2. Your switch cases are not in quotes so they'll be interpreted as constants rather than strings
  3. You seem to have some whitespace before the opening <?php
  4. Your redirects aren't relative to the document root so if this in a sub-directory they may not redirect where you expect them to

Something like this should resolve those issues:

<?php
session_start();

$textboxdata = !empty($_POST['textboxdata']) ? $_POST['textboxdata'] : '';
switch ($textboxdata) {

    case 'COL':
        header("Location: /new_item_5piececollar.php");
        exit();

    case 'BAB':
        header("Location: /new_item_babybangle.php");
        exit();

    /* ... and so on ... */

    default:
        echo "Enter a number please.";
        break;
}
CD001
  • 8,332
  • 3
  • 24
  • 28
  • Its because of the whitespace its failed to run the script. thanks a lot sir. – Iam Srkfan Jun 26 '15 at 11:34
  • Ah - yeah that would prevent the `header()` redirects from working since output would have already been sent ;) ... if you'd had the `display_errors` ini setting on you'd have seen the *"Headers already sent"* error. – CD001 Jun 26 '15 at 11:50
1

As OP requested following my remark, here's a suggestion to improve the code to make it shorter. My PHP is a bit rusty and I've nothing to test here so hopefully there's no syntax error, anyway the idea is there :

<?php
    session_start();
    ob_start();
    $textboxdata = $_POST['textboxdata'];
    $URL_array = array('COL' => 'new_item_5piececollar.php', 'BAB' => 'new_item_babybangle.php', 'BRR' => 'new_item_babyring.php');
    if (isset($textboxdata) && array_key_exists($textboxdata, $URL_array))
    { 
        header("Location: ".$URL_array[$textboxdata]);
    }
    else
    {   
        echo "Enter a number please.";
    }
    ob_end_flush();
?>  

Note that for the sake of example I defined $URL_array here but it would be better in some kind of config file, improving the code even more by separating config from logic : if you need to add a new element, you shouldn't modify the code.

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
0

Sometimes your output buffers are on if you're on a development server, but in most of the cases it's turned off on live-systems. Check your .INI file for the following parts:

http://php.net/manual/en/outcontrol.configuration.php

Otherwise check out that there is no output before the headers are sent.

Astinox
  • 154
  • 1
  • 10