Jump to content

Notice Undefined Index


Bebopgeo
 Share

Recommended Posts

I have put a text field the label is Search on my page for serching and I use a URL variable to keep the text, that I put in there, when it jumps back to the same page and when I view it in the brower I get this notice in the text field "<br /><b>Notice</b>: Undefined index: Search in <b>C:\wamp\www\SearchersLogItPHP\Private\MySites.php</b> on line <b>163</b><br />" In the same website but in .ASP I used a <%= Request.QueryString (Search) %> to keep the text. and my php URL variable looks like this one is Request_Search = <?php echo $_GET['REQUEST_Search']; ?> and the other I used is Search = <?php echo $_GET['Search']; ?> and they bouth make the same notices. the form is lable frmSearch and the action is MySites.php and the method is get. so tell me that I'm making my URL wrong and it is a quick fix.

Link to comment
Share on other sites

That message means that the array didn't have that index, or that $_GET didn't have something called 'Search'. So first, make sure your name is correct, if the name is Search then you should have a field like this in your form:<input type="text" name="Search">To define the value for PHP, you can use code like this:

<?php$search = (isset($_GET['Search']) ? $_GET['Search'] : '');?>

That will check if $_GET['Search'] was set, and if it was set it will set $search to the value from $_GET. If it's not set, then $search will just be an empty string.

Link to comment
Share on other sites

  • 2 weeks later...

OK I have done some testing and I found out that the URL.Search works fine keeping the text in the text field when it loops back, but I have to keep Search in the http://localhost/SearchersLogItPHP/Private...ites.php?Search if I put Search at the end of the MySites.php address like this MySites.php?Search it works fine but when I remove the Search I stell get that error "<br /><b>Notice</b>: Undefined index: Search in <b>C:\wamp\www\SearchersLogItPHP\Private\MySites.php</b> on line <b>163</b><br />" in the text field with your code in or with out your code. I'm useing Dreamweaver CS4 if that makes any differents

Edited by Bebopgeo
Link to comment
Share on other sites

I've always used REQUEST to get variables off a URL string.

$p = (isset($_REQUEST['p'])) ? $_REQUEST['p'] : "";

I don't know if that makes any difference in your case or not, but worth a try.

Link to comment
Share on other sites

Here is a simple block of code of what I have been doing I get a error Notice when I get to that page and when I use the search field on test1, and when I use test2 it works fine keeping the text in the field when it loops but only after I use the clear search results link with the "?Search" in the address. the problem is getting the error when I go to the page or when I use the search field. and I have a URL. variable and it is called Search. So can anyone copy this code and run it in a php page and test it to fine the problem. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>Test1gettes error all the time even after clicking the clear search results link<br /><form id="frmSearch" name="frmSearch" method="get" action="TestSearch.php"> <label> <strong>Search by key word</strong> <br /> <input name="Search" type="text" id="Search" value="<?php echo $_GET['Search']; ?>" /> </label> <br /> <label> <input type="submit" value="Search" /> </label></form> <br /> <a href="TestSearch.php">Clear Search Result</a> <br /> <br /> Test2don't get erroe after clicking the clear search results link the "?Search" in the address stopes the error<br /><form id="frmSearch" name="frmSearch" method="get" action="TestSearch.php"> <label> <strong>Search by key word</strong> <br /> <input name="Search" type="text" id="Search" value="<?php echo $_GET['Search']; ?>" /> </label> <br /> <label> <input type="submit" value="Search" /> </label></form> <br /> <a href="TestSearch.php?Search">Clear Search Result</a> </body></html>

Edited by Bebopgeo
Link to comment
Share on other sites

You note the change yourself, when you have "TestSearch.php?Search", there is a "Search" URL variable, and so $_GET['Search'] is a valid index. When you visit it without "?Search" there is no such index in the $_GET superglobal array. And the line that is triggering the error is where you do:

<?php echo $_GET['Search']; ?>

This will always give you an E_NOTICE level error in cases where 'Search' is not a defined index, and by default it is only a defined index when "?Search" or "?Search=anything" is in the URL query string when using $_GET as you are. Now, in the first reply it was suggested that you do not use $_GET['Search'] directly, except once at the beginning of the document where you could have:

<?php $search = (isset($_GET['Search'])) ? $_GET['Search'] : ''; /* ... more code perhaps ... */ ?>

And use $search anywhere you are currently using $_GET['search']. That line of code will never trigger an "undefined index" notice, because isset() does not have that behaviour. But it's important to remember, if you don't use $search from then on instead of $_GET['Search'], then you're not going to make any change to the notice you are currently triggering.One last thing to note, you should not directly echo out $_GET input, ever. You need to sanitise it first, you should actually have something like this (as well as the previous code sample at the beginning of the document):

<input name="Search" type="text" id="Search" value="<?php echo htmlspecialchars($search, ENT_QUOTES); ?>" />

That will protect you from a XSS vulnerability that was in your code. See the documentation on htmlspecialchars for more detail on what it does.

Edited by AElliott
Link to comment
Share on other sites

OK I put this code at the top of the page befor every thing else

<?php $search = (isset($_GET['Search'])) ? $_GET['Search'] : ''; ?>

and replace the URL. variable

<?php echo $_GET['Search']; ?>

with this

<?php echo htmlspecialchars($search, ENT_QUOTES); ?>

and now it works fine so thinks for the help!!! and I tried putting this code

<?php $search = (isset($_GET['Search'])) ? $_GET['Search'] : ''; ?>

above the search form and it did not make any difference it still work fine.

Link to comment
Share on other sites

OK now I have the same problem with this jump menu. I put this code at the top of the page

 <?php $Category = (isset($_GET['Category']) ? $_GET['Category'] : ''); ?>

but I can't see how this code will work in the jump menu to replace the URL variable, $_GET['Category']

<?php echo htmlspecialchars($search, ENT_QUOTES); ?>

Test jump menu page

<?php $Category = (isset($_GET['Category']) ? $_GET['Category'] : ''); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><form action="TestJumpMenu.php" method="get" name="frmFilterByCategory" id="frmFilterByCategory">	<strong>Filter Sites by Category</strong>	<br />	<select name="Category" id="Category">	  <option value="" <?php if (!(strcmp("", $_GET['Category']))) {echo "selected=\"selected\"";} ?>>Choose a Category</option>	  <option value="Artifact" <?php if (!(strcmp("Artifact", $_GET['Category']))) {echo "selected=\"selected\"";} ?>>Artifact</option>	  <option value="Coin" <?php if (!(strcmp("Coin", $_GET['Category']))) {echo "selected=\"selected\"";} ?>>Coin</option>	</select>	<label>	  <input type="submit" value="Go" />	  </label>	  </form>	<br />	  <a href="TestJumpMenu.php">Clear Search Result</a></body></html>

So can anyone help

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...