PHP: select form menu list error - php

Hi my select menu list is not working.
<?php
<tr>
<select name="package">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
if(isset($_POST['package'])) echo "<option>" . $_POST['package'] . "</option>";
</tr>
?>
Help, would be appreciated.
Many thanks!

you have mixed php with html and also you didnt close SELECT tag.
try that:
<tr>
<select name="package">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
<?php
if(isset($_POST['package'])){ echo "<option value='".$_POST['package'] ."'>" . $_POST['package'] . "</option>"};
?>
</select> <-----you missed this
</tr>
when using php and html you need either :
<?php echo "<select name='package'>
<option value='volvo'>Volvo</option>
....................................";
?>
or the code i provided above without before and after html code.

You can't use html inside php directory. If you want to use html mixed with php either do it using echo or outside <?php //inside ?> //outside. Here try this.
<tr>
<select name="package">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
<?php
if(isset($_POST['package']))
echo "<option>" . $_POST['package'] . "</option>";
?>
</tr>

Related

How to use if condition in echo

I have a form to add product to cart and inside it there is a link.
This is link
<?php
echo $this->Html->link('<div class="single-products">'.'<div class="productinfo text-center myimg">'.$this->Html->image("product/".$row["Product"]["photo"],array(/*"width"=>"2500px",*/"height"=>"250px")).'<h2> &#8377 '.$row["Product"]["price"].'</h2>'.'<p>'.$row["Product"]["name"]."</p><a href='javascript:document.ff".($i++).".submit()' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>".'</div>'.'</div>',
array
(
'controller'=>'Public',
'action'=>'singleproduct?id='.$row["Product"]["id"],
),
array
(
'escape'=>false //NOTICE THIS
)
);
?>
And I want to use this code just above Add to cart button
<?php
if($row["Product"]["psize"]==1)
{
?>
Size<select name="psize">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<?php
}
elseif($row["Product"]["psize"]==2)
{
?>
Size<select name="psize">
<option value="28">28</option>
<option value="30">30</option>
<option value="32">32</option>
<option value="34">34</option>
</select>
<?php
}
?>
This code is working if I put it outside of this HTML helper link, but because of design problem and I want to display it just above the add to cart button
I have tried but couldn't figure out how to put this inside link.
<?php
if($row["Product"]["psize"]==1)
{
?>
Size<select name="psize">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<?php
} else{
if($row["Product"]["psize"]==2){
?>
Size<select name="psize">
<option value="28">28</option>
<option value="30">30</option>
<option value="32">32</option>
<option value="34">34</option>
</select>
<?php
} else{
echo "TEY IT"
}
}
?>
May Be it gone help.
Bring out your codes in the function $this->Html->link(), assign it to a variable and use if condition. And i think you should use CakePHP Form Helper to output select box.
Example:
<?php
$select = $this->Form->input('psize', array(type => 'select', 'options' => $sizeOptions)); // you can use if conditions here
$link = $this->Html->link('<div>...</div>' . $select . '<div>...</div>', $yourUrlArr);
echo $link;

How to set Dropdown List default value based on user selection

I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>

Error setting the selected value from a select list

I have a HTML select list where one can chose an option.
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' selected>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
When this list is submitted via a HTML submit button, this list shows again in another page. Now, I want the option the user selected to be new selected value. I am doing this:
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
But values are appearing twice. The option the user selected is shown as the selected and still appears in the list. For example, we now have something like this:
0-50,000 (this is the selected value)
0-50,000
50,000-100,000
100,000-150,000
150,000-200,000
200,000 and above
How do I solve this?
In other page, make sure the selected option has selected="selected"
<select name='price' id='price'>\
<option value='' >Select....</option>
<option selected="selected" value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name='price' id='price'>
<option value='0-50,000' <?php echo selectBoxSelection('0-50,000', $_POST['price']);?> >0-50,000</option>
<option value='50,000-100,000' <?php echo selectBoxSelection('50,000-100,000', $_POST['price']);?> >50,000-100,000</option>
<option value='100,000-150,000' <?php echo selectBoxSelection('100,000-150,000', $_POST['price']);?> >100,000-150,000</option>
<option value='150,000-200,000' <?php echo selectBoxSelection('150,000-200,000', $_POST['price']);?> >150,000-200,000</option>
<option value='200,000 and above' <?php echo selectBoxSelection('200,000 and above', $_POST['price']);?> >200,000 and above</option>
<option value='see all' <?php echo selectBoxSelection('see all', $_POST['price']);?> >See All</option>
</select>
instead of your code,
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try like this,
<select name='price' id='price'>
<option value='' >Select....</option>
<?php foreach ($arr as $key=>$value)
{
if($key == $_POST['price'])
{
echo "<option selected value=$key>$value</option>";
unset($arr[$key]);
}
else
echo "<option value=$key>$value</option>";
}?>
</select>
This will do it :
<form name="myform" method="post" action="testsel.php">
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="0-50,000") print "selected"; ?>>0-50,000</option>
<option value='50,000-100,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="50,000-100,000") print "selected"; ?>>50,000-100,000</option>
<option value='100,000-150,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="100,000-150,000") print "selected"; ?>>100,000-150,000</option>
<option value='150,000-200,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="150,000-200,000") print "selected"; ?>>150,000-200,000</option>
<option value='200,000 and above' <?php if(isset($_POST["price"]) && $_POST["price"]=="200,000 and above") print "selected"; ?>>200,000 and above</option>
<option value='see all' <?php if(isset($_POST["price"]) && $_POST["price"]=="see all") print "selected"; ?>>See All</option>
</select>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST["price"]))
print $_POST["price"];
?>
Save this file as "testsel.php" and view. It will serve the purpose.
<select name='price' id='price'>
<option value='' >Selec</option>
<option value='0-50,000'<?php if($_POST['price']=='0-50,000'){echo "selected==selected";}?>>0-50,000</option>
<option value='50,000-100,000'<?php if($_POST['price']=='50,000-100,000'){echo "selected==selected";}?>>50,000-100,000</option>
<option value='100,000-150,000'<?php if($_POST['price']=='100,000-150,000'){echo "selected==selected";}?>>100,000-150,000</option>
<option value='150,000-200,000'<?php if($_POST['price']=='150,000-200,000'){echo "selected==selected";}?>>150,000-200,000</option>
<option value='200,000 and above'<?php if($_POST['price']=='200,000 and above'){echo "selected==selected";}?>>200,000 and above</option>
<option value='see all'<?php if($_POST['price']=='see all'){echo "selected==selected";}?>>See All</option>
</select>

Drop down select box with php

I wrote some php to deliver a select box. For some reason it refuses to select the correct option. When I try the HTML directly it works. To check I wasn't doing anything stupid. I copied the generated code and they put the code to generate and the html.
<dd>
<select id="jform_my_foreign_key" class="inputbox " name="jform[my_foreign_key]" size="1">
<option value="">- Select Order title -</option>
<option value="13">00000013</option>
<option value="12">00000012</option>
<option value="9" selected="selected">00000009</option>
<option value="8">00000008</option>
<option value="7">00000001</option>
</select>
</dd>
<select id="jform_my_foreign_key" class="inputbox " size="1" name="jform[my_foreign_key]">
<option value="">- Select Order title -</option>
<option value="13">00000013</option>
<option value="12">00000012</option>
<option selected="selected" value="9">00000009</option>
<option value="8">00000008</option>
<option value="7">00000001</option>
</select>
The top is generated by the php code and the bottom is me putting the html directly into
the page
Code used is:
<dd><select id="jform_my_foreign_key" class="inputbox " size="1" name="jform[my_foreign_key]">
<option value="">- Select Order title -</option>
<?php
for($x=0;$x<count($ordersAvailableHoldingArray);$x+=2){
if($ordersUseHoldingArray[0] ==$ordersAvailableHoldingArray[$x+1]){
$selected ="selected='selected'";
}else{
$selected ="";
}
echo '<option '.$selected.' value="'.$ordersAvailableHoldingArray[$x].'">'.$ordersAvailableHoldingArray[$x+1].'</option>';
}
?>
</select></dd>
I can't figure out why it doesn't work it looks right to me. Any help where to start debugging would be great
You might be better off using a foreach to put key and value in select as example below.
<form action="" method="post">
<select id="jform_my_foreign_key" class="inputbox" name="jform[my_foreign_key]" size="1">
<option value="">- Select Order title -</option>
<?php
$data_for_select = array(13=>'00000013',12=>'00000012',9=>'0000009');
foreach($data_for_select as $k=>$v)
{
if($k==$_POST['jform']['my_foreign_key'])
{
echo "<option value=\"$k\" selected=\"selected\">$v</option>";
}
else
{
echo "<option value=\"$k\">$v</option>";
}
}
?>
</select>
<input name="jello" type="submit" value="send">
</form>

$POST_ to return array from multiple checkbox

Hi I have looked at other answers and they said to add name=checkbox[] to get the array to return but it doesn't appear to working.
The HTML is:
<select class="select" multiple="multiple" name="suburb[]" id="suburb">
<option selected="selected" name="suburb[]" value="Southbank">Southbank</option>
<option selected="selected" name="suburb[]" value="Melbourne">Melbourne</option>
<option selected="selected" name="suburb[]" value="Docklands">Docklands</option>
<option selected="selected" name="suburb[]" value="South Melbourne">South Melbourne</option>
<option selected="selected" name="suburb[]" value="West Melbourne">West Melbourne</option>
<option selected="selected" name="suburb[]" value="Point Cook">Point Cook</option>
<option selected="selected" name="suburb[]" value="Sanctuary Lakes">Sanctuary Lakes</option>
<option selected="selected" name="suburb[]" value="Truganina">Truganina</option>
<option selected="selected" name="suburb[]" value="Williams Landing">Williams Landing</option>
PHP code is:
$message .= "<tr><td><strong>Interested Suburbs:</strong> </td><td>" . strip_tags($POST_['suburb']) . "</td></tr>";
$_POST['suburb']
Is an array not a string also you mispelled it its $_POST, so you would need to loop through it to post like so:
$message .= "<tr><td><strong>Interested Suburbs:</strong> </td><td>";
foreach ($_POST['suburb'] as $suburb)
{
$message .= strip_tags($suburb) . "<br />\n";
}
$message .= "</td></tr>";
Works fine: you just need to use $_POST and print_r the array to see its contents. Try this, and you'll see it works fine.
By the way, you don't need a name attribute on your options
<form method="post" action="<?=$PHP_SELF?>">
<select name="suburb[]" class="select" multiple="multiple" id="suburb">
<option selected="selected" value="Southbank">Southbank</option>
<option selected="selected" value="Melbourne">Melbourne</option>
<option selected="selected" value="Docklands">Docklands</option>
<option selected="selected" value="South Melbourne">South Melbourne</option>
<option selected="selected" value="West Melbourne">West Melbourne</option>
<option selected="selected" value="Point Cook">Point Cook</option>
<option selected="selected" value="Sanctuary Lakes">Sanctuary Lakes</option>
<option selected="selected" value="Truganina">Truganina</option>
<option selected="selected" value="Williams Landing">Williams Landing</option>
</select>
<button type="submit">test</button>
</form>
<?php
print_r($_POST['suburb']);
?>
The variable you're looking for is $_POST, not $POST_; see the PHP reference on $_POST for more details.
Otherwise, from what you have shown, it's probably fine. Add the HTML for your form tag as well if things still don't work.

Categories