Im trying to update a drop down list with the countries of the world from a db query, which is working fine. I want to set the country of the user as selected, based on their IP. I am collecting their IP fine and working out their country, but Im struggling to set the selected in the output based on this.
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT *
FROM tbl_fm_countries
order by Country Asc");
$ipcountry = "United Kingdom";
?>
<form action="results.php" method="post" id="foodmilesTracker">
<fieldset>
<legend>Where are you?</legend>
<ol>
<li>
<label for="countryTo">Your Location</label>
<select name="countryTo">
<?php
// output data of each row
while($row = $result->fetch_assoc()){
echo "<option value='" . $row['CountryId'] . "'>" . $row['Country'] . "</option>";
}
?>
</select>
</li>
</ol>
</fieldset>
I have tried lots of options and seem to get all set as selected or none. For the example I am setting ipcountry manually.
I recommend doing it following way
<?php while($row = $result->fetch_assoc()){ ?>
<option value="<?php echo $row['CountryId']; ?>" <?php echo $row['Country'] == $ipcountry ? 'selected':''; ?>><?php echo $row['Country']; ?></option>
<?php } ?>
Related
I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.
How to retain selected value after submit/refresh with this code?
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
Solved this way:
Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
Because I use explode on $_POST['test_email']
I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.
Perhaps this could be simpler but it works like a charm.
#roberto06: Thank you for your guidance.
You need to add the selected="selected" attribute on the option matching your $_POST value.
You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.
Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
</form>
I've started with a standard form select option that looks like this:
<option value="">All Locations</option>
I was able to re-use a URL parameter using _get to create an option from a value like this: &location=Bellingham
<?php
$location = $_GET['location'];
?>
<option value='<?php echo $location; ?>'><?php echo $location; ?></option>
How do I using PHP test for location value being populated and if so using my _GET option and if not falling back to my standard form select?
are you manually sending the location in the get request?
try putting all the location in the a table and then use mysql
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT location_name FROM location";
$result = mysql_query($sql);
echo "<select name='location'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location_name'] . "'>" . $row['location_name'] . "</option>";
}
echo "</select>";
?>
I am not 100% sure what you mean, but these seems a bit more logical compared to my original answer...
<form>
<select>
<option value="">All Locations</option>
<?php if(isset($_GET['location']) && !empty($_GET['location'])):?>
<option selected value='<?php echo $_GET['location']; ?>'><?php echo $_GET['location']; ?></option>
<?php endif?>
</select>
</form>
Is this what you meant?
Feel free to ask questions should something be unclear.
I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.
i am trying to sort my products using a drop down box. when i select an option the code does not run and the products do not change position. iv managed to sort products by using different buttons but i think a drop down list would look better on a website.
<?php
echo $sort = #$_GET['order'];
if (!empty($sort)) {
echo $query="SELECT * FROM products ORDER BY '".$sort."'";
} else {
echo $query="SELECT * FROM products order by " ;
}
?>
<form name="sort" action="" method="post">
<select name="order">
<option value="choose">Make A Selection</option>
<option value="price_asc">Price </option>
<option value="price_desc">Z-A</option>
<option value="name_asc">A-Z</option>
</select>
<input type="submit" value=" - Sort - " />
</form>
<?php
//Run the query.
$record_set = $connection->query($query);
while( $row = $record_set->fetch_assoc() ) {
echo '<div class="product">';
echo '<div class="product-content"><h3>'. $row['name'].'</h3>' .'</div>' . '<br />'.'<div class="product-thumb"><img src="/ISD assignment2/images/'. $row['imageName'].'"></div>'. '<div class="product-desc">'.$row['description']. '</div>'. '<br />' .'£'. number_format($row['price'], 2) . '<p>Add</p>';
echo '</div>';
}
?>
Two potential issues:
1) Your Form has no action. I believe you are trying to get it to refresh onto itself.
which would be:
action=" <?php echo $_SERVER['PHP_SELF'] ?>"
2) You are submitting via POST, yet using GET.
$sort = $_POST['order'];
I'm trying to add results into a html dropdown.
The php works if I take it outside the html form: it shows the results, but I need it inside the form
<form><form method="post" action="selldo.php">
<label><br /><br /><br /><br />What slot do you want to Sell?</label>
<select name="pokeSLOT" id="pokeSLOT" style="width:150px;padding-left:5px;">
<option value=""></option>
<?php
$result = mysql_query("SELECT * FROM user_pokemon
WHERE belongsto='$_SESSION[username]'");
while($row = mysql_fetch_array($result))
{
echo $row['id'] . " " . $row['pokemon'];
echo "<br />";
}
?>
</select><br/><br/>
<label>Price You Would Like For The Pokemon?</label>
<input type="int" name="cost" id="cost" maxlength="30"/><br/><br/>
<button name="submit" type="submit" id="Submit_But">Sell</button>
<p> </p><p> </p>
</form>
When I look in the dropdown menu there is nothing but if it makes the SQL out of the form it posts the results to the page so it works fine I just need it in side the drop down html form
p.s i have the connect ontop of the page
You will need to echo out HTML option elements:
while($row = mysql_fetch_array($result)) {
echo "<option>" . $row['id'] . " " . $row['pokemon'] . "</option>";
}
You will probably want to give the option elements a value so the selected option is passed along properly when the form is submitted.
Did you look at the source this code generates?
You will find that your options are all there but just somewhere in the void, not wrapped by any html tags. You'll see something like:
<form>
<select>
<option></option>
your first option
your second option
your third option
your n'th option
</select>
</form>
But what you really need, for the markup to be correct, is this:
<option>your first option</option>
<option>your second options</option>
And so forth... that should be enough for you to get it right! If not...
echo '<option value="' . $row['id'] . '">' . $row['pokemon'] . '</option>';
You have an SQL-injection hole and a possible XSS security hole:
Correct this by changing the php code to:
<?php
$username = mysql_real_escape_string($_SESSION['username']);
$result = mysql_query("SELECT * FROM user_pokemon
WHERE belongsto = '$username' ");
while($row = mysql_fetch_array($result))
{
$id = htmlentities($row['id']);
$pokemon = htmlentities($row['pokemon']);
echo '<option value = "$id"> $pokemon </option>';
}
?>
See: What are the best practices for avoiding xss attacks in a PHP site
And How does the SQL injection from the "Bobby Tables" XKCD comic work?
You're not creating a select! you need the <option></option> tags for that, not just echo out your results...
<select name="pokeSLOT" id="pokeSLOT" style="width:150px;padding-left:5px;">
<option value=""></option>
<?php
$username = mysql_real_escape_string($_SESSION['username']);
$result = mysql_query("SELECT * FROM user_pokemon WHERE belongsto='$username'");
while($row = mysql_fetch_array($result)) : ?>
<option value="<?php echo htmlentities($row['id']);?>"><?php echo htmlentities($row['pokemon']);?></option>
<?php endwhile;?>
</select>
This should do the trick:
<select name="pokeSLOT" id="pokeSLOT" style="width:150px;padding-left:5px;">
<?php
$result = mysql_query("SELECT * FROM user_pokemon WHERE belongsto = '$_SESSION[username]'");
while($row = mysql_fetch_array($result)) {
echo "<option value=\"\">" . $row['id'] . " " . $row['pokemon'] . "</option>
?>
</select>