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. 

}


 }
1
  • 1
    But how can you restrict the user or you will create the page like laptop.php... ?? Sep 2, 2013 at 6:11

3 Answers 3

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.
8
  • 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 2, 2013 at 6: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 2, 2013 at 6:10
  • oh okay but regarding error 404. is there a better way for this?
    – amdvb
    Sep 2, 2013 at 6: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 2, 2013 at 6:14
  • 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 2, 2013 at 6:26
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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.