JQM selectmenu first option blank when echoing php-based select items - php

I'm populating a jQuery Mobile select menu in a form from mySQL database using this php:
$OTsql = "SELECT * FROM OrderTypes";
$OTresult = mysql_query($OTsql) or die (mysql_error());
while ($row = mysql_fetch_array($OTresult))
{
$OTid=$row["id_ot"];
$OTname=$row["orderTypes"];
$OToptions.="<OPTION VALUE=\"$OTid\">".$OTname;
}
the html is this:
<label>Order Type:</label><br>
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<option>
<? echo $OToptions ?>
</option>
</select>
While I can click on the selectmenu and see the options listed from the database, it shows a blank (no text listed on the selectmenu button itself) and when I click the menu the top spot is a blank placeholder.
I've tried a variety of jQuery options like variations of this which I've found here at SO:
$(document).bind('mobileinit',function(){
$.mobile.selectmenu.prototype.options.hidePlaceholderMenuItems = true;
});
The bigger problem (I could possibly live without there being the first option listed by default on the button or "Select Order Type" as the item listed on the button--which would be ideal), but when I go to save a pre-existing form, it changes whatever had been selected there to a blank in the database record.
Any insight would be greatly appreciated. I'm not even sure if I'm going about this the right way.

Can you try this,
$OToptions.="<option value=\"$OTid\">".$OTname."</option>";
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<?php while ($row = mysql_fetch_array($OTresult))
{
$OTid=$row["id_ot"];
$OTname=$row["orderTypes"];
echo "<option value=\"$OTid\">".$OTname."</option>";
}
?>
</select>

You are constructing your options wrong. Try this:
$OTsql = "SELECT * FROM OrderTypes";
$OTresult = mysql_query($OTsql) or die (mysql_error());
while ($row = mysql_fetch_array($OTresult))
{
$OTid=$row["id_ot"];
$OTname=$row["orderTypes"];
$OToptions.="<option value=\"$OTid\">".$OTname ."</option>";
}
HTML
<label>Order Type:</label><br>
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<?php echo $OToptions ?>
</select>
</label>
BTW, mysql_* functions are deprecated. Change to mysqli_* or _PDO functions

As I can see from your code, I expect the generated output to be:
...
<option>
<option>....</option>
...
<option>....</option>
</option>
...
which is wrong. Try to remove the outer tags by modifying your html markup:
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<? echo $OToptions ?>
</select>
Additionally, just for an improved style, I suggest you to reformat the PHP part in a more readable way, e.g.:
$OToptions .= '<option value="'.$OTid.'">'.$OTname;

Related

Html Select Box Get value from mysql

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>

What is my error when creating 4th AJAX populated dropdown using mySQL and PHP?

index.php
Hello community. I am using a dropdown list populated from mySQL using AJAX. I got this script as my base from a website that a member of this community suggested and you can see the demo here
In the demo there are 3 dropdowns and I am trying to make it 4, without luck as you can see in the screenshot here
I can not get the 2010 and pass it to the year so the 4th dropdown to show the selected rows, so it is shown as undefined.
One note though, if from the file findEngine.php remove the AND model_year='$yearId' and leave it with only 1 AND, it shows all the rows found for AUDI, not with the selected model or year of course.
I added the source codes below and the index.php to an external service because of it's length.
Any help is appreciate for this, I guess it is a beginners problem but I can not see it..
index.php
http://codepad.org/k3n7HJ4G
findModel.php
<?
$make=$_GET['make'];
mysql
$query="SELECT distinct model_name FROM cars WHERE model_make_id='$make' order by model_name asc";
$result=mysql_query($query);
?>
<select name="model" onchange="getyear('<?=$make?>',this.value)">
<option>Select Model</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['model_name']?>><?=$row['model_name']?></option>
<? } ?>
</select>
findYear.php
<? $makeId=$_GET['make'];
$modelId=$_GET['model'];
echo $modelId;
mysql
$query="SELECT distinct model_year FROM cars WHERE model_make_id='$makeId' AND model_name='$modelId' order by model_year asc";
$result=mysql_query($query);
?>
<select name="year" onchange="getEngine('<?=$makeId?>',this.value)">
<option>Select year</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$modelId?>><?=$row['model_year']?></option>
<? } ?>
</select>
findEngine.php
<? $makeId=$_GET['make'];
$modelId=$_GET['model'];
$yearId=$_GET['year'];
echo $yearId;
mysql
$query="SELECT distinct model_trim FROM cars WHERE model_make_id='$makeId' AND model_name='$modelId' AND model_year='$yearId' order by model_trim asc";
$result=mysql_query($query);
?>
<select name="engine">
<option>Select year</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['model_trim']?></option>
<? } ?>
</select>
As per the definition of getEngine function, it takes 3 parameters but you are passing only 2.
Change below line in findEngine.php
<select name="year" onchange="getEngine('<?=$makeId?>',this.value)">
To,
<select name="year" onchange="getEngine('<?=$makeId?>', '<?=$modelId?>', this.value)">
Also, avoid using mysql extension as it is deprecated in PHP 5.5.0. Read http://www.php.net/manual/en/intro.mysql.php for the details and alternatives.

Creating select list from database

I am making select list from database table everything works fine only issue is in option value if the value consist of two words like "New York" it only returns "New". Here is the code
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['CityName']?>><?=$row['CityName']?></option>
<? } ?>
</select>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
The " " .
You'll have to surrond the value in "", the HTML code thinks the York part is just another attribute of the option tag, so:
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
You haven't included the "" while giving the values. if you look at the html created for the select box by your code is something like
<option value="new" york>new york</option>
correct solution is given below, just included the quotation while giving value of option tag.
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
<? } ?>
</select>
Just use "" for the value of value same as you write HTML tag.

Get value from select with JavaScript for use in PHP

I have this select:
<select id="mymenu" name="id" onchange="getID()">
<option></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
I want to get the value from mymenu to use it in another select in the same form. I searched the 'net and found that I should use onchange.
<select name="uid">
<option></option>
<?
$sql = mysql_query("Select * from $table WHERE id = '$ch_id'");
while ($row = mysql_fetch_array($sql)) {
?>
<option value="<? echo $row['unit_id']; ?>"><? echo $row['unit_name'];?></option>
<?
}
?>
</select>
This is the getID() function:
<script type="text/javascript">
function getChapterID(){ //run some code when "onchange" event fires
var selectmenu=document.getElementById("mymenu")
<?$ch_id?> = this.options[this.selectedIndex].value
}
</script>
You can't redefine a PHP value thru JS, it would be very unsafe. I suggest you to use kind of form that when you change it, it submits, posts into a php self and redefines it. I won't write that code for you, but this is what you need.
Good luck
google "ajax php select menu", the first example here: http://www.w3schools.com/php/php_ajax_database.asp
explains the process...
Beware the w3 demo though, it does not sanitize $_GET[] variable, and is wide open to a sql injection.

Create a list from SQL database using HTML/PHP

Im trying to create a drop down list/menu of users from my SQL database table. Could anyone help me with the code please? The code below is just for a normal list with options 1 & 2. How do i make the list retrieve all the items in a specific table using html/php/sql. Sorry im not very experienced with this, thanks in advance.
<select name="user" id="user">
<option value="a" selected="selected">1</option>
<option value="b">2</option>
</select>
there are several ways but i'll show mine
First, take the data you want to retrieve from query:
$query = mysql_query("SELECT name, id FROM users");
Then echo a select followed by a while cycle to iterate the various options:
<?php
echo "<select name='user'>";
while ($temp = mysql_fetch_assoc($query) {
echo "<option value='".$temp['id']."'>".$temp['name']."</option>";
}
echo "</select>";
?>
The result should be:
<select name="user">
<option value='1'>User name 1</option>
<option value='2'>User name 2</option>
</select>
Hope this was useful for you :)
PHP Dynamic Drop-down

Categories