Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question not recognising a variable?!

    I get an error message:

    Notice: Undefined index: spubs in C:\Documents and Settings\John\Desktop\phptesting\xampplite\htdocs\mps_php\search_result1.php on line 7

    search box form:
    Code:
    <div id="top"><div id="search"><form action="search_result1.php" method="post" name="search form"><input name="spubs" type="text" class="searchbox" />&nbsp;<input name="search" class="searchbutton" type="submit"  value="search" /></form></div></div>
    search results page line7:
    Code:
    $r_name = $_GET['spubs'];

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Hey Jarv,
    You're form method is post, not get, so $_GET['spubs'] won't exist. Try the $_POST['spubs'] instead, and don't forget to handle the !isset for it too, there is nothing preventing a user from directly attaching to the page (just die the page if its not set for example)
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by jarv View Post
    I get an error message:

    Notice: Undefined index: spubs in C:\Documents and Settings\John\Desktop\phptesting\xampplite\htdocs\mps_php\search_result1.php on line 7

    search box form:
    Code:
    <div id="top"><div id="search"><form action="search_result1.php" method="post" name="search form"><input name="spubs" type="text" class="searchbox" />&nbsp;<input name="search" class="searchbutton" type="submit"  value="search" /></form></div></div>
    search results page line7:
    Code:
    $r_name = $_GET['spubs'];
    you need to change
    Code:
    <div id="top"><div id="search"><form action="search_result1.php" method="post" name="search form"><input name="spubs" type="text" class="searchbox" />&nbsp;<input name="search" class="searchbutton" type="submit"  value="search" /></form></div></div>
    to
    Code:
    <div id="top"><div id="search"><form action="search_result1.php" method="get" name="search form"><input name="spubs" type="text" class="searchbox" />&nbsp;<input name="search" class="searchbutton" type="submit"  value="search" /></form></div></div>
    OR(MEAN NOT BOTH!!)
    Code:
    $r_name = $_GET['spubs'];
    to
    Code:
    $r_name = $_POST['spubs'];
    BUT YOU SHOULD
    Code:
    if(!isset $_GET['spuds']{
         exit();
    }
    $r_name = $_GET['spubs'];
    (modify the get or post as needed from your choice of solution.

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Using a form post would be like this.

    if (isset($_POST["search"]))
    {
    $spubs = $_POST["spubs"];

    echo "You are searching for the word $spubs";

    }


  •  

    Posting Permissions

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