By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

Getting variable names passed to form in php

P: n/a
If I have a form:

<FORM ACTION="test.php"><INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>

then in test.php I could write:

$value = $_POST['hello'];

to set $value="Delete".

But what if I wanted to get "hello" out instead of "Delete". Ie how
can I tell the variables inside the porm submit instead of just the
values of the variables?

Many Thanks... I am new to PHP and can not find this info despite
searching around for a while!

Oct 14 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
On Sun, 14 Oct 2007 13:13:30 +0200, <wi************@gmail.comwrote:
If I have a form:

<FORM ACTION="test.php"><INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>

then in test.php I could write:

$value = $_POST['hello'];

to set $value="Delete".

But what if I wanted to get "hello" out instead of "Delete". Ie how
can I tell the variables inside the porm submit instead of just the
values of the variables?

Many Thanks... I am new to PHP and can not find this info despite
searching around for a while!
Examining the $_POST array.
So, for instance:
<?php
if(!empty($_POST)){
foreach($_POST as $key =$value){
//'hello' should be a key:
echo ""$key:$value\n";
}
}
?>
If you only want the array keys, use array_keys($_POST) for that.
--
Rik Wasmus
Oct 14 '07 #2

P: n/a
Oh thankyou but I am tearing my hair out.

I have this html:

<html>
<head></head>
<body>
<FORM ACTION="test.php"><INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>
</body>
</html>

and if I make test.php this:

<?php

echo 'Hello World!';

?>

but when I try your code it's just blank. in fact i am struggeling to
get anything except this hello world example to work and i have tried
a few from the internet. it's killing me (i am not stupid - lots of c#
experience):
<?php
if(!empty($_POST)){
foreach($_POST as $key =$value){
//'hello' should be a key:
echo "$key:$value\n";
}

}
?>

Oct 14 '07 #3

P: n/a
Suppose I just want to print all the keys... why will this not work
please!

many thanks....
<?php
echo 'Hello World!\n';
if(!empty($_POST)){
foreach($_POST)
{
echo "$key\n";
}

}
echo 'Hello World!\n';
?>

it prints an error message of the foreach line

Oct 14 '07 #4

P: n/a
On Sun, 14 Oct 2007 14:11:40 +0200, <wi************@gmail.comwrote:
Suppose I just want to print all the keys... why will this not work
please!

many thanks....
<?php
echo 'Hello World!\n';
if(!empty($_POST)){
foreach($_POST)
Yes, foreach $_POST, where to put the value (and optionally the key) of
the array?
it prints an error message of the foreach line
RTFM:
http://www.php.net/manual/en/control...es.foreach.php
--
Rik Wasmus
Oct 14 '07 #5

P: n/a
Looking on google I tried:

<?php
print_r ($_POST);
?>

and when that runs all I get on the screen is "Array ( )"

Oct 14 '07 #6

P: n/a
"Yes, foreach $_POST, where to put the value (and optionally the key)
of
the array?"

Sorry Rik i wish i could understand that but i really can't. please
please could you think about a few lines that work just to get me
started...

Oct 14 '07 #7

P: n/a
Right i get the bit about having to have foreach ( x as y) { do
something with y }

but then this is blank too:

<?php
foreach($_POST as $s)
{
echo "$s";
}
?>

Oct 14 '07 #8

P: n/a
On Sun, 14 Oct 2007 14:18:38 +0200, <wi************@gmail.comwrote:
Looking on google I tried:

<?php
print_r ($_POST);
?>

and when that runs all I get on the screen is "Array ( )"
Which you get when nothing is posted.

--
Rik Wasmus
Oct 14 '07 #9

P: n/a
On Sun, 14 Oct 2007 14:21:06 +0200, <wi************@gmail.comwrote:
"Yes, foreach $_POST, where to put the value (and optionally the key)
of
the array?"

Sorry Rik i wish i could understand that but i really can't. please
please could you think about a few lines that work just to get me
started...
I already gave you the working example as the first reply. Actually
reading is more then half the work of learning...
--
Rik Wasmus
Oct 14 '07 #10

P: n/a
but what is this doing then:

<html>
<head></head>
<body>
<FORM ACTION="test.php"><INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>
</body>
</html>

it is certainly going to that test.php form with the ?hello=Delete
post thing..

Oct 14 '07 #11

P: n/a
On Sun, 14 Oct 2007 14:27:29 +0200, <wi************@gmail.comwrote:
but what is this doing then:

<html>
<head></head>
<body>
<FORM ACTION="test.php"
method="post"
<INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>
</body>
</html>

it is certainly going to that test.php form with the ?hello=Delete
post thing..
Which is a GET, not a POST.
http://www.tizag.com/phpT/postget.php

BTW: never, ever, use a GET as an action (alteration of data): GETS should
by bookmarkable and only display date, POSTS can alter data, and are not
bookmarkable. Think for instance about a searchbot that will crawl all
'delete' links to realize why.
--
Rik Wasmus
Oct 14 '07 #12

P: n/a
This is just a nighmare. I am stuck with the fact that my HTML code is
not doing a $_POST but something else and I presumally have to find a
$_SOMETHING_ELSE but I can't see it anywhere on google.
Oct 14 '07 #13

P: n/a
Ah $_GET... oh you are saying that the web site crawlers will press
the button when they crawl the page. I wanted to use this technique to
delete a file if you pressed the button next to it. I guess this is
all a disaster then and I am back to square one.

Oct 14 '07 #14

P: n/a
Many thanks anyway for the help. i think i have to give up on how to
do this and find a friend to write it for me.

Oct 14 '07 #15

P: n/a
On Sun, 14 Oct 2007 14:39:14 +0200, <wi************@gmail.comwrote:
Ah $_GET... oh you are saying that the web site crawlers will press
the button when they crawl the page. I wanted to use this technique to
delete a file if you pressed the button next to it. I guess this is
all a disaster then and I am back to square one.
They will NOT press a button, they will follow links. Links aren't the
only way they get their data though. Google Toolbar for instance reports
browsed URLs. For a one time delete statement (assuming some unique ID) it
happens to be no trouble as the 'thing-to-delete' is allready gone. Would
it be an alteration though (make this product $1,- cheaper), it could be
done over and over again.

Just use a POST for _all_ alterations and you're fine.
--
Rik Wasmus
Oct 14 '07 #16

P: n/a
Ah no I get it... I just put a post method in there intead of a get.

I am writing a free file upload/download untility for my friend:

http://willyhoops.com/upload/

and trying to add delete buttons to the file lines. i am getting
closer then

thanks again

Oct 14 '07 #17

P: n/a
In our last episode,
<11**********************@q3g2000prf.googlegroups. com>,
the lovely and talented wi************@gmail.com
broadcast on comp.lang.php:
Oh thankyou but I am tearing my hair out.
I have this html:
><html>
<head></head>
<body>
<FORM ACTION="test.php"><INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>
</body>
</html>
and if I make test.php this:
><?php
echo 'Hello World!';
?>
This works perfectly. Of course since method defaults to GET, this won't
work at all without a server (in most or all browsers). If you use
method="POST", it will work (sort of) without a server in many browsers
(some will complain 'POST not supported' because there is not a server to
post to) but it should take you to test.php --- which looks blank because
without a server, there is nothing to execute your php. But if you show
source you will find you are in the right place.

but when I try your code it's just blank. in fact i am struggeling to
get anything except this hello world example to work and i have tried
a few from the internet. it's killing me (i am not stupid - lots of c#
experience):
That explains it.
><?php
if(!empty($_POST)){
foreach($_POST as $key =$value){
//'hello' should be a key:
echo "$key:$value\n";
}
}
?>

or you could use array_keys. Look it up in the manual.
--
Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
Countdown: 464 days to go.
What do you do when you're debranded?
Oct 14 '07 #18

P: n/a
In our last episode,
<11*********************@y27g2000pre.googlegroups. com>,
the lovely and talented wi************@gmail.com
broadcast on comp.lang.php:
but what is this doing then:
><html>
<head></head>
<body>
<FORM ACTION="test.php"><INPUT TYPE=SUBMIT NAME="hello"
VALUE="Delete"</FORM>
</body>
</html>
it is certainly going to that test.php form with the ?hello=Delete
post thing..
No, method defaults to GET.
--
Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
Countdown: 464 days to go.
What do you do when you're debranded?
Oct 14 '07 #19

P: n/a
In our last episode,
<11**********************@v29g2000prd.googlegroups .com>, the lovely and
talented wi************@gmail.com broadcast on comp.lang.php:
This is just a nighmare. I am stuck with the fact that my HTML code is
not doing a $_POST but something else and I presumally have to find a
$_SOMETHING_ELSE but I can't see it anywhere on google.
If you do not learn HTML, php cannot help you.
--
Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
Countdown: 464 days to go.
What do you do when you're debranded?
Oct 14 '07 #20

P: n/a
..oO(Rik Wasmus)
>BTW: never, ever, use a GET as an action (alteration of data): GETS should
by bookmarkable and only display date, POSTS can alter data, and are not
bookmarkable. Think for instance about a searchbot that will crawl all
'delete' links to realize why.
The Spider of Doom
http://worsethanfailure.com/Articles...r_of_Doom.aspx

Micha
Oct 14 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.