All zero values passed in the table - php

When I execute this code, I only get zeroes inserted into my database. Why? What can I do to solve this problem?
<select class="date" name="year"> <option value="">Year</option>
<?php $i=1; while($i<=31) { ?>
<option value="<?php $i ?>"> <? echo $i; ?> </option>
<?php $i++; } ?> </select>
And this is my mysql code:
$sql="INSERT INTO information ( Year )
VALUES ('$_POST[year]')";

here how it will be your code
<select class="date" name="year">
<option value="">Year</option>
<?php for($i=1;$i<=31 ;$i++ ) { ?>
<option value="<?php $i ?>"> <? echo $i; ?> </option>
<?php } ?>
</select>
EDIT :
<input type ="submit" value =" submit me"/>
<?php if (isset($_POST['year'])) // check if isset the year
{
$year = $_POST['year'] ;
} else {}
$sql="INSERT INTO information ( Year ) VALUES ('".$year."')";
?>
edit 2 :
your code is right but you should escape value to prevent sql injection
do like that
insert into ....
VALUES
('mysql_real_escape_string($_POST[user])','mysql_real_escape_string($_POST[email])'.........

<select class="date" name="day"> <option value="">Day</option>
<?php $i=1; while($i<=31) { ?>
<option value="<?php echo $i; ?>"> <?php echo $i; ?> </option>
<?php $i++; } ?> </select>
Always use <?php and ?> so your browser recognizes PHP
also, with your first <?php $i ?>
I changed this to echo $i; and on the same line I changed <? echo $i; ?> </option> To <?php echo $i; ?> </option>
and it worked for me.
With Your Insert:
$sql="INSERT INTO information (COL_FOR_DAY)
VALUES ('{$_POST['day']}')";

Related

Option value doesn't match the right value

I have a $seasons variable that contains 21 seasons. For each season I want to make a option in html that you can select. If I press the submit button I want the option that is selected before submitting, is still selected. I try to do that with this code:
<select name="season" id="season" class="filter-season">
<option value="all">-- Alle seizoenen --</option>
<?php foreach($seasons as $season): ?>
<option <?php if (isset($_GET['season']) == $season['season']){?> selected = "true" <?php }; echo "selected" ?>\value="<?php echo $season['season'] ?>"><?php echo $season['season']; ?></option>
<?php endforeach; ?>
</select>
The problem is that the value of the option always jumps back to 21.
You need
selected="selected"
instead of
selected=true
<select name="season" id="season" class="filter-season">
<option value="all">-- Alle seizoenen --</option>
<?php foreach($seasons as $season): ?>
<?php
$isSelected = (isset($_GET['season']) && $_GET['season'] == $season['season']) ? 'selected="selected"' : '';
?>
<option <?php echo $isSelected;?> value="<?php echo $season['season'] ?>"><?php echo $season['season'];?></option>
<?php endforeach; ?>
</select>
It depends on your array type, but for a normal array:
$seasons = [
'winter',
'summer'
];
$selected_season = isset($_GET['season']) ? $_GET['season'] : false;
<select name="season" id="season" class="filter-season">
<option value="all">Alle seizoenen</option>
<?php foreach($seasons as $season): ?>
<option value="<?= $season; ?>" <?php $season == $selected_season ? 'selected="selected"' : ''?>><?= $season; ?></option>
<?php endforeach; ?>
</select>
You made a mistake.
I've rewritten your code:
<select name="season" id="season" class="filter-season">
<option value="all">-- Alle seizoenen --</option>
<?php foreach($seasons as $season): ?>
<option <?php if (isset($_GET['season']) && $_GET['season'] == $season['season']) echo "selected" ?> value="<?php echo $season['season'] ?>"><?php echo $season['season']; ?></option>
<?php endforeach; ?>
</select>
Your "echo selected" call was outside of your if statement. In your case you selected all the options and your browser then shows the last selected, in your case option 21. Also your if statement itself was wrong.
I've rewritten you're code. It now checks if $_GET['season'] is set and if $_GET['season'] equals $season['season']

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

Option value Not displaying in drop-down menu?

I'm using a date range on a web page to aggregate MySQL data and present it based on the selected time frame. For some reason the values for each option in the drop-down menus are not displaying. Here's the PHP I'm using:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?><?=$date ?></option>
<?php } ?>
</select>
And here's the HTML output:
<option value="2015-01-03" selected="selected" 2015-01-03<="" option=""></option>
The weirdest part is this was working for the longest time and suddenly the dates in both menus vanished. Any ideas why?
This happened becuase you are missing the ending > of tag
Modified code:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?=$date ?>
</option>
<?php } ?>
</select>
Your PHP snippet is missing a closing >:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option
value="<?=$date ?>"
<?php if($date == $date1) { ?> selected="selected"<?php } ?>
>
<?=$date ?>
</option>
<?php } ?>
</select>
Try this way also :
<select name="per1" id="per1">
<option selected="selected">Choose one</option>
<?php
foreach($names as $name) { ?>
<option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
<?php
} ?>
</select>
Now you can put your code here.
1. Add > tag closer after selected attribute
2. Remove shorthanded <?= ?> tag which is unwanted while you also using <?php ?>
<select name="date1" title="<?php print $date1; ?>">
<?php foreach($availableDates as $date) {
?><option value="<?php print $date; ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?php print $date; ?>
</option>
<?php } ?>
</select>
I suggest this less error prone code.
$selHTML = '<select name="date1" title="'.$date1.'">';
foreach($availableDates as $date) {
$sel = ($date == $date1)?" selected":"";
$selHTML .= '<option value="'.$date.'"'.$sel.'>'.$date.'</option>';
}
$selHTML .= '</select>';
echo $selHTML;

How do I Populate Multi Select drop dwon using php

Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>

Create a PHP Dropdown menu from a for loop?

I am trying to create a drop down menu with the options of 1,2,3 and 4.
The below code is what I am using just now and the dropdown is empty.
Any idea what I am doing wrong?
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
"<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
You are not outputting the option tags.
Try it like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
You basically use html without closing the php syntax.Your code should look like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
Or are you trying to echo the option? In that case you forgot the echo statement:
echo "<option value= ".$i.">".$i."</option>";
This worked for me. It populates years as integers from the current year down to 1901:
<select Name='ddlSelectYear'>
<option value="">--- Select ---</option>
<?php
for ($x=date("Y"); $x>1900; $x--)
{
echo'<option value="'.$x.'">'.$x.'</option>';
}
?>
</select>
You forgot something..
Add print / echo before "<option value=".$i.">".$i."</option>";
place an echo in your loop to output your options.
echo "<option value=".$i.">".$i."</option>";
Just echo the <option> tag
echo "<option value=".$i.">".$i."</option>";
<select name="year">
<?php for ($i = 0; $i <= 9; $i++) : ?>
<option value='<?= $i; ?>' <?= $fetchData->year == $i ? 'selected' : '' ?>><?= $i; ?></option>
<?php endfor; ?>
</select>

Categories