Page 1 of 1

Uninitialized $_POST variables

Posted: Wed May 25, 2005 3:24 pm
by unmsol
Hello all, I'm new both to this community, to php, and scripting languages in general and I've encountered a situation that I'm sure many others have encountered as well, but I've yet to find any information that addesses it. If this is something that has been posted elsewhere and I've missed it, please forgive me. I've run some searches but haven't come up with anything.

So, I have a page that is both the query and results page. The query searches for books in a library database and displays the results. The problem is that I'm using $_POST[""] variables to get the query parameters and the results page is the same page as the query page. This is giving me a warning message that the variable doesn't exist when the page first loads and a query has yet to be performed:
Notice: Undefined index: query in E:\Inetpub\WWWRoot\testing\aq-db\query.php on line 72
Does anyone know how I can perform a check to see if a POST method has been executed before attempting to access the $_POST[""] variables or if that isn't possible, if there is a way to suppress that error so it isn't written to the page?

The code that generates the query (for now):

Code: Select all

<?php
if($_POSTї&quote;query&quote;] != null){
    $connectionstring = odbc_connect(&quote;Allacquistions&quote;, &quote;&quote;, &quote;&quote;); 
    
    $queryStr = $_POSTї&quote;query&quote;];
    $searchOpt = $_POSTї&quote;search-option&quote;];
    
    $Query = &quote;SELECT * FROM AllFields WHERE ($searchOpt LIKE  '%$queryStr%' ) ORDER BY TITLE ASC&quote;;
    
    //...irrelevent code removed...
    
    odbc_close($connectionstring);
    }
?>
Thanks for any help!
Gabe

Posted: Wed May 25, 2005 3:37 pm
by thomas777neo
you could probably use something like

Code: Select all

$test = $REQUEST_METHOD;
This will tell you which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'

An @ before a variable etc, also supresses the error message. But I wouldn't recommend it

Re: Uninitialized $_POST variables

Posted: Wed May 25, 2005 3:50 pm
by Roja
unmsol wrote:The problem is that I'm using $_POST[""] variables to get the query parameters and the results page is the same page as the query page. This is giving me a warning message that the variable doesn't exist when the page first loads and a query has yet to be performed...
if there is a way to suppress that error so it isn't written to the page?
Sure is..

Code: Select all

<?php
if (!isset($_POSTї'query']))
{
    $_POSTї'query'] = '';
}

if($_POSTї&quote;query&quote;] != ''){
    $connectionstring = odbc_connect(&quote;Allacquistions&quote;, &quote;&quote;, &quote;&quote;); 
... blah blah

Posted: Wed May 25, 2005 3:56 pm
by thomas777neo
A lesson on how to keep things simple 8)

Posted: Wed May 25, 2005 4:29 pm
by unmsol
Thank you both for your replies!

thomas777neo, the $REQUEST_METHOD generates the same error as using $_POST vars, probably because prior to executing any form method, there is no method to request. I really appreciate the feedback though, because I didn't know that you could request information about what method a form used. I also didn't know about the @ functionality, and I think it's pretty cool. I would never use such a fix in code that anyone else would actually see, but the neat thing about server side scripts is hacks like this can go unseen!

Roja, thank you for sharing the isset() function with me... I'm sure I'll be using that a lot in the future. Your solution solves my problem perfectly, I appreciate the help!

Regards!
Gabe

Posted: Wed May 25, 2005 4:30 pm
by Sphen001
Just a note, the '@' operator isn't a hack. I know it seems wrong to silence errors, but sometimes it's useful to do so.

Sphen001

Posted: Thu May 26, 2005 7:56 am
by Roja
Sphen001 wrote:Just a note, the '@' operator isn't a hack. I know it seems wrong to silence errors, but sometimes it's useful to do so.
I could be wrong, but it sounded like he was using the original meaning for hack: Clever solution to a tricky problem.
unmsol wrote:Roja, thank you for sharing the isset() function with me... I'm sure I'll be using that a lot in the future. Your solution solves my problem perfectly, I appreciate the help!
I've used it a few *hundred* times in my webgames so far, so it was literally like second nature to me. Different experiences and all that.. Glad it helped.