Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    92
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Retreiving the result from the database when the radiobutton selected by the user

    Hi,

    I am trying to retrieve the values stored in my database when the user selects the data from the selection list and also i am using 2 radio buttons, when 1st radio button is checked. When 1st radio button is checked , get the data from table1 and if the 2nd radio button selected get the data from table2. How can i do it?

    Please somebody help me.

  • #2
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    Something like this will do it:

    In your HTML:
    Code:
    <input type="radio" name="whichTable" value="1">Use Table 1</input>
    <input type="radio" name="whichTable" value="2">Use Table 2</input>
    And in your PHP:
    PHP Code:
    $whichTable htmlentities($_REQUEST["whichTable"], ENT_QUOTES|ENT_HTML401);  //Use htmlentities to prevent code injections.  Never use "naked" $_REQUEST - you might as well say "hack me" on your website!

    switch ($whichTable)
    {
      case 
    1:
        
    //Code to use table 1.
        
    break;

      case 
    2:
        
    //code to use table 2.
        
    break;

      default:
        throw new 
    Exception("Something went wrong");

    Last edited by XmisterIS; 01-05-2012 at 11:45 AM.

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    92
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hey,

    Thank you very much.
    Actually with radio button i have few parameters which the user will selects, when user submits, it should search the value in the table(according to the radio button selected) and display the same. here i am posting my code.

    Sorry for my silly question.

    Please suggest me.

    here is my code:
    Code:
    <form  name="Menus" method="post" action="search.php">
    
      
        
    
    <input type="radio" id="rent" name="btn1" value="rent" />Rent &nbsp; &nbsp; &nbsp; 
    <select id="select1" style='display:none;'>
    <option>----Select Budget----</option> 
    <option value="5000">5000</option> 
    <option value="5000_10000">5000 to 10000</option> 
    <option value="11000_20000">11000 to 20000</option> 
    <option value="above_20000">Above 20000</option>
    </select>
    
    <input type="radio" id="sale" name="btn1" value="sale" />Sale <br /><br />
    <select id="select2">
    <option>----Select Budget----</option> 
    <option value="5000">100000</option> 
    <option value="5000_10000">500000</option> 
    <option value="11000_20000">1000000</option> 
    <option value="above_20000">2500000</option>
    </select>
    <script>
    function select1() {
        document.getElementById("select1").style.display="block";
        document.getElementById("select2").style.display="none";   
    }
    
    function select2() {
     document.getElementById("select2").style.display="block";
    document.getElementById("select1").style.display="none";             
    }    
    
    
    document.getElementById("rent").onclick = select1;
    document.getElementById("sale").onclick = select2;
    </script>
    
    <p><label> <select id="location" name="Location" onChange="populateSub(this, this.form.Area);" > 
    <option>----Select Location---</option> <option value="All_Location">All Location</option> 
    <option value="Central_Mumbai">Central Mumbai</option> 
    <option value="Mumbai_Harbour">Mumbai Harbour</option> 
    <option value="Mumbai_Navi">Mumbai Navi</option> 
    <option value="Mumbai_South">Mumbai South</option> 
    <option value="Mumbai_Thane">Mumbai Thane</option> </select> </label><br />
    
    
    <select id="Area" name="Area"> 
    <option>-----Select Area---</option> 
    <option value="All Area">All Area</option> 
    <option value="Bhandup">Bhandup</option> 
    <option value="Chembur">Chembur</option> 
    <option value="Kurla">Kurla</option> 
    <option value="Mulund">Mulund</option>
    
    <option value="All Area">All Area</option> 
    <option value="Byculla">Byculla</option> 
    <option value="Chembur">Chembur</option> 
    <option value="Govandi">Govandi</option> 
    <option value="Sewri">Sewri</option> 
    <option value="Wadala">Wadala</option> 
    
    <option value="All Area">All Area</option> 
    <option value="Airoli">Airoli</option> 
    <option value="Belapur">Belapur</option> 
    <option value="Ghansoli">Ghansoli</option> 
    <option value="Mahape">Mahape</option> 
    <option value="Nerul">Nerula</option>
    
    <option value="All Area">All Area</option> 
    <option value="Churchgate">Churchgate</option> 
    <option value="CST">CST</option> 
    <option value="Dadar">Dadar</option> 
    <option value="Fort">Fort</option>
    
    <option value="All Area">All Area</option> 
    <option value="Brindavan">Brindavan</option> 
    <option value="Kalothe">Kalothe</option> 
    <option value="Kapur">Kapur</option> 
    <option value="Kalwa">Kalwa</option> 
    <option value="Kopat">Kopat</option> </select><br />
    
    <select id="bedroom" name="bedroom"> 
    <option>---Select Bedroom---</option> 
    <option value="1BHK">1 BHK</option> 
    <option value="2BHK">2 BHK</option> 
    <option value="3BHK">3 BHK</option> 
    <option value="4BHK">3 BHK</option> 
    </select><br /><br /> 
    
    
    <label><input id="search" type="submit" name="search" value="SEARCH" /></label>

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    We need to see your "search.php" script


    But to discuss more about your radio buttons,
    you need to give your select boxes names as well ...
    I also re-did the values of the select boxes so that you can get a low and high range when you process it ...

    <input type="radio" id="rent" name="btn1" value="rent" />Rent &nbsp; &nbsp; &nbsp;
    <select id="select1" name="select1" style='display:none;'>
    <option>----Select Budget----</option>
    <option value="0-5000">5000</option>
    <option value="5000-10000">5000 to 10000</option>
    <option value="10000-20000">10000 to 20000</option>
    <option value="20000-1000000">Above 20000</option>
    </select>

    <input type="radio" id="sale" name="btn1" value="sale" />Sale <br /><br />
    <select id="select2" name="select2">
    <option>----Select Budget----</option>
    <option value="0-100000">100000</option>
    <option value="100000-500000">500000</option>
    <option value="500000-1000000">1000000</option>
    <option value="1000000-2500000">2500000</option>
    </select>

    And then, I would query the values as shown.
    example:


    $sel1=explode("-",$_POST['select1']);
    $low1=$sel1[0];
    $high1=$sel1[1];
    $sel2=explode("-",$_POST['select2']);
    $low2=$sel2[0];
    $high2=$sel2[1];


    if($_POST['btn1']=="rent"){
    query ... blah blah ... WHERE `rent_value` > '$low1' AND `rent_value` <= '$high1' ...
    }
    else{
    query ... blah blah ... WHERE `sale_value` > '$low2' AND `sale_value` <= '$high2' ...
    }


    ... of course we have no idea what your MySQL column names are ...


    .
    Last edited by mlseim; 01-05-2012 at 07:18 PM.

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    92
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi,

    Thank you very much. Still i stuck up in displaying
    Please check my script.

    here is my search.php

    Code:
    <?php
    $host="localhost"; // Host name
    
    $db_name="database1"; // Database name
    $tbl1_name="rent"; // Table1 name 
    $tbl2_name="sale"; //Table2 name
    mysql_connect("localhost","admin","my3prasanna")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
    
    $sel1=explode("-",$_POST['select1']);
    $low1=$sel1[0];
    $high1=$sel1[1];
    $sel2=explode("-",$_POST['select2']);
    $low2=$sel2[0];
    $high2=$sel2[1];
    
    $location= $_POST['Location'];
    $area= $_POST['Area'];
    $bedroom= $_POST['bedroom'];
    
    $search=$_POST['rent'];
    $search1=$_POST['sale'];
    
    
    if($_POST['btn1']=="rent")
    {
    	
    $query1=mysql_query("select * from rent where location='$location' AND area='$area' AND bedroom='$bedroom' AND 'budget'>$low1 AND 'budget'<=$high1;")
    else
    $query=mysql_query("select * from sale where location='$location' AND area='$area' AND bedroom='$bedroom' AND 'budget'>$low2 AND 'budget'<=$high2;")
    
    }
     
    ?>
    and my table rent is like this:
    Code:
    CREATE TABLE `rent` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `location` varchar(40) NOT NULL,
      `area` varchar(40) NOT NULL,
      `bedroom` varchar(20) NOT NULL,
      `budget` varchar(20) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
    table sale
    Code:
    CREATE TABLE `sale` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `location` varchar(40) NOT NULL,
      `area` varchar(40) NOT NULL,
      `bedroom` varchar(8) NOT NULL,
      `budget` varchar(20) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
    When user selects radio button rent with other parameters it should search the data in 'rent' table and if and only if all the parameter matches, then only it show the result and if user selects radio button 'sale' with other parameter it should points to the sale database and it should search the data if all the data matches and display result.
    I am really not getting this
    Please help me
    Last edited by Mythri; 01-06-2012 at 06:50 AM.

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    92
    Thanks
    8
    Thanked 0 Times in 0 Posts
    mlseim: Please look into my code and please suggest me where i am wrong and how it can be done

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    The query uses different "single quotes" the odd one (`) next to the 1 key,
    and the normal one (') under the double quote. The odd one doesn't always
    need to be used, so I'll take that out in my example. It only has to be used if
    you use a reserved word for a column name.

    Try this ...

    PHP Code:
    <?php
    $host
    ="localhost"// Host name

    $db_name="database1"// Database name
    $tbl1_name="rent"// Table1 name 
    $tbl2_name="sale"//Table2 name
    mysql_connect("localhost","admin","my3prasanna")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");



    $sel1=explode("-",$_POST['select1']);
    $low1=$sel1[0];
    $high1=$sel1[1];
    $sel2=explode("-",$_POST['select2']);
    $low2=$sel2[0];
    $high2=$sel2[1];

    $location$_POST['Location'];
    $area$_POST['Area'];
    $bedroom$_POST['bedroom'];

    $search=$_POST['rent'];
    $search1=$_POST['sale'];


    if(
    $_POST['btn1']=="rent"){
    $result=mysql_query("select * from rent where location='$location' AND area='$area' AND bedroom='$bedroom' AND budget > '$low1' AND budget <= '$high1' ");
    }
    else{
    $result=mysql_query("select * from sale where location='$location' AND area='$area' AND bedroom='$bedroom' AND budget > '$low2' AND budget <= '$high2' ");
    }

    // test it out and see which locations were found ...
    while ($row mysql_fetch_assoc($result)) {
    echo 
    "Location: ".$row['location'];."<br />";
    }

    ?>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The grave accent is used to identify fields in your database, and is typically used when a conflict could exist with a reserved word. They do not take the place of single or double quotes.
    Code:
    'budget'>$low1 AND 'budget'<=$high1
    Single quotes are strings, not fields. There is no reasonable way to establish the above results since you are comparing the literal string 'budget' to a number. This can be written with `budget` > $low1 AND `budget` <= $high1, or you can drop the grave accent completely since budget is not a reserved word. Alternatively, you can always use grave accent to mark your fields and tables to ensure no conflict will arise.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Fou-Lu ...
    I never know what to call that character ( ` ) ...
    Usually I use "backquote", or "The thingy under the tilde - next to the 1 key".
    "grave accent" seems to be a better description. Although I do see that the grave
    accent is called "backquote or backtick" when referring to programming ...

    http://en.wikipedia.org/wiki/Grave_accent

    But then again, that's wikipedia ... not always the most accurate source.


    .

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    backtick is typically what I use to describe it as well. This is because of the use of "backtick operators" when calling executables.
    I'm not sure if backtick is actually a real alias to the grave accent or just one acknowledged as such by programmers. So I thought I better call it by its real name :P

  • #11
    New Coder
    Join Date
    Jan 2011
    Posts
    92
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I tried this, but no result is getting displayed Just a blank scree appears, even after selecting the same criteria which is there in the database.

    I do not know why this is happening

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Mythri View Post
    I tried this, but no result is getting displayed Just a blank scree appears, even after selecting the same criteria which is there in the database.

    I do not know why this is happening
    This is either because of a syntactical / parse error, or because of the resulting data.
    Easiest check is to add a simple echo 'done'; immediately before the ?>. If that shows, its a data problem, if not its parse.
    In the event that its syntactical, use error reporting:
    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL);
    At the top and it will show the cause.

    If the done shows, the query is wrong.
    Separate the queries into their own string variables, and feed that to the query:
    PHP Code:
    $sQry 'SELECT * FROM...';
    $result mysql_query($sQry) or die(mysql_error()); 
    this lets you print out the $sQry to show you what the parsed SQL will appear as (please look into 'php mysql injection prevention' as well - google should have lots of results for tutorials). Take that printed query and paste it into a dbms tool, PHPMyAdmin, or whatever is used by the control panel.

    From the looks of it, it will be the query causing the problems; it doesn't look like a syntactical issue.

  • #13
    New Coder
    Join Date
    Jan 2011
    Posts
    92
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I tried correcting it but i really do ot understand why it is giving this error


    Notice: Undefined index: select1 in /home/netelmbn/public_html/realestate/search.php on line 14

    Notice: Undefined offset: 1 in /home/netelmbn/public_html/realestate/search.php on line 16

    Notice: Undefined index: select2 in /home/netelmbn/public_html/realestate/search.php on line 17

    Notice: Undefined index: rent_table in /home/netelmbn/public_html/realestate/search.php on line 25

    Notice: Undefined index: sale_table in /home/netelmbn/public_html/realestate/search.php on line 26
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #2' at line 1

    here all the variables are declared properly

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What is the PHP code you are actually using? I could match up 3 of the undefined indexes, but not the last two, and the last error for SQL looks to me like you are trying to query a resultset instead of a string (if you separate it like I suggested, make sure you are not querying the first string).

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    The "notices" can be ignored for now. They are not fatal, but should be addressed
    later on. The fatal error is the mysql one.

    Can you repost your entire script again, since you've fixed the syntax errors?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •