Forum Moderators: coopster

Message Too Old, No Replies

http post and key value pairs problems

started with PHP short time ago :)

         

aprinc

10:26 am on Jan 18, 2003 (gmt 0)

10+ Year Member



I have to collect data from HTTP post. Data is send in key value pairs. I have to store value in databse.

How HTTP post is send ( how it looks when sending )?
How to collect values?

example of key value pairs:
$first_name => John
$last_name => Smith

Thanks in advance

andreasfriedrich

12:02 pm on Jan 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] aprinc.

Be sure to read Marcia`s WebmasterWorld Welcome and Guide to the Basics [webmasterworld.com] post.

How to access values submitted via forms is explained here [php.net].

To loop over all keys in the $_POST hash use foreach [php.net].

Andreas

Brett_Tabke

12:07 pm on Jan 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



A GET comes in on the url:

$buffer = $ENV{QUERY_STRING};

A POST comes in on STDIN with the environment variable CONTENT_LENGTH holding the length of the content:

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

Now the posted data is in $buffer, and pulling it apart is as simple as:

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$name =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\r//g;
$FORM{$name} = $value;
$debugtxt.="<br> Form name : <b>$name</b> Value : <b> $value </b>\n";
}

andreasfriedrich

12:32 pm on Jan 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoops, I just realized that you did not say you were using PHP.

It seems I have been doing too much PHP coding to just assume that everybody would be using it.

Carefully reading the questions (and not doing so while you are on the phone and thinking about how to prepare for the party later on) before answering seems to be a good idea as well.

Brettīs code would be the way to go in Perl without having to usie CGI.pm or some such module.

Andreas

aprinc

3:42 pm on Jan 19, 2003 (gmt 0)

10+ Year Member



Hi again
Nothing of this did not help. Can not use function foreach()
because I use php3. Is there are any function or workaround
that will work in my case.
And how can I send key value pairs
<input type=text name="f_name" value="$first_name => ADI">
or..
And I know the name of the key but need only value.

toadhall

7:01 am on Jan 20, 2003 (gmt 0)

10+ Year Member



aprinc

Your form will look something like:

<form method=post action="process.php">
First Name: <input type=text name="f_name"><br>
Last Name: <input type=text name="l_name"><br>
<input type=submit>
</form>

Pick up the values with the script "process.php" using the variables $f_name and $l_name

Don't forget the link Andreas gave you, [php.net...] , to learn more from the php site.

Choose a language you're comfortable with from the links at the top of that page.

T