SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question 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.

    Thanks in Advance & Best Regards,
    Team 1504.

  2. #2
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if 5{
        
    /* go to false.html */
    header("Location: false.html");
    }
    else{
        
    /* go to true.html */
    header("Location: true.html");


  3. #3
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 :
    Code HTML4Strict:
    <?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 ?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    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

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code html4strict:
    <?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>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would probably use a whitelist instead, more so as the list of options is a manual process anyway.

    Code PHP:
    <?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: [url]http://example.org/[/url]' . $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>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    Quote Originally Posted by Raju Gautam
    I am not sure about $_POST['needIntroduction'] since there I cannot see the use of it in the above code,
    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:

    Code HTML4Strict:
    <?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

    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.


    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...

    Code HTML4Strict:
    <!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>

    Thank you very much In Advance!,
    Team 1504

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raju Gautam View Post
    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.
    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:
    Code HTML4Strict:
    </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>

    Quote Originally Posted by Raju Gautam View Post
    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.
    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.

  10. #10
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  11. #11
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raju Gautam View Post
    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 Code:
    <?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!
        
    }
    }
    ?>
    Code HTML4Strict:
    <!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.

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that's enough I think if it is fulfilling your requirement out there.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  13. #13
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  16. #16
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code html4strict:
    <!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:
    Code php:
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  17. #17
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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/phpInp...nsubmit=Submit

    and when i type in 'intro' it goes to http://myDomain.com/tests/spf/phpInp...nsubmit=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?

  18. #18
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry that was just a typo 'btnsearchsearch' instead of 'btnsearch'. It should be:
    PHP Code:
    if( isset($_POST['btnsearch']) ){ 
    Then it should work as required!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  19. #19
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah okay. No worries.

    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.

  20. #20
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    btnsearch is the name of the submit button.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  21. #21
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wow im sorry guys. Maybe I should get some sleep

  22. #22
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Actually, I tried the code and again no matter what I do, it (the browser) goes to phpInputParser.php uri/url/file-location and doesn't execute the code in phpInputParser.php if the input was not 'intro' or 'introduction' nor does it go to introduction.html if the input is.

    To simply the code, I deleted all the syntax in phpInputParser.php (I have a back up), and just had it echo the $intro variable, but the issue i mentioned above still occurs

    Here is the code in search.php:
    Code HTML4Strict:
    <!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 here is the code in phpInputParser.php:
    PHP Code:
    <?php
    if( isset($_POST['btnsearch']) ){
        
    $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.
            
    echo $intro;
        }
    }
    ?>
    Am I doing something stupid or wrong or is there a problem with the code?

    Regards,
    Team 1504

  23. #23
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do nothing, just change the name of the submit button:
    <input type="submit" name="btnsubmit" value="Submit" />
    To
    <input type="submit" name="btnsearch" value="Submit" />

    And check what you see...!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  24. #24
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It works! Thank you all very much! Now that just the echo works, I will develop and put the code that is phpInputParser.php in the else condition— hopefully that will go well.

    I apologise for the stupid mistakes that I made by not reading carefully and thank you for your help, all of you, very much!

    Regards,
    Team 1504


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •