ManOnScooter Posted November 8, 2007 Share Posted November 8, 2007 I am taking input from a form and getting result into the pagination(result.php) so the search i give is $totalCount = "select * from student_adv where name like '%$_POST[search]%'"; my pagination works fine when i hard code the valie of $_POST[search] as John or similar.. but when i go to the next page i get the error as "Notice: Undefined index: search in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\result.php on line 56" i tried putting the value of $_POST[search] in hidden variable-it did not work. I know there is a way out-but wots it- any body got any idea??? is the problem because of $_POST[search] or there could be some other problem? Thanks for help in advance... Scooter!! Quote Link to comment Share on other sites More sharing options...
ManOnScooter Posted November 8, 2007 Author Share Posted November 8, 2007 the code goes as follows- anybody got any clues how it should be resolved?? $pageNumber=(isset($_POST["page"]) && is_numeric($_POST["page"])) ? $_POST["page"] :1; $perPage = 10; $padding = 5; $startIndex = ($pageNumber * $perPage) - $perPage; $testsearch = $_POST['search']; $totalCount = "select * from student_adv where name like '%$_POST[search]%'"; $rsCount = mysql_query($totalCount, $conn) or die(mysql_error()); $rowCount = mysql_num_rows($rsCount); print "<div align=\"center\">"; $numOfPages= ceil( $rowCount / $perPage); print "<a href=\"result.php?page=1\">FIRST</a>"; print "</div>"; $sql = "select id, name from student_adv where name like '%$_POST[search]%' order by id limit $startIndex, $perPage"; $rs = mysql_query($sql, $conn) or die(mysql_error()); if ( mysql_num_rows($rs) > 0 ) { while ($row = mysql_fetch_object($rs)) { print "<div>"; print $row->id; print ": "; print $row->name; print "</div>"; } } else { print "sorry!! no rows"; } Quote Link to comment Share on other sites More sharing options...
dbo Posted November 8, 2007 Share Posted November 8, 2007 The problem is the value of the post variable is lost. After you filter and escape it store it in a session variable or something. Quote Link to comment Share on other sites More sharing options...
ManOnScooter Posted November 8, 2007 Author Share Posted November 8, 2007 i realised that-and thats the reason - i put it in another hidden HTML variable... <html> <body> <form method="POST"> <input type="text" name="search" value="<?php print $testsearch ?>"> </form> </body> </html> i again lose it - what do u suggest - how do i resolve this?? thanks again Scooter Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 Using a hidden input field won't do you much good if you the user clicks on the paginations links. You would need to submit the form to have access to the hidden field on the next page. First of all, how is the user getting to the next page? Are they just simple text links that pass it a page number? Without submitting a form, you have two options on how to pass the search parameter to the next page when the user clicks on a link. 1) Through session variables 2) Appending the search parameter to the url and retrieving it by $_GET Here's a sample of how you could accomplish either of them. $_SESSION $_SESSION['search'] = $_POST['search']; Of course, you will need to call session_start(); before you set/access any $_SESSION variables. You will also have to do your own checking on pages that do not contain the $_POST['search'] variable. When it the page doesn't contain it, you should pull the search parameter from the session, instead of the $_POST. $_GET Pass the search parameter in the URL in every pagination link. <a href="search.php?page=5&search=value">Page 5</a> Make sure you remember to escape your search parameter on every page to prevent injection attacks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.