JS showing field if dropdown selected - php

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.

Related

How to display an item from dropdown in php (no database)

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>

force option from <select> drop down before submit form

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>

How to prevent the set of a get variable?

I have a question. Let me explain this with an example.
I have this piece of code:
<form action="vehicles.php" method="get">
<span>Marca:
<select name="brand">
<option value="null" selected="selected"></option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="null" selected="selected"></option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Is any way to prevent the assignment of those variables if the option selected is the "null" one?
If, for example, I select brand="Renault" and model="null" the url is
http://mywebpage.com/vehicles.php?brand=Renault&model=null
but it should be
http://mywebpage.com/vehicles.php?brand=Renault
I know how to unset the variables with "null" value after the form submission with PHP. Is any way to do it before the submission and after the variables are setted to "null".
I would like a cleaner url, free of "variable=null" results.
P/D: I don't have a native english speaking so feel free to edit my question. I wish you understand me.
I am afraid that you might need javascript to achieve what you want.
Take a look at this code:
<form action="vehicles.php" id="carForm" method="GET">
<span>Marca:
<select name="brand" id="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model" id="model">
<option value="none" selected="selected">-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="button" value="Submit" onclick="submitFormFilter();" />
</form>
Javascript : code will be like this:
<script>
function submitFormFilter(){
var myForm = document.getElementById("carForm");
var carBrand = document.getElementById("brand");
var carModel = document.getElementById("model");
if(carBrand.value === "none" || null){
carBrand.parentNode.removeChild(carBrand);
}
if(carModel.value === "none" || null){
carModel.parentNode.removeChild(carModel);
}
myForm.submit();
}
</script>
http://jsfiddle.net/7xzKP/1/
you can it try here.
Include the value attribute of your option tags.
<option value="" selected="selected">-</option>
You should really do this for all of your options.
if you don't want to use $_GET superglobal because of the url extension that it comes with try using $_POST instead. It will not have a url extension and it can still store values that you can later retrieve. Just be sure to change your method to equal POST instead of GET.
So the code for the form tag would change to:
<form action="vehicles.php" method="POST">
And you can later access it by (for example):
echo $_POST['brand'];
or
echo $_POST['model'];
as well, you probably want to add a value param to the values that you have in your option tag.
EDIT-
I've added this new section since you don't want to use POST even though I think you should.
You can stay with the GET method by doing this line of code:
<form action="vehicles.php" method="GET">
<span>Marca:
<select name="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="none" selected>-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Let me know if that helps
You should set the value for the option tag of <select> :)
You's missing set value for option tag. you should set the value for option tag in select tag. With the blank value, you can assign it to a special value like zero(0)
<select name="model">
<option value="0" selected>-</option>
<option value="1">206</option>
<option value="2">Suran</option>
<option value="3">Passat</option>
<option value="4">Punto</option>
</select>
EDIT:
Ok. I got your point. You can import jquery to your page and make a check by javascript before you submit, if the value of select box is blank. you can remove it.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$.ready(function(){
$("form").submit(function(){
if($("select[name='model']").val() == "0") $("select[name='model']").remove();
return true;
});
})
</script>

Echo text based off of dynamic dropdown

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.

How can I stop an empty SQL query from displaying all results?

I am creating a meat packaging search form, users can search for different packages using multiple forms and dropdown boxes. I have a had a lot of problems but most of it is sorted now, I only need to create an "Any" search for the dropdown boxes and the problem of empty textboxes displaying all results.
Currently when a user sends a search, they may have entered in some other text boxes, but when one of the forms is left empty that automatically displays all of the results. I want it so when a search is sent and a box is empty, the code ignores that form and just checks the ones that have info inside of them.
here is my test code (not my current final form code):
<body>
<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("delyn_db", $con);
if (!$con)
{
die("Could not connect: " . mysql_error());
}
$descrip = mysql_real_escape_string($_POST['descrip']);
$sql = "SELECT * FROM delyn WHERE description LIKE '%" . $descrip . "%'";
$r_query = mysql_query($sql);
if ($descrip === "")
{
echo 'Null value';
}
while ($row = mysql_fetch_array($r_query))
{
echo '<br /> Description: ' . $row['description'];
}
?>
</body>
Anyone have any ideas on how to stop this?
EDIT: Sorry here is my HTML with the search boxes. The above php is just where the values are sent.
<body>
<form action="form5null.php" method="post">
<label for="description">Description:</label> <input type="text" name="descrip">
<br>
<label for="trayheight">Trayheight:</label> <input type="text" name="height">
<br>
<label for="traywidth">Traywidth:</label> <input type="text" name="width">
<br>
<label for="traydepth">Traydepth:</label> <input type="text" name="depth">
<br>
<label for="trayrange">Trayrange:</label> <select name="trayrange">
<option value="BBQ">
BBQ
</option>
<option value="Dessert">
Dessert
</option>
<option value="Display">
Display
</option>
<option value="Meat">
Meat
</option>
<option value="Microwave">
Microwave
</option>
<option value="Party">
Party
</option>
<option value="Salad/Wet Pasta">
Salad/Wet Pasta
</option>
<option value="Snacks">
Snacks
</option>
<option value="Standard">
Standard
</option>
</select> <label for="traytype">Traytype:</label> <select name="traytype">
<option value="Open">
Open
</option>
<option value="Cavitised">
Cavitised
</option>
<option value="Lid">
Lid
</option>
<option value="Tray">
Tray
</option>
<option value="Coallition">
Coallition
</option>
<option value="Bowl">
Bowl
</option>
<option value="Hinge pack">
Open
</option>
<option value="Pot">
Pot
</option>
<option value="Base & Lid">
Base and Lid
</option>
<option value="Rectangular">
Rectangular
</option>
<option value="Specalist">
Specialist
</option>
</select>
<br>
<label for="trayshape">Trayshape:</label> <select name="trayshape">
<option value="Rectangular">
Rectangular
</option>
<option value="Oval">
Oval
</option>
<option value="Square">
Square
</option>
<option value="Insert">
Insert
</option>
<option value="Round">
Round
</option>
<option value="Open">
Open
</option>
</select>
<br />
<input type="submit" value="Submit">
</form>
</body>
Add the following as the first option to all your dropdowns:
<option value="*">All</option>
Then use the SQL I provided in your duplicate question here:
Make SQL ignore a specific search value if clicked in a dropdown?
Dirk Nachbar gave a good answer as well.
Have you tried limiting your SQL queries by just checking if the values are blank/null (whichever is appropriate for PHP) and just not running the query if nothing is filled in?
e.g. (pseudocode)
if(box1.HasText || box2.HasText || box3.HasText)
{
RunTheSqlForForm1();
}

Categories