Page 1 of 1

Search based on radio button selection

Posted: Tue Nov 04, 2003 7:53 am
by sergei
I have a simple html form containing a textbox to accept user input and two radio buttons.

I need to send the data to a database search I've written or alternatively, to an ecommerce product search engine that I use.

Does anyone know how to do this using the radio buttons.

Thanks.

Posted: Tue Nov 04, 2003 9:07 am
by JAM
I think this will guide you abit.

Code: Select all

<pre>
<form method="post">
<input type="text" name="text" value="Example" />
<input type="radio" name="theoption" value="Foo" checked />Foo</input>
<input type="radio" name="theoption" value="Bar" />Bar</input>
<input type="radio" name="theoption" value="Muu" />Muu</input>
<input type="radio" name="theoption" value="Kittythrow" />Kittythrow</input>
<input type="radio" name="theoption" value="Argh" />Argh</input>
<input type="submit" />
</form>
<?php
    if (!empty($_POST)) {
        echo 'Example Query:<br />';
        echo '$sql = "select data from table where something = '''.$_POST['text'].''' and another = '''.$_POST['theoption'].'''';
    }
    echo '<br />';
    print_r($_POST);
?>

Posted: Wed Nov 05, 2003 12:52 am
by sergei
Sorry. I wasn't clear in explaining my problem.

My radio button selection needs to do two things.

1) Send data to a sql statement if radio button 1 is 'checked'
2) Else, send data to ecommerce search engine (As you would using the normal action='search_engine_url.php')

This is probably really simple, but I can't see past step 2.

Any help would be greatly appreciated.

Posted: Wed Nov 05, 2003 2:19 am
by JAM
First idea was headers().

Pseudo code:

Code: Select all

if (radio1 == true) &#123;
  // query database
 &#125; else &#123;
  header('Location: http://www.example.com/search/index.php?q='.$_POST&#1111;'text_string']);
 &#125;
Result: http://www.example.com/search/index.php ... foo%20here
In the search-engine page you can later use $_GET['q'] (something%20foo%20here, using the example variable above), to get the string that you need to use in ther search clause.

Read up on:
http://se.php.net/manual/sv/function.header.php

Interesting also: (for transporting strings safely in the URI being one issue)
http://se.php.net/manual/sv/function.rawurlencode.php
http://se.php.net/manual/sv/function.serialize.php
http://se.php.net/manual/sv/function.base64-encode.php

Hope I gave better ideas now. ;)

Posted: Wed Nov 05, 2003 3:07 am
by sergei
How would I select one radio button over another in the same radiobutton group.

Example: both are named 'searchradio'

Posted: Wed Nov 05, 2003 3:54 am
by sergei
This is the code I have.

Code: Select all

&lt;form method="post"&gt;
                    &lt;br&gt;
                    &lt;input name="search_field" type=text class="searchboxes" size="15"&gt;
                    &lt;input type=hidden name="storeid" value="spint1"&gt;
                    
                    &lt;input name="submit2" type="image" value="Search" src="images/go1.gif" align="middle" width="28" height="30"&gt;
                    &lt;br&gt;
                    &lt;span class="searchboxtext"&gt;
                      &lt;input name="searchradio" type="radio" value="Archives" checked&gt;
                      Archives
                      &lt;input name="searchradio" type="radio" value="Products"&gt;
                      Products&lt;/span&gt;            
                    &lt;/form&gt;

Code: Select all

<?php

						$radio = $_POST['searchradio'];

						if ($radio == 'Archives')
						{
							// Gets send to the sql query (This is fine)
						}
						else if ($radio == 'Products')
						{
							// This gets sent to eccomerce site with url: http://spintelligent.com/shopsite_sc/sh ... eid=spint1 
						}

					?>
I don't have access to the eccomerce productsearch.cgi file, so I can't make any changes. How do I send data to this file. Without using headers. If I use headers, it brings back an error saying that the headers have already been sent.

Normally the form action would be set to point to the ecommerce URL

Thanks

Posted: Wed Nov 05, 2003 5:47 am
by JAM
$_POST['searchradio'] should contain either Archives or Products as value.
<input name="searchradio" type="radio" value="Products">
name = what you use when fetching the data in $_POST/$_GET.
value = what the name above contains.


The headers sent issue is discussed here: viewtopic.php?t=1157

Don't give up & test some more. ;)