Search based on radio button selection

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Search based on radio button selection

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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);
?>
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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. ;)
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Post by sergei »

How would I select one radio button over another in the same radiobutton group.

Example: both are named 'searchradio'
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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. ;)
Post Reply