PDA

View Full Version : ghenerate search string from form


scottish_drunk
01-29-2005, 03:12 AM
I have a form that executes a perl script. One of the inputs to the perl script is "q" the search query. I want to ask a list of questions where the user will answer by clicking one of two radio buttons per quesstion.


<FORM>
<INPUT TYPE="Radio" checked name="a" VALUE="answera" checked tabindex="1">
<INPUT TYPE="Radio" NAME="b" VALUE="=answerb">
</FORM>

<FORM>
<INPUT TYPE="Radio" checked name="c" VALUE="answerc" checked tabindex="1">
<INPUT TYPE="Radio" NAME="d" VALUE="=answerd">
</FORM>

.
.
.
.
<FORM>
<INPUT TYPE="Radio" checked name="y" VALUE="answery" checked tabindex="1">
<INPUT TYPE="Radio" NAME="z" VALUE="=answerz">
</FORM>

Finally I have this form:-


<FORM target="_blank" ACTION="./../cgi-bin/search.pl" METHOD="GET">
<INPUT TYPE="Submit" class="mainlinksfont" VALUE="Search">
<INPUT TYPE="Hidden" NAME="db" VALUE="1">
<INPUT TYPE="Hidden" NAME="t" VALUE="3">
<INPUT TYPE="Hidden" NAME="q" VALUE="??">
</FORM>

The problem I have for the final form is that I want 'q' to equal the value of the answers for the previous FORM
So When the user presses the button "search" the search string "q" will be "answera answerc answerf .....answerx"

I am at a loss on how to do this. Any help would certainly be appreciated.

allanm
02-02-2005, 06:18 PM
I was just answering something similar in a previous thread. Try something like this:

Try calling a function on each of your radio buttons that sets the value of 'q' once it's checked. For example:



//This is your javascript function
function setVar(pElem){
document.q.value = pElem.value;
}

//This is your form information
<INPUT TYPE="Radio" onclick="setVar(this)" name="a" VALUE="answera">

<INPUT TYPE="Radio" onclick="setVar(this)" name="b" VALUE="answerb">

//etc etc



hope that helps

scottish_drunk
02-02-2005, 09:49 PM
Thaks for the info. I think I saw the thread you were talking about but at least now I don't have to think too much to try it!!

One further question - I'm still not sure how to actually pass this into the PERL script as I have to use the following to pass in "q":-

FORM target="_blank" ACTION="./../cgi-bin/search.pl" METHOD="GET">
<INPUT TYPE="Submit" class="mainlinksfont" VALUE="Search">
<INPUT TYPE="Hidden" NAME="db" VALUE="1">
<INPUT TYPE="Hidden" NAME="t" VALUE="3">
<INPUT TYPE="Hidden" NAME="q" VALUE="??">
</FORM>



One other question:-

Do you know how to do the same thing using PHP?

The reason I ask is that this search will be one of the main features of my site and of course if someone turns off the JAVA support in their browser then the search won't work. With PHP there is no such possibility so PHP would be better in my case.

Again thanks for taking the time to help.

allanm
02-02-2005, 10:42 PM
No problem.

Firstly, I can't help you with the PERL question as I know nothing about the language. I'm guessing there is a dedicated PERL forum out there somewhere so give it a try there.

On the second question, turning off JAVA (or rather disabling Java applets) is different to turning off scripting. If you mean the latter then I would say to the users tough luck. It's not like your downloading ActiveX controls or anything so if you're paranoid about it then display a message of some kind..also, my guess is that this situation would be rare anyway. And if it's disabling applets your worried about, then don't worry. Javascript and Java are two different things.

scottish_drunk
02-03-2005, 08:16 PM
Thanks again for taking the time to respond.

I guess to be a little more specific. Is there a way to take this concatenated variable 'q' and pass it into an INPUT statement

I have <INPUT TYPE="Hidden" NAME="q" VALUE="??">

The variable q is what gets passed to the PERL script, I know the INPUT statement above does this. To make life easy for me, so I don't have to touch the PERL script(as I don't know the language either) I want to pass in the contents of this concatenated variable into where the ?? are in the input statement. Can this be done?

I hope this makes sense.

allanm
02-03-2005, 09:13 PM
yeah, sure.

You could use either of these:


//If you give your form a name then substitute
//the 'yourFormName' bit with your forms name
document.yourFormName.q.value = pElem.value;

or

//If you choose this one then make sure you put the id="q"
//to the input element
document.getElementById('q').value = pElem.value;


Is that what you mean?

darksidepuffin
02-03-2005, 09:41 PM
Originally posted by scottish_drunk

Do you know how to do the same thing using PHP?

You would have to submit each page of the form to the next one,keep that variable in a hidden form value,then at the last page conecenate and echo into the value of the hidden form field.


Do you wish to have this explained/an example given?(granted this isn't the php forum,I stumbled in here on a random tendency)

scottish_drunk
02-03-2005, 09:53 PM
First Allanm:-

I'll try it tonight I guess with your code the q var will get passed straight into the PERL script and I do not need the <INPUT TYPE="Hidden" NAME="q" VALUE="??">

demonicpuffin:-

An example would be good....

Thanks Guys.

darksidepuffin
02-03-2005, 10:06 PM
Heres how you'd do it...for each form you have...EXCEPT the last one..do:


<form action="next_form_page.php" method="get">
<input type="radio" name="answera" value="1"/>
<input type="submit" name="submit" value="submit"/>
</form>


On next_form_page.php..it would resemble:


<form action="next_form_page_2.php" method="get">
<input type="radio" name="answerb" value="1"/>
<input type="hidden" name="answera" value="<?=$_GET["answera"]?>"/>
<input type="submit" name="submit" value="submit"/>
</form>


and if you went up to,for example,answerc..the final submit page would be:


$q = $_GET["answera"].$_GET["answerb"].$_GET["answerc"];

header("Location: search.pl?q=$q");
exit;


hth,I'd post a better example..but I'm half way between dead on my feet and dead off of them.