LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-21-2004, 12:31 PM   #1
zgauthier
Member
 
Registered: Aug 2004
Distribution: Slackware 10.1
Posts: 45

Rep: Reputation: 15
Multi-variable CGI


I've been slaving over google for what seem like days now and have yet to find a way to pass multiple variables to a cgi script. What I've got set up so far is an html document that has two forms on it. The user enters things into the forms and clicks submit. It then sends these variables to my cgi script. At this point this is what I have.


HTML Frontend
Code:
<HTML>


<FORM METHOD=get

ACTION="/cgi-bin/new.cgi">

<input type=string name=input>

<input type=string name="input1">

<input type=submit value="Submit">

</form>

</HTML>

CGI Script
Code:
#!/bin/bash

echo "Content-type: text/html"

echo ""

echo "<HTML>"

  host="`echo $QUERY_STRING | awk -F= '{print $2}'`"

  junk="`echo $host | awk -F& '{print $1}'`"

  test="`echo $QUERY_STRING | awk -F= '{print $3}'`"

  echo "<blockquote><PRE>"

  echo "$junk is to $test"

  echo "</pre></blockquote>"


echo "</HTML>"

exit 0
I think this would work if I could get awk to use & as a seperator, but I've only had success with = for some reason. Right now that code generates " is to dog" if cat and dog are entered into each input box.

Last edited by zgauthier; 11-21-2004 at 12:33 PM.
 
Old 11-21-2004, 01:40 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Try this - remember to use <body> tags and that the type is text not string:
Code:
<html>
 <body>
  <form method="GET" action="/cgi-bin/new.cgi">
   <input type="text" name="input">
   <input type="text" name="input1">
   <input type="submit" value="Submit">
  </form>
 </body>
</html>
This code should work for the backend:
Code:
#!/bin/bash

echo -e "Content-type: text/html\n\n<html><body>"

IFS="&"
for pair in $QUERY_STRING;do
 name=`echo $pair | awk -F= '{print $1}'`
 value=`echo $pair | awk -F= '{print $2}'`
 echo "$name is $value<br>"
done

echo "</body></html>"

exit 0
 
Old 11-21-2004, 01:43 PM   #3
zgauthier
Member
 
Registered: Aug 2004
Distribution: Slackware 10.1
Posts: 45

Original Poster
Rep: Reputation: 15
Thanks a lot man. I did come up with a solution shortly after posting this. However it's not nearly as clean as yours.

This is what I came up with.
Code:
#!/bin/bash

echo "Content-type: text/html"

echo ""

echo "<HTML>"

#These lines love each other...

  junk="`echo $QUERY_STRING | awk -F= '{print $2}'`"

  var="`echo $junk | awk -F'&' '{print $1}'`"

#End of line love

  var1="`echo $QUERY_STRING | awk -F= '{print $3}'`"

  echo "<blockquote><PRE>"

  echo "$var is to $var1"

  echo "</pre></blockquote>"


echo "</HTML>"
 
Old 11-21-2004, 02:25 PM   #4
zgauthier
Member
 
Registered: Aug 2004
Distribution: Slackware 10.1
Posts: 45

Original Poster
Rep: Reputation: 15
OK I thought this would work out great, but now it's driving me insane.

I'm setting up my box so people can give 3 inputs, frequency, duration, and receptions. I'm trying to write a script that will pass that data to the beep program that beeps my motherboard speaker. I had this working fine when it only took one variable. Something here is messed up. When the html frontend calls this page http://z4x.net:81/cgi-bin/private/se...epetitions=334 I get this for an output "2500 500 334 2500 500 334 2500 500 334". Now, why in the world is it repeating 3 times?


This is my adaptation of your example script.

Code:
#!/bin/bash

echo -e "Content-type: text/html\n\n<html><body>"

IFS="&"
for pair in $QUERY_STRING;do
 hz=`echo $pair | awk -F= '{print $2}'`
 echo "$hz"
done

for pair in $QUERY_STRING;do
 ms=`echo $pair | awk -F= '{print $2}'`
 echo "$ms"
done

for pair in $QUERY_STRING;do
 reps=`echo $pair | awk -F= '{print $2}'`
 echo "$reps"
done


echo "</body></html>"


exit 0
 
Old 11-21-2004, 02:33 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Because you have 3 for loops. Just use the one for loop like my example and test for what $name is to find out what veriable you are on.
 
Old 11-21-2004, 03:56 PM   #6
zgauthier
Member
 
Registered: Aug 2004
Distribution: Slackware 10.1
Posts: 45

Original Poster
Rep: Reputation: 15
OK this is pissing me off, can we say that here? I can get it to spit out all the names, or all the values, but no matter what I try, I can't get it to just give me one value. Even awking $value for $1 is all messed up. I need it to assign each individual value as a variable, not all 3 as one. It seems what you gave me would work if there was only one pair of values in the query string, but I need to use more than that.
 
Old 11-23-2004, 12:56 PM   #7
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
You can pick out the variable you want by testing $name in an if statement for the name you want.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Boot Multi Windows & Multi Linux olkar Linux - Newbie 5 10-03-2005 11:52 AM
How do you change card# of multi multi-media cards. Nichole_knc Slackware 3 12-01-2004 12:49 PM
CGI to work out side of cgi-bin? crashedspine Linux - Newbie 13 09-02-2004 08:49 PM
GRUB, Multi Linux/Multi Disk Boot gtnorton Linux - Software 1 03-16-2003 03:48 AM
http://www.burstnet.com/cgi-bin/ads/ad7954a.cgi/3980/RETURN-CODE rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:37 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