I want to comment off .cities whenever I select the value Singaporean however it should get visible If I choose any other nationality apart from singaporean. Is it possible in PHP ?
<div class="cities">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control'>
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
I tried this but for some reason it is not working
<?php
if ($_POST['emp_nationality'] != 'Singaporean') {
echo '<div class="cities">New York</div>';
}
?>
ERROR: Undefined index: emp_nationality
You can use an onchange handler in js and hide / show the ".cities" div depending on the value chosen. Note I added a display:none to the .cities since your first option in the select list is Singaporean and the function requires a change to trigger it. Also I would normally separate the onclick as a separate handler - rather than mixing it into the HTML but for this example I did it this way. Does not require PHP which would need the option to be selected on page load and would not easily change the visibility of the div on a different choice. This functions to hide the div if the correct option is selected and show it on other option selections.
function hideDiv(option)
{
if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cities" style="display:none">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
<div class="cities" style="display:none">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
//js
function hideDiv(option)
{
if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
}
If you like would to avoid the "Undefined index: emp_nationality" you need to make sure this variable exists before checking it's value. Use isset( ).
http://php.net/manual/en/function.isset.php
if (isset($_POST['emp_nationality']) && ($_POST['emp_nationality'] != 'Singaporean')) {
echo '<div class="cities">New York</div>';
}
Related
I need help with this: HTML
<select class="nesty-input" style="max-width: 100%" name="what_about">
<option selected disabled hidden><?php echo $what_about[1]; ?></option>
<option value="0|-">-</option>
<option value="1|one">one</option>
<option value="2|two">two</option>
<option value="3|three">three</option>
</select>
<select class="nesty-input" style="max-width: 100%" name="im">
<option selected disabled hidden><?php echo $im[1]; ?></option>
<option value="0|-">-</option>
<option value="1|one">one</option>
<option value="2|two">two</option>
<option value="3|three">three</option>
</select>
PHP
$what_about = explode('|', $_POST['what_about']);
$im = explode('|', $_POST['im']);
So, what basically does this script: on page reload prints the value (0=id, 1=text), this because I wanted to do that in the database will be archived the id of the selected item, the problem is that when I select a value and I reload the page it still remains, but when I select the new one the value of the old is null.
Also known as: Warning: Undefined offset: 1.
You default selected element has no vlaue. So then you you submit it you get default values of $what_about and $im equals ['']. And somewhere in your code later you are calling for $what_about[1] or $im[1]
I would recommend you to check if count($what_about) == 2 before you call for $what_about[1] and if count($im) == 2 before you call for $im[1]
or set the value for default select option
<option selected disabled hidden value='|'><?php echo $im[1]; ?></option>
I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>
I have this drop down list and my idea is when i click on videos that i disable other drop down list.This is not working.Do i need something else?
<select name="Type" id="type">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
<?php
$value=$_POST['image'];
?>
</select>
<select id="text-one" <?php if($value=="V"){ ?> disabled="disabled"<?php }?> >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select
You can do this with PHP, but that's going to require submitting form the data so you can then access $_POST, and then loading a new page, that's basically the same as the original, just with text-one disabled.
Remember, PHP runs server side, and once it's rendered in the browser, nothing else can be done via PHP. For example, you've got a line in your sample code that show's you tring to assign $_POST['image'] to $value - until you submit a form, $_POST will be empty.
Most likely, you want to do this client side and without a reload, and this can be done using javascript.
As a basic overview:
monitor the onChange event handler for your type select element
check the value of the type select element, and set the disabled attribute on the text-one as needed
Another option (maybe simpler?):
attach an onclick attribute to the video input, that will run a javascript function that disables text-one
jQuery will make some of this easier, but you could write all of the above in plain javascript, without any libraries.
try this code :
<script>
function disable(val)
{
if(val=="V")
document.getElementById("text-one").disabled=true;
else
document.getElementById("text-one").disabled=false;
}
</script>
<select name="Type" id="type" onchange="disable(this.value)">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
</select>
<select id="text-one" >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select>
Sorry, I know I asked this yesterday, but the answer I got was for Javascript, which worked for me, but now I am writing the PHP script and I can't figure out the PHP Version either. I tried to research it, but nothing came up.
I am trying to create a validation script for a few selectors such as month, day, and year for your birthday. Basically I want the script to run as:
IF the selector is on the first option return false ELSE return true
What is the IF statement I need in order to return the script false if the first option in the list is selected.
For example,
<select>
<option>FAIL IF THIS IS SELECTED</option>
<option>PASS IF THIS IS SELECTED</option>
<option>PASS IF THIS IS SELECTED</option>
...And So On
</select>
HTML
<form action="post.php" method="post">
<select name="test" id="test">
<option value="0">FAIL IF ...</option>
<option value="1">PASS IF ...</option>
<option value="2">PASS IF ...</option>
</select>
</form>
PHP inside some validate function (form.php)
if($_POST['test'] == "0") return false;
else return true;
You can supply value attribute inside each option tag
<select>
<option value="">FAIL IF THIS IS SELECTED</option>
<option value="something">PASS IF THIS IS SELECTED</option>
<option value="something else">PASS IF THIS IS SELECTED</option>
...And So On
</select>
Then check the value of the <select> tag by using
if($_POST['name of select tag'] == '') // first selector is chosen
{
// display the error message
}
i want to know that how to get multiple selectbox values in php
means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user
i think an array should be used but i dont know how??
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
like in above example
how do i get know that how many selectboxes were used and how to get value of each box
code for adding the new dropdown
function addRow()
{
var newRow = document.all("tblGrid").insertRow();
var oCell = newRow.insertCell();
oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.
So you can do it something like this:
<select name="myselect[]" id="select1">
<select name="myselect[]" id="select2">
<select name="myselect[]" id="select3">
After form post you can get all select box values:
print_r( $_POST['myselect'] );
For number of select boxes on page you can try:
echo count( $_POST['myselect'] );
Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.