This is my first question here as I just joined. I have had a hard time at stackoverflow because of their emphasis on formalities and not actually wanting to help even if I have shown all my work and all. I hope I don’t have the problem here.
Basically I’m building a property website and need to implement a simple drop down based on another drop down so that values available for drop down 2 are always going to be more than the values selected for drop down 1.
I have looked at so many sites and different tutorials and plugins and each give an option where the javascript/jquery/ajax has hard coded values or url or select options that are not at all flexible.
I was wondering where there was a way to do this so I can reuse the javascript/jquery/ajax (jja) plugin on all my drop downs without hardcoding anything on the jja.
Desired workflow
Select value in drop down 1
define a variable in drop down 2 or in mysql select that takes value from drop down 1 and limit the options or change the options as required
I’ve never written any jja but always modified the code available on tutorial sites and downloaded plugins and modified as required.
Can someone point me to a link or some code to be able to do this. Like I said, I want the jja to be reusable with multiple dropdowns so the code should not store values or selects
Ideally I would like the following for dropdown2 so that the mysql select statement will be able to use the dropdown1 selected value
<select name="maxprice" id="maxprice">
<?php
$rows = db_select("SELECT amount,price_word FROM price_change where max_<>'0' AND amount>= $dropdown1_value");
foreach($rows as $row){
echo “<option value='”.$row[‘amount’].“'>”.$row[‘price_word’].“”;
}
?>
</select>
Apologies if I have not shown enough information or formatted the question wrong, but I’d appreciate if someone would point it out rather than ridicule (as is the case with so)
I just want to point out the constructive criticism isn’t ridicule, it’s to help both sides (the person asking for help and the the person helping). So when someone asks to clarify or asks for more code it doesn’t mean that they are ridiculing you. Sometimes the response can seem a little harsh, just take it with a grain a salt. Trying getting a piece of work critique now that can seem like ridicule, but again it’s to help you improve you work.
I’m new to Ajax-style stuff, but my guess would be that you’d need to implement some javascript on change of dropdown1 value which runs your query and populates the values for dropdown2 based on the results. I’m not sure how much success you’d have in using the same javascript code for multiple different dropdowns, but if you pass the query into your javascript function that should work. Maybe you’ll spend so much time in the javascript function coping with the different things that might need to be dealt with that it works out easier to code each one. Something like this though, keeping in mind I don’t know much javascript:
<select onchange="runquery('select amount, price_word FROM price_change where max<>0 and amount>=' , this.options[this.selectedIndex].text, dropdown2);" id="dropdown1" name="dropdown1">
Maybe that would pass enough into the runquery() function for it to be able to call a generic query-run PHP program with the query, the selection criterion (the text from the selected item in the first dropdown) and the id of the selection to populate with the results, . Still not sure how you’d decide what to put into the second dropdown, though perhaps the query could be made to return it in the exact format required.
The more I think about it, the more I wonder whether there is a security issue with passing the query itself as part of the page source. Maybe it would be better to have a half-and-half approach, where you predefine the queries in your PHP code, and refer to them by an index value or name rather than revealing the table names and so on.
This works - http://www.plus2net.com/php_tutorial/dd.php but the only problem is that it runs a simulation of page refresh with new url which I don’t want. I basically want the URL to be the same. But this is a step in the right direction as it doesn’t hard code the values or selects or anything
I personally think it’s a bad practice to call or run query loops within html. There is also no need to make multiple queries from the same table. Just grab all values and build an array then use foreach to loop through the array. Here’s a basic PDO example.
<?php
//PDO connect
//MySQL Database user name.
$login = "";
//Password for MySQL.
$dbpass = "";
//MySQL Database name.
$dbname = "";
//Establish a connection
$db = new PDO("mysql:host=localhost;dbname=$dbname", "$login", "$dbpass");
// Query table for amounts and build array
// Asumming id is auto_increment and values are inserted in order
$sql = "SELECT
amount
, price_word
FROM price_change
ORDER BY id asc";
$query = $db->prepare($sql);
$query->execute();
// Build amounts array
$amounts = array();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$amounts[] = $row;
}
// Set POST minprice to variable
if(isset($_POST['minprice']) && !empty($_POST['minprice'])):
$minprice = $_POST['minprice'];
endif;
// Set POST maxprice to variable
if(isset($_POST['maxprice']) && !empty($_POST['maxprice'])):
$maxprice = $_POST['maxprice'];
endif;
?>
<html>
<body>
<form action="" method="post">
<select name="minprice" id="minprice" onchange="this.form.submit()">
<option value="">Select</option>
<?php
foreach($amounts as $arr):
$selected_min = (isset($minprice) && $minprice === $arr['amount'] ? ' selected="selected"' : '');
?>
<option value="<?php echo $arr['amount'];?>"<?php echo $selected_min;?>><?php echo $arr['price_word'];?></option>
<?php
endforeach;
?>
</select>
<select name="maxprice" id="maxprice">
<option value="">Select</option>
<?php
foreach($amounts as $arr):
if(isset($minprice) && $arr['amount'] > $minprice):
$selected_max = (isset($maxprice) && $maxprice === $arr['amount'] ? ' selected="selected"' : '');
?>
<option value="<?php echo $arr['amount'];?>"<?php echo $selected_max;?>><?php echo $arr['price_word'];?></option>
<?php
endif;
endforeach;
?>
</select>
</form>
</body>
</html>
// Query table for amounts and build array
// Asumming id is auto_increment and values are inserted in order
$sql = "SELECT
amount
, price_word
FROM price_change
ORDER BY id asc";
$query = $db->prepare($sql);
$query->execute();
Yes, I would think if you are “using mysqli” then you would know how to convert my PDO example to your query style. I don’t use mysqli but I would imagine it would be something like this.
<?php
$host = "localhost";
//Database user name.
$login = "";
//Database Password.
$dbpass = "";
//Database name.
$dbname = "";
$db = mysqli_connect("$host", "$login", "$dbpass", "$dbname");
// Query table for amounts and build array
// Asumming id is auto_increment and values are inserted in order
$sql = "SELECT
amount
, price_word
FROM price_change
ORDER BY id asc";
$result = mysqli_query($db, $sql);
// Build amounts array
$amounts = array();
while($row = mysqli_fetch_assoc($result)){
$amounts[] = $row;
}
// Set POST minprice to variable
if(isset($_POST['minprice']) && !empty($_POST['minprice'])):
$minprice = $_POST['minprice'];
endif;
// Set POST maxprice to variable
if(isset($_POST['maxprice']) && !empty($_POST['maxprice'])):
$maxprice = $_POST['maxprice'];
endif;
?>
<html>
<body>
<form action="" method="post">
<select name="minprice" id="minprice" onchange="this.form.submit()">
<option value="">Select</option>
<?php
foreach($amounts as $arr):
$selected_min = (isset($minprice) && $minprice === $arr['amount'] ? ' selected="selected"' : '');
?>
<option value="<?php echo $arr['amount'];?>"<?php echo $selected_min;?>><?php echo $arr['price_word'];?></option>
<?php
endforeach;
?>
</select>
<select name="maxprice" id="maxprice">
<option value="">Select</option>
<?php
foreach($amounts as $arr):
if(isset($minprice) && $arr['amount'] > $minprice):
$selected_max = (isset($maxprice) && $maxprice === $arr['amount'] ? ' selected="selected"' : '');
?>
<option value="<?php echo $arr['amount'];?>"<?php echo $selected_max;?>><?php echo $arr['price_word'];?></option>
<?php
endif;
endforeach;
?>
</select>
</form>
</body>
</html>