Passing query string in a form action to the same page

I am farly new to the world of serverside scripting and need a bit of assistance.


<form action="<?php echo $_SERVER[ 'PHP_SELF'] .'?[b]some='. $num[/b]; ?>" method='post'>

Is the above possible or is my syntax wrong? The reason I ask is becasue the page I’m working on doesn’t error when executed but by $some variable is coming up undefined. So am I just marking it up wrong or is this type of passing not possible?

I forgot to mention that I have tested for this variable in the part of the script which it resides. It is coming up as it should so that isn’t the promblem. Its also defined in the url on the page when it reloads.

thanks

You are trying to mix GET (sending parameters through the query string) with POST (sending them through a form with method=post). You have to choose one or the other.

If you want to send data through a form instead of a link, make “some” a hidden field.

<input type="hidden" name="some" value="<?php echo $num; ?>" />

And I’ll assume when you said $some, you meant $_GET[‘some’], but with the above form it’ll be in $_POST[‘some’].

Dan I have to disagree about the need to choose between GET and POST.
In the past I’ve found it helpful to use both. For example on a page that displays and allows the user to edit information with an ID in the query string. The form can just POST back to the page with the ID in the query string and no extra logic is required to determine the ID of the entity being edited.

Such as:


//now at user.php?id=1
<form method="post" action="user.php?id=1">
<input type="text" name="new_username">
</form>

Odz - I suspect you just have register globals off (as they should be). This means when the page POSTs you’ll need to access $_GET[‘some’], instead of $some

If $_GET[‘id’] is populated in that example, I’d consider it a PHP bug, not a feature, since the HTTP request was POST.

I’d never thought of that. I’m not familiar enough with the HTTP spec to know whether this should be considered a bug or not. From a usage POV I find it pretty handy to be able to do this.

I wonder if other server side languages have the same behaviour?

thanks for the help dan grossman

cranial-bore wrote:

Odz - I suspect you just have register globals off (as they should be). This means when the page POSTs you’ll need to access $_GET[‘some’], instead of $some

yep I know.

If oddz changes the form’s method to get (instead of post), the URL would work. Then the value of some would be accessed in PHP as:

$some = $_GET[‘some’];