Jump to content

Recommended Posts

I have three radio buttons each named searchType with

the value radio1, radio2, radio3. Also, there is a text field

named searchTerm.

 

I am trying to select the data that the user enters into the

text field and that is equal to the radio button.

 

How would I do this?

 

 

 

<?php

$sql = "SELECT user_name FROM members_info";
//$sql = "SELECT * FROM members_info WHERE user_name='$searchType'";

$res = mysql_query($sql);
$num_results = mysql_num_rows($res); 

for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($res);

  $user_name = htmlspecialchars( stripslashes($row["user_name"]));

echo "
<div class=\"friendtable\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\">
<tr>	
  <td class=\"image\">
<img src=\" \">
  </td>
  <td class=\"info\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">

<tr>
   <td class=\"name\">Name:</td>
   <td class=\"name\">$user_name</td>
   <td class=\"actions\"><a style=\"text-decoration:none;\" href=\"http://\">View Profile</a></td>
</tr>	
  
<tr>
   <td class=\"label\">
    Network:
   </td>
   <td>
     <a href=\" \">Lander '09</a><br />
   </td>

   <td class=\"actions\">
    <a style=\"text-decoration:none;\" href=\"http://\">Send Message</a>
    <a style=\"text-decoration:none;\" href=\"\">Poke Dolores!</a>
    <a style=\"text-decoration:none;\" href=\"http://\">View Friends</a>
    <a style=\"text-decoration:none;\" href=\"http://\">Add to Friends</a>
   </td>
</tr>

</table>

</td>
</tr>
</table>
		  
</div>";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/52908-solved-help-with-radio-button/
Share on other sites

Hrm, I don't think I fully understand what you mean. And your code partially confuses me, because searchType is in a mysql query as username? That would imply you only have 3 usernames, litterally names "radio1", "radio2", and "radio3" :P

 

What I think you might be trying to do is have 3 different queries, each searching something different, depending on what the radio selection is.

 

Is this correct?

Hrm, I don't think I fully understand what you mean. And your code partially confuses me, because searchType is in a mysql query as username? That would imply you only have 3 usernames, litterally names "radio1", "radio2", and "radio3" :P

 

What I think you might be trying to do is have 3 different queries, each searching something different, depending on what the radio selection is.

 

Is this correct?

 

That's it

Okay then :) Won't this work?

 

<?php

$searchType = $_POST['searchType'];
$searchTerm = $_POST['searchTerm'];

$sql = "SELECT * FROM members_info WHERE user_name='$searchType' AND something = '$searchTerm'";

?>

 

If not, how about something like this:

 

 

<?php

$searchType = $_POST['searchType'];
$searchTerm = $_POST['searchTerm'];

switch($searchType) {
    case radio1:
         $sql = some query...
         break;
    case radio2:
         $sql = some different query...
         break;
    case radio3:
         $sql = some other different query...
         break;
     default:
       $sql = not NEEDED, but nice to have a backup 
}

// carry on with your code

?>

 

o.O;  If that doesn't work, please provide a few more details, because I don't really know how anything is structured :)

Could get either to work! Here is the entire code.

 

HTML form

<form action="search_result.php" method="post">
<table width="100%" border="0" cellspacing="5" cellpadding="0">

<tr>
<td width="25%"class="gensmall">Search by:</td>
  <td class="gensmall">
    <input type="radio" name="searchType" value="radio1">Username 
<input type="radio" name="searchType" value="radio2">Email 
<input type="radio" name="searchType" value="radio3">State 
  </td>
</tr>
<tr>
<td> </td>
<td ><input type="text" name="searchTerm" size="35" ></td>
<td width="25%" ><input type="submit" name="search" class="submit" value="Search"></td>
</tr>
</table>
</form>

 

search_result.php

<?php
if (isset($_POST['search']))
// They have just tried the search
{

$sql = "SELECT user_name FROM members_info";
//$sql = "SELECT * FROM members_info WHERE user_name='$searchType'";

$res = mysql_query($sql);
$num_results = mysql_num_rows($res); 

for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($res);

  $user_name = htmlspecialchars( stripslashes($row["user_name"]));

echo "
<div class=\"friendtable\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\">
<tr>	
  <td class=\"image\">
<img src=\"http:// \">
  </td>
  <td class=\"info\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">

<tr>
   <td class=\"name\">Name:</td>
   <td class=\"name\">$user_name</td>
   <td class=\"actions\"><a style=\"text-decoration:none;\" href=\"http://\">View Profile</a></td>
</tr>	
  
<tr>
   <td class=\"label\">
    Network:
   </td>
   <td>
     <a href=\" \">Lander '09</a><br />
   </td>

   <td class=\"actions\">
    <a style=\"text-decoration:none;\" href=\"http://\">Send Message</a>
    <a style=\"text-decoration:none;\" href=\"\">Poke Dolores!</a>
    <a style=\"text-decoration:none;\" href=\"http://\">View Friends</a>
    <a style=\"text-decoration:none;\" href=\"http://\">Add to Friends</a>
   </td>
</tr>

</table>

</td>
</tr>
</table>
		  
</div>";

}
}
?>

Okay, You still didn't provide all the details I need (mysql table structure for example...) But, I'll try one more time :) Hopefully you can adjust it how you want.

 

<form action="search_result.php" method="post">
<table width="100%" border="0" cellspacing="5" cellpadding="0">

<tr>
<td width="25%"class="gensmall">Search by:</td>
  <td class="gensmall">
    <input type="radio" name="searchType" value="username">Username 
<input type="radio" name="searchType" value="email">Email 
<input type="radio" name="searchType" value="state">State 
  </td>
</tr>
<tr>
<td> </td>
<td ><input type="text" name="searchTerm" size="35" ></td>
<td width="25%" ><input type="submit" name="search" class="submit" value="Search"></td>
</tr>
</table>
</form>

 

<?php
if (isset($_POST['search']))
// They have just tried the search
{

$searchType = $_POST['searchType'];
$searchTerm = $_POST['searchTerm'];

switch($searchType) {
    case 'username':
         $sql = "SELECT user_name FROM members_info WHERE user_name='$searchTerm'
         break;
    case 'email':
         $sql = "SELECT user_name FROM members_info WHERE email='$searchTerm'
         break;
    case 'state':
         $sql = "SELECT user_name FROM members_info WHERE state='$searchTerm'
         break;
}

$res = mysql_query($sql);
$num_results = mysql_num_rows($res); 

for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($res);

  $user_name = htmlspecialchars( stripslashes($row["user_name"]));

echo "
<div class=\"friendtable\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\">
<tr>	
  <td class=\"image\">
<img src=\"http:// \">
  </td>
  <td class=\"info\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">

<tr>
   <td class=\"name\">Name:</td>
   <td class=\"name\">$user_name</td>
   <td class=\"actions\"><a style=\"text-decoration:none;\" href=\"http://\">View Profile</a></td>
</tr>	
  
<tr>
   <td class=\"label\">
    Network:
   </td>
   <td>
     <a href=\" \">Lander '09</a><br />
   </td>

   <td class=\"actions\">
    <a style=\"text-decoration:none;\" href=\"http://\">Send Message</a>
    <a style=\"text-decoration:none;\" href=\"\">Poke Dolores!</a>
    <a style=\"text-decoration:none;\" href=\"http://\">View Friends</a>
    <a style=\"text-decoration:none;\" href=\"http://\">Add to Friends</a>
   </td>
</tr>

</table>

</td>
</tr>
</table>
		  
</div>";

}
}
?>

 

 

I suggest not using "radio1" "radio2" etc for the names of your radio buttons...

 

I hope what I have done makes sense. Depending on the user choice, the code i offered runs 3 different mysql queries...

 

If you don't understand it, I suggest you look up SWITCH, and maybe other php tutorials because you might not understand all you need to.

 

Also, be careful with the email search box... special characters can cause issues :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.