I am using Magento and using Custom Options. I have 5 custom options that allow the user to choose the condition of the item. I want some text below to show what the condition means when they select it.
Im sorry for the confusion. I want this to update live on the screen when the appropriate option is selected.
Here is my current code but it isn't displaying the text.
<select name="options[1][]" id="select_1" class="multiselect required-entry product-custom-option" title="" onchange="opConfig.reloadPrice()">
<option value="1" price="0" >Perfect </option>
<option value="2" price="-35" >Excellent </option>
<option value="3" price="-105" >Good </option>
<option value="4" price="-140" >Poor </option>
<option value="5" price="-252" >Broken </option></select>
<?php if( $_POST['select_1']=='1' ){
echo "Perfect Condition Text";
}elseif( $_POST['select_1']=='2' ){
echo "Excellent Condition Text";
}elseif( $_POST['select_1']=='3' ){
echo "Good Condition Text";
}elseif( $_POST['select_1']=='4' ){
echo "Poor Condition Text";
}elseif( $_POST['select_1']=='5' ){
echo "Broken Condition Text";
} ?>
Changing the <select> name to select_1 will solve your problem:
<select name="select_1" id="select_1" class="multiselect required-entry product-custom-option" title="" onchange="opConfig.reloadPrice()">
If you can't change the names for some reason, change $_POST['select_1'] to $_POST['options'][1][0]
If you want to display the value of the <select> without refreshing the page then you could use javascript or jquery. Here is a sample of Javascript:
<script language="javascript">
function displayCondition() {
condition = new Array("", "Perfect", "Excellent", "Good", "Poor", "Broken");
var getsel = document.getElementById('select_1').value;
document.getElementById("divId").innerHTML = condition[getsel];
}
</script>
</head>
<body>
<form name="formName">
<select name="options[1][]" id="select_1" class="multiselect required-entry product-custom-option" title="" onchange="displayCondition()">
<option value="0">Select Condition</option>
<option value="1" price="0" >Perfect</option>
<option value="2" price="-35" >Excellent</option>
<option value="3" price="-105" >Good</option>
<option value="4" price="-140" >Poor</option>
<option value="5" price="-252" >Broken</option>
</select>
<div id="divId" name="divName" ></div>
</form>
</body>
</html>
The array will not be available in PHP $_POST by select_1 index but PHP $_POST is going to have an options array $_POST['options']
The post array name is based on the name of the form element, not the id of the form element. Change the name property of your select element to "select_1" and you will get what you are looking for.
Related
I have a dropdown and i want to show the value which is selected.
i have tried
<select name ="dd">
<option> option1 </option>
<option> option2 </option>
<option> option3 </option>
</select>
<?php
$a = $_POST["dd"];
echo $a; ?>
But it always shows undefined index :dd
YOu must use value Atribiute like bellow,
<option value="option1" >option1</option>
then your code will work fine ;)
You must have something, which will have a value what you selected. For example, let's say, that this is in form with method="post".
Then, after clicking the submit button, your selection is stored in $_POST["dd"]. So you can compare, if value of option is equal to value in a $_POST. If yes, add an attribute "selected" So for example, your select can look like:
<select name ="dd">
<option value="1" <?=(($_POST["dd"]==1)?"selected":"")?>> option1 </option>
<option value="2" <?=(($_POST["dd"]==2)?"selected":"")?>> option2 </option>
<option value="3" <?=(($_POST["dd"]==3)?"selected":"")?>> option3 </option>
</select>
You can also compare string values in , but don't forget to use "" or '' if you are writting down string
You just need a value attribute in your html - You also need to add a form and a button.
<FORM method="post" action="yourpage.php">
<select name ="dd">
<option value="option1"> option1 </option>
<option value="option2"> option2</option>
<option value="option3"> option3 </option>
</select>
<INPUT name="Submit" type="submit" value="Submit" />
</FORM>
Or you can simply get the value using javascript:
Your Html will need a id:
document.getElementById("myselect").onchange = function() {
alert(document.getElementById("myselect").value);
}
<select name="dd" id="myselect">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>
I am currently working on a edit form with drop down list. I have a edit form that display current item from database. But I have no idea how my drop down menu to display current value from database that match drop down list. I know my explanation a bit confuse therefore I picture below will show better explanation.
This is one of my edit form that showing drop down menu. What I want is display database value with this view of drop down menu.
Example :
My database value is Pending. Then it will show Pending inside this view of drop down menu. And when I click on the drop down menu, it will show me like below picture :
this is what I am looking for. And below is the code that I tried.
<div class="floatleft">
<select name="select2" class="select-form-standard" >
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
</div>
But I just know how to design a drop down menu and do not have idea how to work like this. I am working with smarty framework. Can someone give me some solution to work on it? Thanks in advanced.
Add selected attribute for the option which must be selected. Assume $value = 0
<?php $value = 0; ?>
<select name="select2" class="select-form-standard" >
<?php foreach($options as $id => $option) { ?>
<option value="<?php echo $id ?>"<?php
if($id == $value) {
echo " selected";
} ?>><?php echo $option ?></option>
<?php } ?>
</select>
Try this:
$statusval is variable coming from your database
<option value="0" id="0" <?php if($statusval==0){echo "selected;"}?> name="status">Deleted</option>
<option value="1" id="1" <?php if($statusval==1){echo "selected;"}?> name="status">Active</option>
<option value="2" id="2" <?php if($statusval==2){echo "selected;"}?> name="status">Pending</option>
<option value="2" id="2" <?php if($statusval==3){echo "selected;"}?> name="status">Suspended</option>
<?php
$databaseValue = $your_database_value;
?>
<select name="select2" class="select-form-standard" >
<option value="0" <?php echo (($databaseValue==0)?"selected":"") ?> id="0">Deleted</option>
<option value="1" <?php echo (($databaseValue==1)?"selected":"") ?> id="1">Active</option>
<option value="2" <?php echo (($databaseValue==2)?"selected":"") ?> id="2">Pending</option>
<option value="3" <?php echo (($databaseValue==3)?"selected":"") ?> id="3">Suspended</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="status">
<option value style='display:none;'>-SELECT Status--</option>
$select_data=mysql_query("Select status,options from yourtable");
while($result=mysql_fetch_array($select_data)){
echo "<option value ='YourValue' ".selectBoxSelection('YourOptionValue', $result['status']).">".$resule['option']."</option>";
}
</select>
<select name="select2" class="select-form-standard" >
<option><?php echo $select2e; ?></option>
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
You need to add the attribute "selected" to the option you wish to have preselected:
<option value="2" id="2" name="status" selected>Pending</option>
or if you are using xhtml you will need to use
selected="selected"
To have this work off a value in database you will need to write an if statement to determine if the database value is the same as the option value, and if it is, then echo out the "selected" attribute within the option tag.
you can easily do it by a simple if statement. inside your loop try this
<option value="<?= $row['id']) ?> " <?= $row['id']==$categoryIDfromDatabase?'selected':''; ?> >
<?= $row['categoryName'] ?>
</option>
I want the user to be forced to select any option in the drop down apart from the default "selected" option
here is my drop down menu code;
<form><label>Select Tank:</label>
<select class="text2" name="tank" id="mySelect">
<option value="0" selected="selected">Select your tank</option>
<option value="4"> Tank#1 </option>
<option value="9"> Tank#2 </option>
<option value="21"> Tank#3 </option>
<option value="34"> Tank#4 </option>
</select>
<input type="button" id="btncheck" value="Submit"/>
</form>
and here is my java;
$('#btncheck').click(function(){
if ($("#mySelect ")[0].selectedIndex <= 0) {
alert("Please select a tank from the menu!");
return false;
}
});
jsfiddle: http://jsfiddle.net/36JZL/41/
for some reason it validates correctly but form isnt submitted.
You can do it with jQuery on client side but its not a good option to validate things on client side.
PasteBin: http://jsbin.com/zer/1
<form id="myForm">
<label>Select Tank:</label><br />
<select class="text" name="tank" id="tankSelector">
<option value="All" selected="selected"> Select Tank </option>
<?QUERY HERE TO PULL AND LOOP FROM DATABASE?>
<option value="<?php echo $row->id; ?>"> <?php echo $row->description; ?> </option>
<? END LOOP ?>
</select>
<input type="submit">
</form>
<script>
$(function() {
$('#myForm').submit(function(e){
if($('#tankSelector').val() == 'All') {
alert('Select tank!');
e.preventDefault();
}
});
});
</script>
<label>Select Tank:</label><br />
<select class="text" id="seltank" name="tank">
<option value="All" selected="selected"> Select Tank </option>
</select>
in the client side click event of your submit button add some javascript to check the value of your drop down.
if(document.getElementById("seltank").value == "All")
{
//put some code to alert the user or show error
return false; //this should prevent your post
}
Similar post is here How do I cancel form submission in submit button onclick event?
Here's what I do:
<select class="text2" name="tank" id="mySelect">
<option></option>
<option value="4"> Tank#1 </option>
<option value="9"> Tank#2 </option>
<option value="21"> Tank#3 </option>
<option value="34"> Tank#4 </option>
</select>
If the user hits submit without making a selection, they will be prompted to select an option automatically. The limitation is that you don't get a pretty little "select an option" placeholder.
Maybe this can help. Im yet to find a stable answer
If using php:
$val = $_POST['select_input']; <--- works fine
or
$val = $request->select_input <---- can be problematic
On Html
<select id="select_input" name="select_input" >
<option value="0">Pending</option>
<option value="1">Approved</option>
<option value="0" selected>Pending</option>
</select>
I am wanting to show a hidden field but only when an option is selected in the dropdown. It does not matter which option is selected but I need one to be selected.
Javascript:
<script type="text/javascript">
function Select(sel,id,nu){
document.getElementById(id).style.display=sel.selectedIndex==nu?'block':'none';
}
</script>
Selection:
<select name="options[1]" id="select_1"
class=" required-entry product-custom-option"
title="" onchange="opConfig.reloadPrice();displayCondition();Select(this,divShow,1)">
<option value="" >-- Please Select --</option>
<option value="1" price="0" >Perfect </option>
<option value="2" price="-35" >Excellent </option>
<option value="3" price="-105" >Good </option>
<option value="4" price="-140" >Poor </option>
<option value="5" price="-252" >Broken </option>
</select>
The above onchange will only work if the first item is selected, so I am unsure how to make it work for any item selected. Also, I know the divShow should have some ' around it but I don't know how to write it into the following php:
$extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();Select(this,'."divShow".',1)"';
Div Box:
<div id="divShow" style="display:none;">
<?php echo $this->getPriceHtml($_product) ?></div>
You do have to display simple quote to make divShow a JS string. In PHP (and in most of common languages) you escape them with \:
$extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();Select(this,\'divShow\',1)"';
Without these quotes, your JS would try to look for divShow variable.
I have an intial group of options like these:
<select name="item[type]">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
I want to check if a variable isset($test) is defined. If it is, then I want to change the option selected where the value is equal of $test. Something like this <OPTION SELECTED>
For example. $test = 3; so, the option selected should be fourth. If $test is empty or not defined, then the first should be the option that is selected.
One way:
<select name="item[type]">
<option <?=$test==0?'selected="selected"':'';?> value="0" class="dr">First</option>
<option <?=$test==1?'selected="selected"':'';?> value="1" class="dr">Second</option>
<option <?=$test==2?'selected="selected"':'';?> value="2" class="dr">Third</option>
<option <?=$test==3?'selected="selected"':'';?> value="3" class="dr">Fourth</option>
</select>
Another:
<? $selected[$test] = 'selected="selected"'; ?>
<select name="item[type]">
<option <?=$selected[0];?> value="0" class="dr">First</option>
<option <?=$selected[1];?> value="1" class="dr">Second</option>
<option <?=$selected[2];?> value="2" class="dr">Third</option>
<option <?=$selected[3];?> value="3" class="dr">Fourth</option>
</select>
<select name="item[type]" id="selectBoxId">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
<script type="text/javascript">
var test = "<?= $test; ?>";
if (test != '' && parseInt(test)) {
document.getElementById('selectBoxId').selectedIndex = test;
}
</script>
Remove "[type]" from select name, make it simple to "item".
Then execute this code.
$test = isset($_POST['item']) ? $_POST['item'] : "0";
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "<option value=\"$loop_var\" class=\"dr\"$selected_html>$text</option>";