Dynamic drop down menu - php

I'm trying to do a dynamic drop down menu. First I read from a sql table some dates and them I want to print the dates in a drop down box and select one. I can print them, but I can't seem to select one. This is my code on the relevant part:
?>
<form action="dataProc.php" method="POST">
<select id="dates" name="dates">
<option value="0">--Selecionar Dia--</option>
<?php
for($i = 0 ; $i < count($resultados) ; $i++)
{
?>
<option value="<?php $i ?>"><?php echo $resultados[$i]['anoDia'] ;?></option>
<?php
}
?>
</select>
<input type="submit" value="Escolher">
</form>
<?php
if(isset($_POST['dates']))
{
if(empty($_POST['dates']))
{
echo 'ya';
}
else
{
echo $_POST['dates'].'<br>';
}
}
EDIT: manage to solve my problem. Just changed <?php $i ?> to <?php echo $resultados[$i]['anoDia'] ;?>

Solved by using the specified EDIT

Related

PHP Assign Value from dropdown selection

I have this loop coded to create a dropdown with the numbers 0-30.
<td style="text-align:left;">
<select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i=0; $i<=30; $i++) {
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
I cannot seem to figure out how to get the value of I to work in my calculations on an order processing form.
I assign the variable using
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
but if I try to output $iPhone12 * 15 for example the answer is always zero.
Make sure you select is wrapped in a form element with method="POST". You should also use isset to make sure it exists.
<form action="" method="post">
<td style="text-align:left;"><select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i = 0; $i <= 30; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select></td>
<button>submit</button>
</form>
<?php
if (isset($_POST['iPhone12Case'])) {
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
printf($iPhone12 * 15);
}

Set default value for select dropdown in Php

I'm a absolute newby for php and i'm struggling with just a basic thing.
I want to set a default value for a select box i have. Default value should be 2
My code
<div class="selector-wrapper">
<select id="adults" class="form-control" onchange="toggleSetGuests()">
<?php
for($i = 0; $i <= 20; $i++):
?>
<option value="<?php echo $i ?>" <?php if($i == 0){ echo('selected="true"'); } ?>>
<?php echo $i ?> Adults
</option>
<?php
endfor;
?>
</select>
</div>
So in this select box i get options from 0 - 20. I want to set the default value as 2 instead of 0
My approach was like below
<option value="<?php echo $i ?>" <?php if($i == 2){ echo('selected="true"'); } ?>>
But did not work.
How do i set the default value to be selected as the value 2
<option value="<?php echo $i ?>" <?php if($i == 2){ echo ' selected'; } ?>>
should work.You don't need selected=true

PHP Form select correctly populates MySQL values, does not with simple PHP echo

I have a PHP select that dynamically populates based on a MySQL recordset and uses a in_array value to identify and select the value(s) found in the database like this:
<select name="StaffSetSelection[]" size="5" multiple="MULTIPLE" id="StaffSetSelection">
<option <?php if ($totalRows_StaffSetID == 0) { echo "selected"; } ?> value="">Choose a Staff Set</option>
<?php
do {
?>
<option <?php if (in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo "selected";
} ?> value="<?php echo $row_StaffSetChoices['EmpNumber'] ?>"><?php echo $row_StaffSetChoices['EmpFirstName'] ?></option><?php
} while ($row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices));
$rows = mysql_num_rows($StaffSetChoices);
if ($rows > 0) {
mysql_data_seek($StaffSetChoices, 0);
$row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices);
}
?>
</select>
Instead of displaying the selected values in a form option select I'd like to just display them as text.
I've tried to use the in_array again in a simple php echo but it does not display the data and there are no errors in my apache log.
This is what I've tried:
<?php if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) echo $row_StaffSetChoices['EmpFirstName']?>
I think you forgot the brackets,
Try this way:
<?php
if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo $row_StaffSetChoices['EmpFirstName'];
}
?>

Select box values in PHP MYSQL

I have a select box like this. This is nothing but looping from 1 to 10.
<select name="author_box[]" id="author_box[]">
<?php for($j=1;$j<=10;$j++){ ?>
<option id="<?php echo $j; ?>" name="<?php echo $j; ?>" value="<?php echo $$j; ?>"><?php echo $j; ?></option>
<?php } ?>
</select>
For example: I have a variable $tmp_rating="5". So what I want to display is showing the value 5 in the select box along with the other values (i.e. from 1 to 10).
How can I achieve that. In short, the value which I fetch from the database should be displayed first in the select box.
Thank you gentleman, for all the time and instant reply. This portal helps me a lot in fixing my issues in less time.
You have to use the selected attribute for the option:
<select name="author_box[]" id="author_box[]">
<?php for($j=1;$j<=10;$j++){ ?>
<option value="<?php echo $j; ?>" <?=($tmp_rating==$j)?'selected="selected"':null;?>><?php echo $j; ?></option>
<?php } ?>
</select>
This way, if $tmp_rating is the same as $j in your loop, the attribute selected is added and the correct value displays in your selectbox.
By adding this condition
<?php if($j == $tmp_rating) echo 'selected="selected"'; ?>
Full code
<option <?php if($j == $tmp_rating) echo 'selected="selected"'; ?> id="<?php echo $j; ?>" name="<?php echo $j; ?>" value="<?php echo $$j; ?>"><?php echo $j; ?></option>

All zero values passed in the table

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']}')";

Categories