LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2007, 08:56 AM   #1
girish_hilage
Member
 
Registered: Oct 2003
Posts: 78

Rep: Reputation: 15
problem is posting variables in CGI script


Hi,

I am wrinting one CGI script.
The HTML screen it creates has 2 elements on it (1 text box and 1 submit button).
In the "form" I am using POST method.

The problem is that, when I click on the submit button the variables for text box and the submit button are not getting posted.
I checked it by inserting the command "export > /tmp/environment" command in the CGI script that I am calling from the browser.
The variables that are supposed to be posted should be part of the environment and should appear in the file /tmp/environment. But
I could not find them in this file.

However, If I use GET method then I could see the variables and their values in the QUERY_STRING variable.

Can anybody suggest why this is happening and am I missing something here?

Regards,
Girish
 
Old 05-03-2007, 09:17 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
not a networking question. moved to programming.
 
Old 05-03-2007, 09:34 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I belive that data transfer with the POST method is available to the CGI on its STDIN, not in the environment. Try reading STDIN, until eof().
--- rod.

Last edited by theNbomr; 05-03-2007 at 09:35 AM.
 
Old 05-04-2007, 02:04 AM   #4
girish_hilage
Member
 
Registered: Oct 2003
Posts: 78

Original Poster
Rep: Reputation: 15
My script (test.cgi) is as follows :

#!/bin/bash

export > /tmp/environment
read -s arg1 arg2
echo "arg1=$arg1" > /tmp/args
echo "arg2=$arg2" >> /tmp/args

echo "Content-type: text/html"
echo ""
echo "<html>"
echo "<head>"
echo "<title>Test CGI!</title>"
echo "</head>"
echo "<body>"
echo "<form name=INPUT_FORM method=POST action=test.cgi>"
echo "Enter any text :"
echo "<input type=text name=text_box size=16>"
echo "<input type=submit value=OK name=submit_btn>"
echo "</form>"
echo "</body>"
echo "</html>"

exit 0

And I am getting following output in the file /tmp/args
arg1=text_box=hello+world&submit_btn=OK
arg2=

How can I get above values in separate variables?
 
Old 05-04-2007, 03:43 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
the problem is, that the stdin comes in as one long line
like:
Code:
 text_box=hello&submit_btn=OK
but has no terminating linefeed, hence the read never
completes.

I would strongly advise maybe using perl's CGI module as it's much safer than trying yourself.
Still here is a solution of sorts...
(I've tidied up those horrible echoes

Code:
#!/bin/bash
cat <<EOF
Content-type: text/html

<html>
<head>
<title>Test CGI!</title>
</head>
<body>
<form name=INPUT_FORM method=POST action=test.cgi>
Enter any text :
<input type=text name=text_box size=16>
<input type=submit value=OK name=submit_btn>
</form>
EOF


IFS=\&          # ampersands that delimit parameter=value pairs
set -- `cat`    # a trick to assign pairs to $@ variables
for x;do
    var=${x%=*}
    val=${x#*=}
    echo "<p>$var is:$val</p>"
done




cat << EOF
</body>
</html>
EOF

exit 0

Last edited by bigearsbilly; 05-04-2007 at 03:45 AM.
 
Old 05-04-2007, 05:15 AM   #6
girish_hilage
Member
 
Registered: Oct 2003
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks for your reply. Its working fine. But I still think there is some other way by which we can directly read the values of the respective variables.
 
Old 05-04-2007, 05:27 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I think you are in for a disappointment
 
Old 05-04-2007, 09:28 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by girish_hilage
Thanks for your reply. Its working fine. But I still think there is some other way by which we can directly read the values of the respective variables.
If you think about it, in your scenario, how could the second argument ever get read? If the argument list is separated by eofs, then only the first argument could ever get read. Alternatively, the argument list is separated by '&'s, which is left up to your code to interpret. When the data is a stream, you can only ever read it all, and then break it up as appropriate.

--- rod.
 
Old 05-04-2007, 10:07 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
agreed.
if it's POST it's a line on stdin
if it's GET its in $QUERY_STRING

them's the rules, we don't make 'em.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CGI-Script Problem shipon_97 Linux - Newbie 1 05-09-2006 02:30 AM
CGI-script problem on Apache2 jkt2000 Linux - Networking 3 01-28-2005 04:38 AM
Posting to CGI Forms goldcougar Linux - Newbie 1 07-02-2004 10:13 AM
CGI Posting Probs... stoney79 Programming 1 04-19-2004 10:16 AM
POSTing to Cgi binaries nodger Programming 1 01-23-2004 09:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration