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

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.

hi this task can be done by two ways 1.) display data using ajax when second radio button clicked 2.) display data by refreshing page when second radio button is clicked

replay your choice how’s you want to display data… According to that i will provide you answer which is fulfill your requirement

replay your feedback
~SR~

@krma: Thank you very very much.

Please can you provide me the answere for using ajax.
Actually with the radio button i have few parameters which user will select, those values also i can display from the database right?

Please dont mind for my silly questions. Here is my code

[code]

Rent      

----Select Budget---- 5000 5000 to 10000 11000 to 20000 Above 20000

Sale


----Select Budget---- 100000 500000 1000000 2500000

----Select Location--- All Location Central Mumbai Mumbai Harbour Mumbai Navi Mumbai South Mumbai Thane
-----Select Area--- All Area Bhandup Chembur Kurla Mulund All Area Byculla Chembur Govandi Sewri Wadala All Area Airoli Belapur Ghansoli Mahape Nerula All Area Churchgate CST Dadar Fort All Area Brindavan Kalothe Kapur Kalwa Kopat
---Select Bedroom--- 1 BHK 2 BHK 3 BHK 3 BHK

[/code]
these parameters i want to search in my database

hi here is your solution

first file is testform.php have all form data
second is testajax.php wihch is call when search button is clicked. this page contain your logic how to get data from database based on posted data.

Put both files on same folder and run testform.php and clicked the search button, check the response.
[php] // save this file as testform.php Sanjay Rao Logic Rent Sale
----Select Budget For Rent---- 5000 5000 to 10000 11000 to 20000
<option value="above_20000">Above 20000</option>
----Select Budget For Sale---- 100000 500000 1000000
<option value="above_20000">2500000</option>

----Select Location--- All Location Central Mumbai Mumbai Harbour Mumbai Navi Mumbai South Mumbai Thane

-----Select Area--- All Area Bhandup Chembur Kurla Mulund All Area Byculla Chembur Govandi Sewri Wadala All Area Airoli Belapur Ghansoli Mahape Nerula All Area Churchgate CST Dadar Fort All Area Brindavan Kalothe Kapur Kalwa Kopat

---Select Bedroom--- 1 BHK 2 BHK 3 BHK 3 BHK





[/php]

[php]
// save this file as testajax.php
//here i have show the data posted on search button clicked.
echo “

”;
print_r($_POST);
// echo $_POST[‘table’]; # will tell you from which table you want to get data.

if($_POST[‘table’] == ‘rent_table’)
{
// Get data from rent table(table 1)
// put your logic here for table 1
}

if($_POST[‘table’] == ‘sale_table’)
{
// Get data from sale table(table 2)
// put your logic here for table 2
}

//put your logic here for get data from data base on received posted data.
exit;

[/php]

Hope this will helpfull for you.
replay your feedback for further help or query.

SR

Hi,

Thanks a ton

I tried it by putting my query/logic to display data but its giving error :frowning:

Here is my query

[php]<?php

$host=“localhost”; // Host name

$db_name=“netelmbn_realestate”; // Database name
$tbl1_name=“rent_table”; // Table1 name
$tbl2_name=“sale_table”; //Table2 name
mysql_connect(“localhost”,“netelmbn”,“xF^Lj(pLr_eX”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

echo “

”;
print_r($_POST);

if($_POST[‘table’] == ‘rent_table’)
{
$result=mysql_query("select * from rent_table where location=’$location’ AND area=’$area’ AND bedroom=’$bedroom’ AND $budget=‘budget’ ");
echo “

”;
			echo '<tr>'; 

while ($row = mysql_fetch_assoc($result)) {
echo "

”;
}
echo “
Budget: ".$row[‘budget’]."
Location: ".$row[‘location’]."
Area: ".$row[‘area’].“
BHK: “.$row[‘bhk’].”
”;
}

if($_POST[‘table’] == ‘sale_table’)
{
$result=mysql_query("select * from sale_table where location=’$location’ AND area=’$area’ AND bedroom=’$bedroom’ AND $budget=‘budget’ ");
echo “

”;
			echo '<tr>'; 

while ($row = mysql_fetch_assoc($result)) {
echo "

”;
}
echo “
Budget: ".$row[‘budget’]."
Location: ".$row[‘location’]."
Area: ".$row[‘area’].“
BHK: “.$row[‘bhk’].”
”;
}
while ($row = mysql_fetch_assoc($result)) {
echo " Budget: ".$row[‘budget’]."
Location: ".$row[‘location’]."
Area: ".$row[‘area’].“
BHK: “.$row[‘bhk’].”
”;
}
echo “”;
exit;[/php]

and i am attaching the error which i got when i run this.
i am confused whether i am querying correctly or not. Please suggest me
error: Please check the url below
http://netelity.com/realestate/error.png

Hey i tried to correct by putting

$result = mysql_query(…);
if (!$result) {
echo mysql_error();
die;
}

but always it takes salesvalue is 5000 i mean budget. where i am wrong i do not know and

please have a look

i have checked your code and found that there is sql syntax error in your code near budget code you are use. Also i found you are also using large code. I have refine your code. i hope this will work perfect for you. One more thing please use this hidden input box in your form which is i have added additional in form if you are missing this than add this one. [php]
$host="localhost"; // Host name 
$db_name="netelmbn_realestate"; // Database name 
$tbl1_name="rent_table"; // Table1 name  
$tbl2_name="sale_table"; //Table2 name 
mysql_connect("localhost","netelmbn","xF^Lj(pLr_eX")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

$location = $_POST['location'];
$area = $_POST['area'];
$bedroom = $_POST['bedroom'];
$resp = '';
if($_POST['table'] == 'rent_table')
{
	$rentbudget = $_POST['rentvalue'];
	$sql = "select * from ".$tbl1_name." where location='".$location."' AND area='".$area."' AND bedroom='".$bedroom."' AND budget='".$rentbudget;
}

if($_POST['table'] == 'sale_table')
{
    $salebudget = $_POST['salevalue'];
    $sql = "select * from ".$tbl2_name." where location='".$location."' AND area='".$area."' AND bedroom='".$bedroom."' AND budget='".$salebudget;
}

$result=mysql_query($sql);
$resp .= "<table cellpadding='20'>";
$resp .= '<tr>'; 
while ($row = mysql_fetch_assoc($result)) 
{ 
	$resp .= "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
$resp .= "</tr></table>";

print_r($resp)
exit;

[/php]

Reply your feedback
~~ SR~~

Hi,

I am extremely sorry for asking too silly questions.
I will surely learn everything from scratch. Please help me in this

Actually it is giving error : Parse Error : unexpected string EXIT_T …

i checked but all the loops are closed properly. If i remove print and exit statement and placed echo then it says

Data Loaded:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/netelmbn/public_html/realestate/testajax.php on line 33

please do this. Note : database must be connected please check.

[php]
#Replace $sql with below
$sql = “select * from rent where location=’”.$location."’ AND area=’".$area."’ AND bedroom=’".$bedroom."’ AND budget=’".$rentbudget."’";

#replace mysql_fetch_assoc($result) with
mysql_fetch_array($result)
[/php]

Replay feedback
SR

Yes my database is connect and i have renamed my tables as rent_table and sale_table instead of rent and sale. So i think no need to change the query.

But still i changed the $sql as you directed, still that T_EXIT error persists :frowning:

ok do not worry, please do the below this as i explain here

Step 1:-

Create a new database having name “test” for testing.

After creating database run below sql for create “rent’’ table

CREATE TABLE IF NOT EXISTS rent (
id int(11) NOT NULL auto_increment,
location varchar(255) NOT NULL,
area varchar(255) NOT NULL,
bedroom varchar(255) NOT NULL,
budget varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


– Dumping data for table rent

INSERT INTO rent (id, location, area, bedroom, budget) VALUES
(1, ‘Central_Mumbai’, ‘Bhandup’, ‘2BHK’, ‘5000’);

Run below sql for create “sale’’ table

CREATE TABLE IF NOT EXISTS sale (
id int(11) NOT NULL auto_increment,
location varchar(255) NOT NULL,
area varchar(255) NOT NULL,
bedroom varchar(255) NOT NULL,
budget varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


– Dumping data for table sale

INSERT INTO sale (id, location, area, bedroom, budget) VALUES
(1, ‘Mumbai_Harbour’, ‘Chembur’, ‘3BHK’, ‘5000_10000’);

#Save below File as “testsrform.php”

[php]

#Save below File as “testsrform.php”

Sanjay Rao Logic
Rent Sale
----Select Budget For Rent---- 5000 5000 to 10000 11000 to 20000
<option value="above_20000">Above 20000</option>
----Select Budget For Sale---- 100000 500000 1000000
<option value="above_20000">2500000</option>

----Select Location--- All Location Central Mumbai Mumbai Harbour Mumbai Navi Mumbai South Mumbai Thane

-----Select Area--- All Area Bhandup Chembur Kurla Mulund All Area Byculla Chembur Govandi Sewri Wadala All Area Airoli Belapur Ghansoli Mahape Nerula All Area Churchgate CST Dadar Fort All Area Brindavan Kalothe Kapur Kalwa Kopat

---Select Bedroom--- 1 BHK 2 BHK 3 BHK 4 BHK





[/php]

Save below File as “testsrajax.php”
// please add your database detail mysql_connect(“localhost”,“YOUR DATABASE USER NAME”,“YOUR DATABASE USER PASSWORD”);
[php]

##Save below File as “testsrajax.php”

$db = mysql_connect(“localhost”,“root”,“root”); // please add your database detail mysql_connect(“localhost”,“YOUR DATABASE USER NAME”,“YOUR DATABASE USER PASSWORD”);
mysql_select_db(“test”); // if you create database name

// table name must be rent and sale because i hvae used them below in query.

$location = $_POST['location'];
$area = $_POST['area'];
$bedroom = $_POST['bedroom'];
$resp = '';
if($_POST['table'] == 'rent_table')
{
	$rentbudget = $_POST['rentvalue'];
	$sql = "select * from  rent where location='".$location."' AND area='".$area."' AND bedroom='".$bedroom."' AND budget='".$rentbudget."'";
}
elseif($_POST['table'] == 'sale_table')
{
	$salebudget = $_POST['salevalue'];
	$sql = "select * from  rent where location='".$location."' AND area='".$area."' AND bedroom='".$bedroom."' AND budget='".$salebudget."'";
}

$result=mysql_query($sql,$db);
$number=mysql_num_rows($result); 

$resp .= "<table cellpadding='20'>";
$resp .= '<tr>'; 
if($number >  0)
{
	while($row = mysql_fetch_array($result)) 
	{ 
		$resp .= "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bedroom']."<br></td>";
	}
}
else
{
	$resp .='<td> no record found</td>';
}
$resp .= "</tr></table>";
print_r($resp);
exit;

[/php]

Put both file(testsrform.php, testsrajax.php) at same folder.

DO BELOW THING

For rent table
//Now run the “testsrform.php” than click rent radio button.
// select 5000 from select budget for rent dropdown.
// select Central Mumbai from location dropdown.
// select Bhandup from area dropdown.
//select 2 BHK from Bedroom dropdown.

For sale table

//Now run the “testsrform.php” than click sale radio button.
// select 500000 from select budget for rent dropdown.
// select Mumbai Harbour from location dropdown.
// select Chembur from area dropdown.
//select 3 BHK from Bedroom dropdown.

Reply your feedback
~SR~

Hi,

Thanks

Here i have added <form name="srajax" method="post" action="testsrajax.php"> to submit data and <input type="submit" name="search" id="srajax" value="Search"/> changed type, button to submit, still it gives me the error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/netelmbn/public_html/test/testsrajax.php on line 23
no record found

if i take out mysql_num_rows(), it returns no records even if i select all the parameters which are there in the tables.

Do the below thing and if possible than provide your rent and sale table data. So i can provide you exact solution..

[php]
#add
echo $sql;
#before
$result=mysql_query($sql,$db);

/* Run the sql in your database and check it.
one important thing i have treated location, area, bedroom and budget as varchar in database.
so location,area, bedroom and budget not be other than varchar in there database structure.

I think you are treating one of field as integer or other than varchar
*/
[/php]

SR

Table structure for table rent

CREATE TABLE IF NOT EXISTS `rent` ( `id` int(11) NOT NULL auto_increment, `location` varchar(255) NOT NULL, `area` varchar(255) NOT NULL, `bedroom` varchar(255) NOT NULL, `budget` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Table structure for table sale
CREATE TABLE IF NOT EXISTS sale (
id int(11) NOT NULL auto_increment,
location varchar(255) NOT NULL,
area varchar(255) NOT NULL,
bedroom varchar(255) NOT NULL,
budget varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

I tried putting echo $sql;
but it gives “no records found”

hi do this i have forget one thing last time so please do this. It will return the sql query. than run this query in your data base. if it generate any error or empty row. post this query here her i want to see that one

[php]
#add
echo $sql;
exit;
#before
$result=mysql_query($sql,$db);
[/php]

SR

It returns an empty row.

I feel its not entering into if statement itself.

Am sorry if i am irritating you :frowning:

hi [php] echo $sql; exit; // please give me the sql query your getting from ajax. [/php]

~SR~

Hi,

here you can see the result.

http://www.netelity.com/test/testform.php

no results display, only blank screen

i have seen the URL given by you.

please do the below changes
1.) remove or comment this line in your form
2.) remove or comment
3.) please put correct ajax script name in stead of test_vasu.php , i think your ajax script name is testsrajax.php so please use this file.

add replace your testsrajax.php file code with below code.

[php]
$db = mysql_connect(“localhost”,“root”,“root”); // please add your database detail mysql_connect(“localhost”,“YOUR DATABASE USER NAME”,“YOUR DATABASE USER PASSWORD”);
mysql_select_db(“test”); // if you create database name

// table name must be rent and sale because i hvae used them below in query.

$location = $_POST['location'];
$area = $_POST['area'];
$bedroom = $_POST['bedroom'];
$resp = '';
if($_POST['table'] == 'rent_table')
{
	$rentbudget = $_POST['rentvalue'];
	$sql = "select * from  rent where location='".$location."' AND area='".$area."' AND bedroom='".$bedroom."' AND budget='".$rentbudget."'";
}
elseif($_POST['table'] == 'sale_table')
{
	$salebudget = $_POST['salevalue'];
	$sql = "select * from  rent where location='".$location."' AND area='".$area."' AND bedroom='".$bedroom."' AND budget='".$salebudget."'";
}
echo $sql; exit;

[/php]
do the needed modification and reply soon… i will check this changes on previously given URL.

SR

Sponsor our Newsletter | Privacy Policy | Terms of Service