for in combination with html forms - php

I want to provide different options in a html form, with a php for loop, but the loop gets all the time stuck. Thanks in advance for helping me out.
<select name="position">
<?php
for($i=1;$i<10;$i++) {
echo "<option value= . $i >$i</option>";
}
?>

if you are use double quotes can without . Operator
<select name="position">
<?php
for($i=1;$i<10;$i++) {
echo "<option value= ' $i '>$i</option>";
}
?>
</select>

You look like you are missing the quotes in the value tag.
echo "<option value= \"$i\" >$i</option>";

Related

HTML Options from SQL Database

I have a HTML DataList input box, and I would like to have the options in the DataList to be things from my SQL database. I have been trying to make this work, but for some reason I cant. Does anyone know how this could be achieved?
Here is my code:
<?php
include_once 'connect.php';
$sql="select * FROM table";
?>
<form>
<input list="list" name="name">
<datalist id="datalist">
<?php
foreach ($dbo->query($sql) as $row) {
echo "<option value="$row[name]"/>";
}
?>
</datalist>
<input type="submit">
</form>
The option I get with this is literally $row[name]
Could someone tell me what I'm doing wrong?
Datalist id has to match the input list attribute and change the loop to "<option value=" . $row['name'] . "/>"

Populating drop-down list with PHP not working

I am trying to get a drop-down list populated with PHP to my website but it is not displaying anything. It's empty as shown in this image. Under the state it shows nothing.
Please help me to get some details from my database results.
My code is:
<?php
$con=mysql_connect('localhost', '', '')or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db('')or die("Failed to connect to MySQL: " . mysql_error());
?>
<form>
<div class="form-group">
<div class="form-control-small">
<select id="search_status" name="state" data-placeholder="State">
<option value=""> </option>
<?php
$dd_res=mysql_query("Select DISTINCT state from MyTable");
while($row=mysql_fetch_row($dd_res))
{
echo "<option value='$row[state]'> $row[state] </option>";
}
?>
</select>
</div>
<div class="form-control-small">
<select id="search_bedrooms" name="dist" data-placeholder="District">
<option value=""> </option>
<?php
$dd_res=mysql_query("Select DISTINCT dist from MyTable");
while($r=mysql_fetch_row($dd_res))
{
echo "<option value='$row[dist]'> $row[dist] </option>";
}
?>
</select>
</div>
<div>
<button type="submit" class="btn btn-fullcolor">Search</button>
</div>
</div>
</form>
Try some thing like this:
$arr = array(1 => 'MP', 2 => 'UP');
echo '<select>';
foreach($arr as $key => $val)
{
echo '<option value="'.$key.'">'.$val.'</option>';
}
echo '</select>';
It will generate html like:
<select>
<option value="1">MP</option>
<option value="2">UP</option>
</select>
You cant display Array values in a string, even with double quotes.
Either you have to use curly brackets or concatenation
echo "<option value='{$row[state]}'> {$row[state]} </option>";
or
echo "<option value='".$row[state]."'> ".$row[state]." </option>";
Try this:
echo "<option value='".$row['state']."'> ".$row['state']."</option>";
According to the documentation, the mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
This means that you cannot reach your column names like this: $row["state"].
Try instead:
echo "<option value='$row[0]'> $row[0] </option>";
However the mysql_query and mysql_fetch_row function are deprecated, and they will be removed in PHP version 7. It can be an issue for you too. Consider upgrading to something more modern way of interacting with MySQL.
You will find alternatives here:
http://php.net/manual/en/mysqlinfo.api.choosing.php

HTML PHP: Why do two identical select elements produce different HTML displays?

Could someone kindly help out with this rather basic issue: In a simple HTML form (functionally all good) two select elements are included.
Code:
<select id="dept" name="dept" required>
<?php
foreach($stddept as $item0) {
echo "<option value='$item0'";
if ($_POST['dept'] == $item0) echo 'selected="selected"';
echo ">$item0</option>";
}
?>
</select>
<br><br>
<select id="lev" name="lev" required>
<?php
foreach($stdlev as $item1) {
echo "<option value='$item1'";
if ($_POST['lev'] == $item1) echo selected="selected"';
echo ">$item1</option>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>`
These two drop-down boxes behave differently in that only the second one honours the 'required' attribute. The first one can be submitted blank, which is obviously not right.
They also appear differently (see image) in that the first displays the first option in the list (from MySQL DB) but the second displays a blank. I have checked everything I could to see what else could cause the difference - without any joy.
Does anyone know what I am missing here? Thank you.
You can do this using jquery/javascript by getting the id of the first selected value and then select the result for the second box based on that id.
I think the problem with the code ( as it is shown ) is that you are incorrectly echoing the option in the second dropdown - it is missing quotes. You can streamline the code somewhat like this:
<select id="dept" name="dept" required='required'>
<?php
foreach($stddept as $item0){
$selected=$_POST['dept'] == $item0 ? " selected='selected'" : "";
echo "<option value='$item0'{$selected}>";
}
?>
</select>
<br /><br />
<select id="lev" name="lev" required='required'>
<?php
foreach($stdlev as $item1){
$selected=$_POST['lev'] == $item1 ? " selected='selected'" : "";
echo "<option value='$item1'{$selected}>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>

Set HTML dropdown options from PHP variable

I want to set the on a HTML dropdown menu from a php variable. I give you my code so you can see I want to do:
<?php
$html_table = '
<table border="0" cellspacing="0" cellpadding="0"><tr>';
while($arr = pg_fetch_array($result1))
{
$html_table .= "<tr><td> $arr[0] </td></tr>";
}
$html_table .='</tr>';
?>
<p>
<select name="db" size="1">
<option> $html_table </option> #### <- that is my question, how to get that working
</select>
</p>
I hope you understand what I want to do. If you know about nicer ways, let me know.
Cheers
<?php
$options = '';
while($arr = pg_fetch_array($result1)) {
$options .= '<option>'.$arr[0].'</option>';
}
?>
<p>
<select name="db" size="1">
<?php echo $options; ?>
</select>
</p>
Just replace $html_table with <?php echo $html_table; ?> and you're good to go - from the PHP point of view that is. HTML is of course invalid, as pointed out by others.
First: you didn't close your <table> tag.
Second: you can't paste<table> tag into <select>. You should put a series of <option> elements, each with one element of the array.
Third: To print PHP variable you should do like this:
<option><?php echo $html_table; ?></option>
But again: it will break your HTML, because table can't be inside select option.
The best solution is:
<select name="db" size="1">
<?php while ($arr = pg_fetch_array($result1)): ?>
<option><?php echo $arr[0]; ?></option>
<?php endwhile; ?>
</select>
so set your option elements like this
<option value="<?php echo $html_table; ?>"><?php echo $html_table; ?></option>
just know, an option can't be a table.

Codeigniter, problems using set_select() and set_checked()

I have a bit of an issue using codeigniter's set_select and set_checked within my forms, I am adding these to my existing forms as I am the stage in development where I am trying to tidy things up and failed validation resetting forms was not a big issue when I was still working on the project but now its coming to a close its become a major headache.
Firstly the set_select, I have this code which outputs me drop down from an array which is passed to the view from the controller which gets the results from a table in my database, the form I am implementing this in has 10 drop down boxes each corresponds to a table in my database. Anyway this is the code:
<label for="rating">Rating: </label>
<select name="rating">
<?php
if(isset($rating) && $rating != 'none') {
echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
foreach($rating as $row) {
echo '<option value="'.$row->door_rating_rating.set_select('rating', $row->door_rating_rating).'">'.$row->door_rating_rating.'</option>';
}
} else {
echo '<option value="none">Nothing to list</option>';
}
?>
</select>
It is just not working and as far as I can see there shouldn't be a problem with my code but this is the first time I have used this and I have looked at examples of using it but could not find an example of using it in a for loop so is what I am doing even possible?
This is my set_checked code within the view and this too is not working after failed validation:
Temporary Refuge Door?<input type="checkbox" class="temp_ref" name="tempref" value="1" <?php echo set_checkbox('tempref', '1'); ?> />
Any help with either of these would be really appreciated.
Looks like you had the set_select within the quotes for the option value. I moved it after it. I have also made an edit to use printf for better readability.
<label for="rating">Rating: </label>
<select name="rating">
<?php
if(isset($rating) && $rating != 'none') {
echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
foreach($rating as $row) {
printf('<option value="%s" %s>%s</option>', $row->door_rating_rating, set_select('rating', $row->door_rating_rating), $row->door_rating_rating);
}
} else {
echo '<option value="none">Nothing to list</option>';
}
?>
</select>
To solve set_select() try the following. Assuming you have an array named $isps which contains id and name field.
<select id="isp" name="isp">
<option value="" selected>Select a ISP</option>
<?php foreach ($isps as $row) { ?>
<option value="<?php echo $row->id ; ?>" <?php echo set_select('isp', $row->id, False); ?> ><?php echo $row->name ; ?> </option>
<?php } ?>
</select>

Categories