...

View Full Version : how to handle form action url with parameter?



frozenade
11-26-2007, 10:21 AM
Hi..

I need to process value from a form handled by parameter.
I write these code:


<form action="<? echo $_SERVER['PHP_SELF']; ?>?id=1&action=add" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" value="Send" />
</form>

<?php

if ($id == "1") {
if ($action == "add") {
echo $_POST['name'];
echo $_POST['age'];
}
}

?>

But, I got blank result when submit button is clicked.

Please help me.. :(

Inigoesdr
11-26-2007, 10:30 AM
You need to use $_GET['id'] & $_GET['action'] for the variables in your action url.

frozenade
11-26-2007, 01:02 PM
if i use $_GET['id'] & $_GET['action'] so i should use method="get", isn't it?
I've use GET method but it has blank result too if I implemented it with form.

How to correct this? Could you fix my code?
and is my method to catch values by using 'if' correct?

I need suggestions. Please help me. :(

Thank you so much before. :)

Ahri
11-26-2007, 03:32 PM
if ($_GET['id'] == "1") {
if ($_GET['action'] == "add") {
echo $_POST['name'];
echo $_POST['age'];
}
}

The ?foo=bar part of a URL is actually a GET request, and you probably have register_globals off (or you should have) so $id and $action won't be in the global namespace.

You do not need to change your action URL as suggested above.

Fumigator
11-26-2007, 05:49 PM
As Ahri suggests, your code is written assuming register_globals is on, and you're probably on a server where register_globals is off.

aedrin
11-26-2007, 06:47 PM
You need to use $_GET['id'] & $_GET['action'] for the variables in your action url.

One could also use $_REQUEST instead of $_GET. ;)

Inigoesdr
11-26-2007, 09:34 PM
if i use $_GET['id'] & $_GET['action'] so i should use method="get", isn't it?
The method parameter for the form only affects how the inputs inside of the form are submitted.

One could also use $_REQUEST instead of $_GET. ;)

One could; but it would be overwritten if you had a field with the same name in the form, or cookie, or session. :D

aedrin
11-26-2007, 09:50 PM
One could; but it would be overwritten if you had a field with the same name in the form, or cookie, or session. :D

Actually, it does not include the session variables. Which only leaves Cookies to mess things up, which generally doesn't (and shouldn't - since they should be used sparsely) happen.

It also reinforces the fact that one should properly name their variables/names. Using identifiers of less than 3 characters is generally bad, unless it's for a loop or counter.

rpgfan3233
11-26-2007, 09:57 PM
It also reinforces the fact that one should properly name their variables/names. Using identifiers of less than 3 characters is generally bad, unless it's for a loop or counter.

That is true in most cases. In some cases, a simple $id would be more efficient than something like $action_id or $user_id. After all, sometimes the actions are driven via different pages. That makes $action_id useless. Sometimes you don't have a $msg_id or $ticket_id because it isn't a part of a forum or ticket system or whatever. That might leave only $user_id, which would be less efficient than just $id.

However, I should add that there is a noteworthy counterargument - consistency between scripts. If you have a forum integrated with your site, using $user_id, $msg_id or whatever else might be used is typically better than just $id because the variable explains the contents of the variable, reducing the need for commenting, and because of the fact that a lack of consistency can lead to unnecessary confusion.

Whichever side you choose, be aware of the disadvantages.

arnyinc
11-26-2007, 10:30 PM
Just to clarify the issue with your original code... you have specified the method="post". So you will access your form elements (name, age, and send) as $_POST['name'], etc. Since you have concatenated the other query strings onto the action (id=1&action=add), those are passed as "GET" variables and you would have to access them as $_GET['id'] and _$GET['action'].

The following would work.


<form action="<? echo $_SERVER['PHP_SELF']; ?>?id=1&action=add" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" value="Send" />
</form>

<?php

if ($_GET['id'] == "1") {
if ($_GET['action'] == "add") {
echo $_POST['name'];
echo $_POST['age'];
}
}

?>

Inigoesdr
11-26-2007, 11:57 PM
Hmm.. I thought that was pretty clear. The code you posted, and the explanations were already stated.

frozenade
11-27-2007, 03:07 AM
wow.. thank you so much for help me out.. :)

i very very thank you..
now i can get the values for user input.
thank you once again.. :D

codingforums is :thumbsup:

Fumigator
11-27-2007, 03:31 AM
Apparently he was just looking for fixed code to copy/paste. :rolleyes:

frozenade
11-27-2007, 09:21 AM
Apparently he was just looking for fixed code to copy/paste. :rolleyes:

oops, no Mr. Fumigator, please don't be sarcastic. i'm just trying to find a solution to fixing my code. I am a newbie and I write the code my self then I got that problem. It's pure for me to need some helps, not just for copy paste.

aedrin
11-27-2007, 05:11 PM
Especially if someone is learning one shouldn't get the answer spelled out for them. If you learn by reading what someone suggests to you, but never create yourself, you become stuck in a pattern where you might as well start learning a new language. Because it makes you not very useful.

arnyinc
11-27-2007, 10:19 PM
Hmm.. I thought that was pretty clear. The code you posted, and the explanations were already stated.

I realize that you thought it was clear. That's exactly why I had to step in and explain.

:thumbsup:

Inigoesdr
11-28-2007, 12:19 AM
The point was that you didn't explain anything more or even different then what was already posted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum