1

Hi I have here a code which will search for a name then returns the result paginated. I'm having an error on lines 19 and 20 which is

Notice: Undefined index: submit in C:\xampp\htdocs\eventreg\trypaginate2.php on line 19 - NOW OK

and

Notice: Undefined index: search in C:\xampp\htdocs\eventreg\trypaginate2.php on line 20 - NOW OK

But it is working. When I turn to the next page it won't show any results just a blank page.

Here is my html code:

            <html>
            <head>
            <title>Title of your search engine</title>
            </head>
            <body>
            <form action='trypaginate2.php' method='POST'>
            <center>
            <h1>My Search Engine</h1>
            <input type='text' size='90' name='search'></br></br>
            <input type='submit' name='submit' value='Search source code' ></br></br></br>
            </center>
            </form>
            </body>
            </html>

Then php code here:

                            <?php

                if(array_key_exists('search', $_GET))
                {
                    echo "hello";
                    $search = $_GET['search'];
                    echo "$search";
                }
                  if(isset($_POST['submit']))
                  {
                if(array_key_exists('search', $_POST))
                {
                    echo "hi";
                    $search = $_POST['search'];
                }
                  }


            if(strlen($search)<=1)
            echo "Search term too short";

            else
                {
            echo "You searched for <b>$search</b> <hr size='1'></br>";

            include("dbcon.php");

            $search_exploded = explode (" ", $search);

            $x = "";
            $construct = "";  

            foreach($search_exploded as $search_each)
                    {
                        $x++;
                        if($x==1)
                        $construct .="name LIKE '%$search_each%'";
                        else
                        $construct .="AND name LIKE '%$search_each%'";

                    }
             echo $construct;
            $constructs ="SELECT * FROM reginformation WHERE $construct";
            $run = mysql_query($constructs);

            $foundnum = mysql_num_rows($run);

            if ($foundnum==0)
            echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
            Try more general words. for example: If you want to search 'how to create a website'
            then use general keyword like 'create' 'website'</br>2. Try different words with similar
             meaning</br>3. Please check your spelling";
            else
                    { 

            echo "$foundnum results found !<p>";

            $per_page = 5;
            $start = isset($_GET['start']) ? $_GET['start']: '';
            $max_pages = ceil($foundnum / $per_page);
            if(!$start)
            $start=0; 
            $getquery = mysql_query("SELECT * FROM reginformation WHERE $construct LIMIT $start, $per_page");

            /*while($runrows = mysql_fetch_assoc($getquery))
                        {
                            $title = $runrows ['regID'];
                            $desc = $runrows ['name'];
                            $url = $runrows ['address'];

                            echo "
                            <a href='$url'><b>$title</b></a><br>
                            $desc<br>
                            <a href='$url'>$url</a><p>
                            ";
                        }*/

              if(mysql_num_rows($getquery)>0)
                        {
                    while ($row = mysql_fetch_assoc($getquery)) {
                    // echo data
                    echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr><br>";

                    } // end while
                        }
                    else
                        {
                            echo ("<SCRIPT LANGUAGE='JavaScript'>
                                window.alert('No record found.');
                                window.location.href='reglistresp.php';
                            </SCRIPT>");
                        }

            //Pagination Starts
            echo "<center>";

            $prev = $start - $per_page;
            $next = $start + $per_page;

            $adjacents = 3;
            $last = $max_pages - 1;

            if($max_pages > 1)
                        {   
            //previous button
            if (!($start<=0)) 
            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    

            //pages 
            if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
                            {
            $i = 0;   
            for ($counter = 1; $counter <= $max_pages; $counter++)
                                {
            if ($i == $start)
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                }
            else 
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                }  
            $i = $i + $per_page;                 
                                }
                            }
            elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
                {
            //close to beginning; only hide later pages
            if(($start/$per_page) < 1 + ($adjacents * 2))        
                    {
            $i = 0;
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                        {
            if ($i == $start)
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                }
            else 
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                } 
            $i = $i + $per_page;                                       
                        }

                    }
            //in middle; hide some front and some back
            elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

                    $i = $start;                 
                    for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
                    {
                        if ($i == $start)
                        {
                            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                        }
                        else 
                        {
                            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                        }   
                        $i = $i + $per_page;                
                    }

                }
            //close to end; only hide early pages
            else
            {
                echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
                echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

                $i = $start;                
                for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
                {
                    if ($i == $start)
                    {
                        echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                    }
                    else 
                    {
                        echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
                    } 
                    $i = $i + $per_page;              
                }
            }
                }

            //next button
            if (!($start >=$foundnum-$per_page))
            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
                        }   
            echo "</center>";
                    } 
                } 

            ?>

EDIT! Changed my php code now I'm getting an error on line 34. The error is
Notice: Undefined variable: search in C:\xampp\htdocs\eventreg\trypaginate2.php on line 34

  • `$button = $_GET['submit'];` and `$search = $_GET ['search'];` should be `$button = $_POST['submit'];` and `$search = $_POST['search'];` – Shehary Aug 16 '15 at 05:59
  • Then if I turn into the next page how should I be able to get the datas without using **GET**? – user2254004 Aug 16 '15 at 06:02
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Aug 17 '15 at 01:03

1 Answers1

0

You shouldn't check whether a key exists in array (which is what $_POST and $_GET are) by blindly assigning variables to those values or even by using isset - even though I know that seems like it would make sense. That's the behavior that PHP is sending you a warning about. That error comes from trying to find the value of things like $_POST['search'] or $_GET['submit'] when those values aren't set in that array.

Instead, use array_key_exists('submit', $_GET) or array_key_exists('search', $_POST) to check whether those values are set. If that expression evaluates to true, then you can proceed with accessing those values in your code and you should not receive any notices in the logs.

mrcheshire
  • 525
  • 2
  • 8
  • Actually, the reason for using `array_key_exists` instead of `isset` is actually a bit more subtle and less important than I made it seem, but you should definitely not be accessing those values before checking whether or not they exist in the array, if that's the workflow you're trying to set up. – mrcheshire Aug 16 '15 at 06:04
  • Thanks for the idea. Can you help me in the other problem? When I turn to the next page I don't get any results – user2254004 Aug 16 '15 at 06:11
  • First of all, you don't need to have all that code to handle both get and post requests in the first place. You can change the method in the form in your HTML to use 'GET' instead of 'POST', and that way you can just always get GET. In any case, the notices about an undefined index are probably not related to your other problem. There is probably some other error causing the page to fail to display. When you say it doesn't display, do you mean no HTML get rendered whatsoever? If that's the case, there's probably something else in the logs. – mrcheshire Aug 16 '15 at 07:28
  • If some HTML gets rendered, but not any results, than you can try to print the output of either the $_GET array or the mysql query to the page, to see how they differ between the first page and subsequent pages. – mrcheshire Aug 16 '15 at 07:30