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);
}
Related
Iam trying a solution in PHP/MYSQL where in there is a some country list with a checkbox. Certain countries have something called entities which is a drop down which shows Micro, Small, Large as a dropdown. This entity dropdown appears for certain countries only. I am able to pass the checkbox checked values to next page. But i need to pass the value of the entity selected in the drop down also. Iam not able to acheieve that. My code ie posted below. Here i need to pass the value of the drop down (select - entity_selected) also to next page:
<?php
echo '<table border="1">';
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1["country"];
$entity1 = $row1["entity"];
echo '<tr>';
?>
<td><input name="check_list[]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?></td>
<td>
<?php
if($entity1 == 'Yes'){ ?>
<select class="form-control selectpicker" name="entity_selected">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php }
?>
</td>
<?php echo '</tr>'; } ?>
</table>
The Next page code is below to get the check box selected countries.
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
How can i get the drop down values here? iam getting the checkbox (countries values correctly). Can anyone pls help me on this pls?
<form method="POST" action="nextpage.php">
<table border="1">
<?php
$i = 0;
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1['country'];
$entity1 = $row1['entity'];
echo "<tr>";
?>
<td>
<input name="check_list<?=$i ?>" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
</td>
<td>
<?php
if($entity1=="Yes") {
?>
<select class="form-control selectpicker" name="entity_selected<?=$i ?>">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php
};
?>
</td>
<?php
$i++;
echo "</tr>";
};
?>
</table>
</form>
nextpage.php:
<?php
$entities = $checklist = [];
foreach($_POST as $k => $v) {
if(preg_match("/^checklist(\d+)$/", $k, $matches))
$checklist[intval($matches[0])] = $v;
unset($matches);
if(preg_match("/^entity_selected(\d+)$/", $k, $matches))
$entities[intval($matches[0])] = $v;
};
?>
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
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
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>
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']}')";