Jump to content

GET variable coming up empty when inserting integers


jumpenjuhosaphat

Recommended Posts

[code]
$distance=$_GET['distance'];
if($distance='' || !is_numeric($distance)){$distance=10000;}[/code]

When I echo $distance, if I entered a number in the field, $distance comes back empty....
if I entered a non-numeric string or if I don't enter anything, $distance comes back as 10000

Why would that be?
Im using a form.....
[code]<form method="get" action="index.php">
<input type="text" name="distance"/>
<input type="submit" name="submit" value="Search"
</form>
[/code]

When I enter 295 into the form field, the URL looks like: http://www.......com/index.html?distance=295&submit=Search
I tried that, but it gives me invalid XHTML errors when I try to validate it at W3C.  It didn't make any difference here, because the get just tells the form how to pass the information, it doesn't actually assign any variable names.
Your not using the GET method, get is used in links you are using POST, switch the method to post and try again with your script reading $_POST variables.

Read,

http://www.phpfreaks.com/phpmanual/page/language.variables.predefined.html
In addition,

[code=php:0]
<form name="myform" method="post" action="index.php">
<input name="distance" type="text" />
<input name="apply" type="submit" value="Search" />
</form>
[/code]

Its a bad idea to use submit as the name of a submit button.
No, I'm using the get method.  It needs to be the get method so that the pages can be bookmarked.  If I use the post method, it'll hide the underlying variables and the page won't be markable.

I discovered where I had gone wrong.....In the if statement, I made a big duh...:)

I didn't use the double =, if(this==that), instead I used if(this=that), and by doing that, I set the value of the $distance variable to a 0.  Thank you all for your help.
[quote author=jumpenjuhosaphat link=topic=123969.msg513084#msg513084 date=1169722081]
No, I'm using the get method.  It needs to be the get method so that the pages can be bookmarked.  If I use the post method, it'll hide the underlying variables and the page won't be markable.[/quote]

I'm a little lost by this but if you say so. Naming a control submit causes some problems when using client side script.
[quote author=Cep link=topic=123969.msg513089#msg513089 date=1169722662]
[quote author=jumpenjuhosaphat link=topic=123969.msg513084#msg513084 date=1169722081]
No, I'm using the get method.  It needs to be the get method so that the pages can be bookmarked.  If I use the post method, it'll hide the underlying variables and the page won't be markable.[/quote]

I'm a little lost by this but if you say so. Naming a control submit causes some problems when using client side script.
[/quote]

Well, the difference between get and post is how the variables are passed to the script.  In the get method, the variables are passed via the URL, in the post method, the variables are passed via the HTTP header.  You can't pass variables via the post method using a link, and you can't bookmark, or save the variables using the post method.  Posted variables are convenient when you don't want the values to be visible, typically for security reasons.  But in my case I need the values to be part of the URL, because the site is a database driven site, and each page is a derivative of the index.php page.  In other words, page 2 would be index.php?action=go&page=2

Does that make sense?  Thanks for the tip on the submit name, I never knew that.
[quote author=Cep link=topic=123969.msg513089#msg513089 date=1169722662]
[quote author=jumpenjuhosaphat link=topic=123969.msg513084#msg513084 date=1169722081]
No, I'm using the get method.  It needs to be the get method so that the pages can be bookmarked.  If I use the post method, it'll hide the underlying variables and the page won't be markable.[/quote]

I'm a little lost by this but if you say so.
[/quote]

Cep,

Your understanding of HTML forms is slightly off the mark.  When you submit a form, you can use either the POST or the GET method.  The OP clearly has GET in his/her form method and so the parameters will be passed in the URL.

Regards
Huggie
hmm.. 2 pages and no one has seen the error:

This: [code=php:0]if ($distance=''[/code]

should read: [code=php:0]if ($distance==''[/code]

You also don't need to use the $distance=$_GET['distance']; assignment, you can just use $_GET['distance'] directly, and you should also check it exists first.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.