Am I thinking the right way? Using radio button to search...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Am I thinking the right way? Using radio button to search...

Post by crazytopu »

Hi,

this is all to do with search. People search book by call_no, author, title...etc.


I am running into a problem. when i tried to write the code for this search functions..here i explain it in details...


i let the users to select one of the 5 radio buttons namely call_no, author, isbn, category,title.

I place 5 text box, one against each radio button to take the value that the user enters after he made the selection.

Then he hits the search button...my code first checks which radio button is selected, and then takes the value from the corresponding text box and send it to my php file that will process back the result and display it to the users.

so, depending on the selection my sql statement will change:

when the selected radio button is call_no it would be:

Code: Select all

<?php 
$sql = "Select * FROM book WHERE call_no=('{$_POST['call_no']}')"; 

?>



when the selected radio button is author it would be:

Code: Select all

<?php 
$sql = "Select * FROM book WHERE call_no=('{$_POST['author']}')"; 

?>




and so on........................


Now, i m not sure how to let my code to check which radio button is selected.

the following code i have copied from my html page:

Code: Select all

<?php 

<td width="15%"> 
      <p align="right"><input type="radio" value="V1" checked name="R1"></p> 
    </td> 
    <td width="6%">Call No</td> 
    <td width="51%"><input type="text" name="T1" size="20"></td> 
    <td width="57%"><a href="http://localhost/insertbook.php"><font color="#0000FF">Insert 
      Entry</font></a></td> 
  </tr> 
  <tr> 
    <td width="15%"> 


    <p align="right"><input type="radio" name="R1" value="V2"> 


    </td> 
    <td width="15%"> 

Author 

?>




Here, the first radio button is selected by default, so it has an extra property PHP:

<?php checked name="R1" ?>




I wonder if i should let the code to check "checked" value...

Am i thinking the right way?

If you know more specific and more effective way please let me know.

Code: Select all

// Added PHP tags for eyecandy. --JAM
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

For your SELECT to work, the radio buttons should all have the same name but different values.. this insures that only one is selected.
Like this...

Code: Select all

<input type="radio" name="author" value="nobel-2003">J.M. Coetzee
<input type="radio" name="author" value="nobel-2002">Imre Kertész
<input type="radio" name="author" value="nobel-2001">V.S. Naipaul
<input type="radio" name="author" value="nobel-2000">Gao Xingjian
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

I want to write a php code which will do something like this:



if(selected==call_no) //here call_no is the name of my radio button
{

require(http://localhost/call_no_search.php);//this php file displays the search results

}

else if(selected==author)
{

require(http://localhost/author_search.php);

}

else if(selected==isbn)
{

require(http://localhost/isbn_search.php);

}



Did anybody do it before? or similar type of stuff?
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

I want to write a php code which will do something like this:

Code: Select all

<?php

if(selected==call_no) //here call_no is the name of my radio button
{

require(http://localhost/call_no_search.php);//this php file displays the search results

}

else if(selected==author)
{

require(http://localhost/author_search.php);

}

else if(selected==isbn)
{

require(http://localhost/isbn_search.php);

}

?>
Did anybody do it before? or similar type of stuff?
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

Post by mwong »

switch statement could be used

Code: Select all

switch($selected) &#123;

case 'call_no':
//search call_no
break;

case 'author':
//search author
break;

case 'isbn':
//search isbn
break;

&#125;
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

thanks for your help..but i am not worried about the if else or switch statement.

I am yet to figure out how to check which radio button is selected. Is there any function in php?

i have just selected my first radio button so its property got changed to

Code: Select all

<td width="9%"><input type="radio" value="V1" name="R1" checked></td>
from

Code: Select all

<td width="9%"><input type="radio" value="V1" name="R1"></td>

As u can see a new word has been added " checked".
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

I am almost close to the solution just need a bit more help:

Code: Select all

<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 



if($R1=='V1')&#123;   // R1 my radio button and V1 is its value


$sql = "Select * FROM book WHERE call_no=('&#123;$_POST&#1111;'call_no']&#125;')";&#125;

if($R1=='V2')&#123;

$sql = "Select * FROM book WHERE author1=('&#123;$_POST&#1111;'author']&#125;')";
&#125;

if($R1=='V3')&#123;

$sql = "Select * FROM book WHERE title=('&#123;$_POST&#1111;'title']&#125;')";
&#125;

if($R1=='V4')&#123;

$sql = "Select * FROM book WHERE category=('&#123;$_POST&#1111;'category']&#125;')";
&#125;

if($R1=='V5')&#123;

$sql = "Select * FROM book WHERE isbn=('&#123;$_POST&#1111;'isbn']&#125;')";
&#125;


$result = mysql_query($sql); 

?> 


<table border=2> 
  <tr> 
    <td>call_no</td> 
    <td>title</td> 
    <td>author1</td> 
    <td>author2</td> 
    <td>publisher</td> 
    <td>isbn</td> 
    <td>price</td> 
    <td>no_of_page</td> 
    <td>edition</td> 
    <td>category</td> 
  </tr> 


<?php 


while ($myrow = mysql_fetch_row($result)) 
&#123; 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   &#123; 
      echo '<td>'.$myrow&#1111;$i].'</td>'; 
   &#125; 
   echo '</tr>'; 
&#125; 
?> 

</table>


Just need a bit help...with this code

Code: Select all

if($R1=='V1')
what's wrong with this if statement? should i use isset function?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Continued in another thread.
viewtopic.php?t=16343
Post Reply