Go to a page via PHP

Hello,
I was wondering if it was possible andI hoping someone could help me use PHP to go to a specific url / page. More specifically, as the suite inside an if statement.

Here is a simple instance / example:
if 3 > 5{
/* go to false.html /
}
else{
/
go to true.html */
}

Does a tone have an idea how I could do this or approach doing this?

Please post any Questions, Comments, Concerns, Suggestions, or Solutions. :slight_smile:

Thanks in Advance & Best Regards,
Team 1504.

if 3 > 5{
    /* go to false.html */
header("Location: false.html");
}
else{
    /* go to true.html */
header("Location: true.html");
}

hmmm okay.

well what i was trying to do was read in user-input and if it is a specific word go to a specific page.

More specifically, it was going to introduction.html if the user types in intro or introduction in an input field.

I tried doing what I thought would work with the code you posted, which I thought was the only thing I didn’t know how to do, but it is not working. Could anyone help me with this?

Here is a snippet of the code that I have inside an html document / file :


<?php
$introduction = '';
$intersect = $_POST['needIntroduction'];
$introduction = $_POST['search'];
if(trim($introduction) == 'introduction'){
    header("Location: introduction.html");
}
else if(trim($introduction) == 'intro'){
    header("Location: introduction.html");
}
/*
else{
    /* no need for else as all that would happen on else if setting the header's Location to the current-page */
}
*/
?>
</head>
<body>
    <input name="needIntroduction" type="text" autocomplete="off" value="" />
</body>
</html>

Does anyone know what I am doing wrong and how I can fix it ?


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


// a line of debug you can comment in/out when you are unsure you know what is being passed
// var_dump($POST);

  $intro = trim($_POST['search']); // do the trim in one place

    // generally better practice to use === when making comparisons

    if( $intro === 'introduction' || $intro === 'intro' ){  // if true or true, then evaluate as true
      header("Location: introduction.html");
       exit();  // stop any other processing !important!
    }

  // here code to handle where
  // $_POST['search'] was set but did not match requirements

}else{

// code to handle $_POST['search'] not being set at all

}

That is untested and could be tidied up so that you fail first ($_POST[‘search’] not being set etc) but the comments should help you figure this type of conditional check, and introduces a few practices you could adopt early on which will help you in the future

I am not sure about $_POST[‘needIntroduction’] since there I cannot see the use of it in the above code, I would rather let the user an option in the dropdown instead of letting to type in the text box and have N number of checks. Because a user can type anything in the text box.
Something like this:


<?php
if(isset($_POST['btnSubmit'])){
	if(!empty($_POST['redirectto'])){
		$redirect = $_POST['redirectto'];
		if(file_exists($redirect . "_page.html")){
			header("Location: " . $redirect . "_page.html");
			exit();
		}
	}
}
?>
<html>
</head>
<body>
	<select name="redirectto">
		<option value="">--Select--</option>
		<option value="introduction">Introduction</option>
		<option value="services">Services</option>
		<option value="features">Features</option>
	</select>
	<input type="submit" value="GO" name="btnSubmit" />
</body>
</html>

I would probably use a whitelist instead, more so as the list of options is a manual process anyway.


<?php
$permitted = array(
    'services'    => 'services.html',
    'intro'            => 'intro.html',
    'about-us'    => 'about_us.html'
);


if(false === empty($_POST['location']))
{
    $isPermitted = array_key_exists($_POST['location'], $permitted);
    
    if($isPermitted)
    {
        // note full uri
        header('Location: http://example.org/' . $permitted[ $_POST['location'] ]);
        exit;
    }
}
?>
<html>
    <head>
        <title>Demo</title>
    </head>
    <body>
        <form action="#" method="post">
            <select name="location">
                <option value="" selected="selected">-- Select --</option>
                <option value="services">Services</option>
                <option value="intro">Introduction</option>
                <option value="about-us">About Us</option>
            </select>
            <input type="submit" value="Go!" />
        </form>
    </body>
</html>

Thank you all very much— i really appreciate it. However, for the project / app in its entirety, I need to have an input and not a select with options :frowning:

I don’t want to you to think that your help was in vain because i did learn from reading through the posts.

that is the value for the name attribute of the <input />

Also, I ran the code you posted and it did not go to any url once an option was selected and go was pressed.
Here is the exact syntax that i saved in a php file and viewed from my server in chrome:


<?php
if(isset($_POST['btnSubmit'])){
	if(!empty($_POST['redirectto'])){
		$redirect = $_POST['redirectto'];
		if(file_exists($redirect . "_page.html")){
			header("Location: " . $redirect . "_page.html");
			exit();
		}
	}
}
?>
<html>
</head>
<body>
	<select name="redirectto">
		<option value="">--Select--</option>
		<option value="introduction">Introduction</option>
		<option value="services">Services</option>
		<option value="features">Features</option>
	</select>
	<input type="submit" value="GO" name="btnSubmit" />
</body>
</html>

Anthony’s code did work though :slight_smile:

[B]Although as i mentioned above i need to have it be a value typed in an input and submitted by pressing ‘entre’/‘return’ or a submit-button.

Would you guys be willing to help me with that? I would really appreciate it as i have tried what I could and know how to do.[/B]

I was working with Cups’ code somewhat and I know this isn’t much, especially compared to his original post, but its a starting point…


<!Doctype HTML>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>go to page on type in input</title>
		<?php
		if( isset($_POST['search']) ){
		
			// a line of debug you can comment in/out when you are unsure you know what is being passed
			// var_dump($POST);
                        /* Is it just echo var_dump($POST); ?

  			$intro = trim($_POST['search']); // do the trim in one place


    		       // generally better practice to use === when making comparisons
    		
    		      if( $intro === 'introduction' || $intro === 'intro' ){  // if true or true, then evaluate as true
      		          header("Location: introduction.html");
       		          exit();  // stop any other processing !important!
    		      }
                      
  		      // here code to handle where
  		      // $_POST['search'] was set but did not match requirements
  		      /* else{
                              /* isn't there no need for an else as i don't need or want other words to go to specific
                              pages so the other option is that they would ser the header's Location to the current-page.
                              And doing so would resemble refreshing the page, which I do not want to do.
                              So, in other words, there is no need to define an else ? */
  		          } 
                      */

		}
                // code to handle $_POST['search'] not being set at all
		else{
                    /* How do I do this&#8212; check if $_POST['search'] is not set?
                    Is it just if(! $_POST['search']){...}
                    */
		}
		?>
	</head>
	<body>
		<input type="text" name="search" autocomplete="off" value="" />
                <!-- <!-- With submit button as well. --> <input type="submit" value="Submit" /> -->       
	</body>
</html>

Thank you very much In Advance!,
Team 1504

Okay that’s fine with the requirement. But how many possibilities you will be checking with that input value? Like a user can type anything not only intro or introduction etc. I don’t think that you are going to create those pages dynamically as per the user’s input. Instead you have already fixed number of HTML pages and that you can let the user to select only among them.

BTW, what is the problem with the code that Cups gave you? The idea is enough what he has suggested if you really don’t want to use select box.

Well just for intro or introduction it goes to introduction.html

Eventually, meaning its not done yet, any other value was going to be sent to a php document via the action=“” attribute of the <form> the <input /> will be in.

So that part of the html would look like:


</head>
<body>
    <form action="phpInputParser.php">
        <input type="text" name="search" autocomplete="off" value="" />
        <!-- <!-- With submit button as well. --> <input type="submit" value="Submit" /> -->
    </form>
</body>

hmm nothing i guess. I just had some questions which I put in /* */ comments. One of them I remember is else for if the text inputted is not ‘introduction’ or ‘intro’ and another is handling $POST[‘search’] not being set.

First of all, since you are going to use header() function, do put all the PHP related codes that checks and redirects to another page on top of the page (before <!Doctype HTML>) because header() function has to be used before any output goes in the header.

Secondly, what to display if the search is not set “if( isset($_POST[‘search’]) ){” is up to you. It will just display rest of the page (below that if block) whatever you have written. In above code, in else case the form will remain there.

So then since I don’t want anything to happen if (! isset($_POST[‘search’])) because the form will already exist in the html below, can’t i just ignore that else?

So then would the code so far be, this below?


<?php
if( isset($_POST['search']) ){

    // a line of debug you can comment in/out when you are unsure you know what is being passed
    // var_dump($POST);
    /* Is it just echo var_dump($POST); ? */
 

    $intro = trim($_POST['search']);

    if( $intro === 'introduction' || $intro === 'intro' ){
        header("Location: introduction.html");
        exit();  // stop any other processing !important!
    }
}
?>


<!Doctype HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>go to page on type in input</title>
    </head>
    <body>
        <form action="phpInputParser.php">
            <input type="text" name="search" autocomplete="off" value="" />
            <!-- <!-- With submit button as well. --> <input type="submit" value="Submit" /> -->
        </form>
    </body>
</html>

with as the PHP being above all the html, but in the same file. I separated the syntax highlighting by language to make the code clearer.

Yes that’s enough I think if it is fulfilling your requirement out there. :slight_smile:

Well I can see how it is sound in terms of syntax, but the url that is sent or that is goes to is ‘current url’ + ‘?search=’
and not introduction.html?

Also how can I get the PHP to interrupt executing the form’s action and doing this instead? And the only time the form’s action would be interrupted is when ‘intro’ or ‘introduction’ is typed in the form and submitted by hitting entre / return or the submit-button.

I think I did not understand your question. Do you want to send the searched string to the redirecting page i.e. introduction.html?search=<searched_string>? Or it is now the searched key is being passed via URL (query string) and you do not want that? Because since you do not have set the form’s method attribute and GET is the default.

The input is part of a form that is part of the web-app I am building. Apart from all of this on going to a page, once the user submits the form the data goes to phpInputParser.php. that is done by the value for the <form>'s method attribute being set to that file-name.
Now, no matter what you type in the input field and once the form is submitted either by pressing entre/return or pressing the submit button, what is in the input field is sent to phpInputParser.php

But what I want to do is stop the action of going to phpInputParser.php only when ‘intro’ or ‘introduction’ is typed in the input and submitted. And then now that the form’s default actions is interrupted or stopped, I want to go to introduction.html

All of the references I made to code correspond or are to the code in the last post I made with code— post #11.

For that, either you have to use some JavaScript to check what they have typed in the input box or you can even check the same in phpInputParser.php itself. Like check if they have typed intro or introduction then redirect to respective introduction.html page else you do whatever you want to do in that phpInputParser.php file.

So your HTML page lets say search.html (or .php) page will look like which will have that form.


<!Doctype HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>go to page on type in input</title>
    </head>
    <body>
        <form action="phpInputParser.php" method="post" name="frmsearch">
            <input type="text" name="search" autocomplete="off" value="" />
            <input type="submit" name="btnsubmit" value="Submit" />
        </form>
    </body>
</html>

And now your phpInputParser.php file check what they have typed and send the user to introduction.html page if they typed ‘intro’ or ‘introduction’. Otherwise will do what you want:


if( isset($_POST['btnsearchsearch']) ){
    $intro = trim($_POST['search']);
    // check if they typed introduction or intro then redirect them to introduction.html page.
    if( $intro === 'introduction' || $intro === 'intro' ){
        header("Location: introduction.html");
        exit();
    }
    else{
        // write the code in here that you wanted to do in phpInputParser.php file.
    }
}

Hope this is clear now.

It makes much more sense now; however, it doesn’t seem to work. It just goes to the url of the file in the <form>'s method regardless of what I type in the input and submit.

Could it be in the first line of the PHP? Where does btnsearchsearch come from? I don’t see that anywhere in the html.

More specifically on the issues.
If search.php is at http://myDomain.com/tests/spf/search.php. When I typed ‘a’ the browser went to http://myDomain.com/tests/spf/phpInputParser.php?search=a&btnsubmit=Submit

and when i type in ‘intro’ it goes to http://myDomain.com/tests/spf/phpInputParser.php?search=intro&btnsubmit=Submit

so, how can we fix it or do you have any ideas on how to fix it so that it goes to http://myDomain.com/tests/spf/introduction.html once ‘intro’ or ‘introduction’ is typed in?

Sorry that was just a typo ‘btnsearchsearch’ instead of ‘btnsearch’. It should be:


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

Then it should work as required!

Ah okay. No worries. :slight_smile:

Before I try that, could you explain to me where btnsearch comes from? as I don’t see that in the html either.

I always thought that one put a name of an html element. And btnsearch is not the name of the form or the inputs.

btnsearch is the name of the submit button.