0

I have a page where the user can enter what he is selling and be redirected to that page. I know the mysql LIKE '%%' function but I am not using database. If the case matches, I just would like to redirect to that page. How is that possible? whether it is javascript or php?

For examples: if the user enters "laptop" then he/she will be redirected to laptop.php(I have the pages). How can I do that?

The form structure:

 <form class="sellcont" action="" method="post">
     <input type="text" name="selling" >
<input type="submit" name="submit" value="Next" class="myButton">
 </form>


 <?php if(isset($_POST['submit'])){
 $sell = $_POST['selling'];
 if(!empty($_POST['selling'])){

//This is where I am lost. 

}


 }
Community
  • 1
  • 1
amdvb
  • 209
  • 1
  • 6
  • 15

3 Answers3

2

After Comments

You use php header to redirect and check the input ($_POST['selling']) with in_array if its a good input.

$goodinput = array("laptop", "pc", "mac", "cellphone");

if(!empty($_POST['selling'])){

    if (in_array($_POST['selling'],$goodinput)){ //checks if user input is a  good input

       header('Location: http://www.yoururl.com/'.$_POST['selling'].'php');
       exit();

    } else {

       header('Location: http://www.yoururl.com/wedonthavethatpage.php');
       exit();

    }
}

Suggestion:

  • Instead of creating individual pages e.g.: laptop.php you store similar values in a database and create dynamic pages.
Jo E.
  • 7,822
  • 14
  • 58
  • 94
  • I don't think you got my point. I know the header function but my question is that how do I check for what the user entered and then redirect. using header ofcourse – amdvb Sep 02 '13 at 06:07
  • 1
    @amdvb `$_POST['selling']` holds what the user has typed in. if you wan't to know what they typed in you just have to check `$_POST['selling']`. Is that what you mean? – Jo E. Sep 02 '13 at 06:10
  • oh okay but regarding error 404. is there a better way for this? – amdvb Sep 02 '13 at 06:11
  • 1
    @amdvb it would be best that you have a list of all your pages and store it in an array. Then process `$_POST['selling']` if its in the array, if its not you send the user to a page that says, `sorry. we don't have that`. OR you could let users choose what is allowed, maybe inside a dropdown menu instead of your users typing it. – Jo E. Sep 02 '13 at 06:14
  • I mage suggestions but which one do you want? – Jo E. Sep 02 '13 at 06:19
  • I already have auto complete for suggestion. But I didn't like using select box. I also have the pages created already just need to redirect. – amdvb Sep 02 '13 at 06:23
  • 1
    @amdvb ahh. i see. then simply store your accepted inputs in an array then use `if statement` with `in_array` to check if the value is accepted else redirect to a friendlier page than a `404 error` page – Jo E. Sep 02 '13 at 06:26
  • @amdvb I editted my answer. If you find it helpful check it so that others will know that your question has been answered. cheers! – Jo E. Sep 02 '13 at 06:35
1

PHP code should be like this:

<?php if(isset($_POST['submit'])){
 $sell = $_POST['selling'];
 if(!empty($_POST['selling'])){
     header('location: ./'.$_POST['selling'].'.php');   
 }
 else {
     header('location: ./index.php');  //index.php is PHP files on which user enter selling item
  }  
}
?>
1

You could also use the code in this answer:

How can I check if a URL exists via PHP?

to see if the what the user typed in is available. If so, do the redirect, if not, send it to an alternate page.

E.g.

if(!empty($_POST['selling'])){
    $file = 'http://www.yourdomain.com/'.$_POST['selling'].'.php';
    $file_headers = @get_headers($file);
    if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
        $exists = false;
    }
    else {
        $exists = true;
    }

    if ($exists) {
        header(sprintf('Location: %s', $file);
    }
    else {
        header('Location: http://www.yourdomain.com/other_page.php');
    }
    exit();
}
Community
  • 1
  • 1
Stephen O'Flynn
  • 2,309
  • 23
  • 34