SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  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)

    redirect to URL with PHP

    I have a form at index.php that takes in user input, it the includes and sends the user input to another php file for processing.

    Here is the code of index.php:

    Code:
    <?php
    if(isset($_GET['q'])){
        include_once "form.php";
        exit(0);
    }
    ?>
    <!Doctype HTML>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Search</title>
        </head>
        <body>
            <form method="get">
             <input type="text" name="q" />
        </form>
        </body>
     </html>
    When one submits the form it goes to http://mysite.com/?q=textUserEntered (if just the domain was visited before) or http://mysite.com/index.php?q=textUserEntered (if index.php is visited before)

    How can I get it to go to http://mysite.com/form?q=textUserEntered or http://mysite.com/index.php/form?q=textUserEntered while still passing the form data to form.php

    I tried this in the beginning index.php and form.php, it navigates to the URL but doesn't pass the data to form.php and instead goes to a 404 error page.

    Code:
    if(!empty($_GET['q']))
    {
        header("Location: form?q=".rawurlencode($_GET['q']));
        exit;
    }
    Update:

    I can't use the action attribute because adding form.php to the value of the action attribute would make the URL http://mysite.com/form.php?q=userEnteredText not http://mysite.com/form?q=userEnteredText

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    remove the exit() call from your if, and make your form's Action point to form. (If you're trying to hide the search after, use an ELSE)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  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)
    @StarLion So, I remove the exit(); and then change the form's action like so
    HTML Code:
    <form action="/form" method="get"></form>
    I don't understand what you mean by
    (If you're trying to hide the search after, use an ELSE)

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by team1504 View Post
    @StarLion So, I remove the exit(); and then change the form's action like so
    HTML Code:
    <form action="/form" method="get"></form>
    I don't understand what you mean by
    I think what @StarLion ; means is this:

    PHP Code:

    <?php
    if( isset( $_GET['q'] ) )
    {
        include_once 
    "form.php";
        
    // exit(0);
    }else{ ?>
    <!Doctype HTML>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Search</title>
        </head>
        <body>
            <form method="get">
             <input type="text" name="q" />
        </form>
        </body>
     </html>
    <?php ?>
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  5. #5
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    I think what @StarLion ; means is this:

    PHP Code:

    <?php
    if( isset( $_GET['q'] ) )
    {
        include_once 
    "form.php";
        
    // exit(0);
    }else{ ?>
    <!Doctype HTML>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Search</title>
        </head>
        <body>
            <form method="get">
             <input type="text" name="q" />
        </form>
        </body>
     </html>
    <?php ?>
    Thats the right way to use your php code so as to include the "included page" and prevent the main page from showing.

  6. #6
    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. Thank you guys for your help! I will try this later today and let you all know!


Tags for this Thread

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
  •