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.
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:
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
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:
[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— 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>
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.
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.
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.
}
}
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.