I am trying to pass the variable from my database to an HTML code but it isn't working. If I enter in a numeric value for my Select option it will work but it doesn't work with me trying to reference my class_id. The code I'm basing mine off of is found here. I am writing this code on wordpress also.
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo "
<form>
<select name='department' onchange ='showUser(this.value)'>";
foreach($result as $results){
echo "<option selected value='$results->class_id'>$results->course</option>";
}
echo" </select>
</form>";
Try not to mix your HTML and PHP together, I've fixed a few things for you below. You need to select class_id in your query to use it as the option value, you can't use selected on all options as well. You may want to check if there are actually any results before looping over them too.
<?php
global $wpdb;
$results = $wpdb->get_results("SELECT class_id, course FROM class;");
?>
<form>
<select name="department" onchange="showUser(this.value)">
<?php foreach( $results as $result ): ?>
<option value="<?= $results->class_id ?>"><?= $results->course ?></option>
<?php endforeach ?>
</select>
</form>
global $wpdb;
$result = $wpdb->get_results("SELECT course FROM class;");
echo '<form><select name="department" onchange="showUser(this.value)">';
foreach($result as $results){
echo '<option selected value="'.$results->class_id.'" >'.$results->course.'</option>';
}
echo '</select></form>';
it is easy and simple for understanding use this code it's working prefect
<?php global $wpdb; $result = $wpdb->get_results("SELECT course FROM class;"); ?>
<form>
<select name='department' onchange ='showUser(this.value)'>
<?php foreach($result as $results){ ?>
<option selected value='<?php echo $results->class_id; ?>'><?php echo $results->course; ?> </option>
<?php } ?>
</select>
</form>
Related
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
I am trying to use php code within html to grab content from a database to populate the values for a drop down menu (see below). When I run the php script on its own (test.php) I get all the expected values. When nested within html I get nothing but a single blank value under Select a Species. I would expect this to run through the while loop more than once as there are about 7 values returned and I would also expect the contents to include data derived from the table.
What I am attempting is possible correct??
Is it just an error with code (no errors popping up in the logs)
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
?>
<option><?php echo $row['species'];?></option>
<?php } ?>
</select>
<option><?php echo $row["species"]; ?></option>
<?php
$optionData = '<option id = "0">-- Select a Species -- </option>';
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
$optionData .= "<option>".$row['species']."</option>" ;
}
?>
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<?php echo $optionData;?>
</select>
//Put your fetched data on an array then loop them like this
<?php
foreach ($list as $data => $item) {
?>
<option value="<?php echo $data?>">
<?php echo $data?>
</option>
<?php
}
?>
This should work:
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species");
while ($row = $result->fetch_assoc()){
echo '<option>'. $row["species"].'</option>
} ?>
</select>
You were not iterating through the values within the HTML <option> tags, so I moved those into the while loop. (notice the PHP section includes echoing those tags).
I also removed the unnecessary "SQL injection-looking" where 1 clause from your query.
I am calling from my SQL server a list for the options and outputting into an array.. see query:
SQL
$location = "SELECT location_id, location_name, location_postcode, location_buildingname
FROM dbo.system_locations";
$stmt = sqlsrv_query( $conn, $location );
PHP
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option>
<?php echo $row['location_name']?>
</option>
<?php } ?>
</select>
This works great, however I want the option which is selected within the database to be the selected option..
There is one factor, the select list can be added to by visiting settings section and adding new location otherwise I would use the ternary operator method.
try this
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option <?php ($row['selected'] == 1){echo 'selected';}?>>
<?php echo $row['location_name']?>
</option>
<?php }?>
</select>
I have the below code. It's a function creating a from containing a dropdown menu and a selectionbox, and is being called from a different page).
<?php
require_once 'forbindtilDB.php';
function populerDropdownsOpretProgram()
{
global $mysqliOOP;
$stmt = $mysqliOOP->prepare("SELECT DISTINCT primaer FROM oevelser ORDER BY primaer ASC");
?>
<form action="temp.php" method="post">
<select multiple>
<option value="0">Vælg ønskede øvelse(r)</option>
<?php
$stmt->execute();
$stmt->bind_result($primaereMuskelgruppe);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $primaereMuskelgruppe;?>" >
<?php echo $primaereMuskelgruppe;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<?php
$stmt = $mysqliOOP->prepare("SELECT antalreps FROM antalreps ORDER BY antalreps ASC");
?>
<select>
<option value="0">Vælg ønskede antal gentagelser</option>
<?php
$stmt->execute();
$stmt->bind_result($antalreps);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $antalreps;?>" >
<?php echo $antalreps;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<input type="submit">
</form>
<?php
}
I want to post the user input on a different page (currently temp.php), but I don't know how to handle the fact that the form contents is variables fetched by a mysqli call.
So far I've tried different versions of the below on the temp.php-page
<?php
echo $_POST[$antalreps];
?>
<?php
echo $_POST[$primaereMuskelgruppe];
?>
But I'm getting errors stating that there is undefined variables (antalreps and primaeremuskelgruppe) and undefined indexes...
Also, there's the added complexity that the selection box may return more than one result.
Pretty sure echo $_POST[$antalreps]; etc. is the wrong way to go about this, but I haven't been able to figure out alternatives...
Any hints?
You should add 'name' to your select and then use it.
For example instead of:
<select>
you should have:
<select name="yourname">
and then in php to display its value use:
echo $_POST['yourname'];
and for select multiple you can use:
<select name="othername[]">
and then in PHP use it as array:
foreach ($_POST['othername'] as $item) {
echo $item;
}
of course where I put yourname and othername you should put descriptive name such as colours for color box and similar
I've been trying to find the best way to do this for a while now but cannot figure it out.
I have a simple dropdown which auto populates based on an SQL query. When you press "search" I need it to go to a page with the url extension ?id=x where x is the id of the option they selected.
$locations = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM wp_places_index WHERE visible='Y' ORDER BY place_name ASC") );
?>
<form id="find-buses-form" method="post" action="places_index.php">
<select class="default-value" type="text" name="from">
<option>Please select...</option>
<?php
foreach($locations as $location)
{
echo "<option>".$location->place_name."</option>";
// maybe this? echo "<input type=\"hidden\" name=\"id\">".$location->id."</input>";
}
?>
</select>
<input type="submit" name="submit" value="Show me" />
</form>
I think I may need to make it go to an external page which uses $_POST to pull that hidden field but I'd rather do it on one page.
I've achieved this before out of wordpress using something like this:
while ($row = mysql_fetch_array($result))
{
$picture = $row['Image'];
if($picture == ""){
$picture= "thumbnails/no-image-small.png";
}
$product_ID = $row["ProductID"];
But wordpress does not like the mysql_fetch_array :( Any advise?
You need to put whatever in a value attribute of your option tag. For example:
<?php
$locations = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM wp_places_index WHERE visible='Y' ORDER BY place_name ASC") );
?>
<form action="places_index.php" method="get">
<select name="x">
<?php foreach ($locations as $location): ?>
<option value="<?php echo $location->id; ?>"><?php echo $location->place_name; ?></option>
<?php endforeach; ?>
</select>
</form>
When submitted, the form will go to http://example.com/places_index.php?x=1 presuming the name of your select is x and the option selected has 1 in its value attribute.
Hope this helps.