I put a first option in my select element just to tell the user what they are selecting, I think it's advisable to put a null first option, but I want to put an error message that the user didnt select anything if he is just selecting the first option which is the null option here's my code
<select class = "strand" name = "strand">
<option >=====Strand=====</option>
<option value ="GAS">GAS</option>
<option value ="ICT">ICT</option>
<option value ="STEM">STEM</option>
<option value ="HUMSS">HUMMSS</option>
<option value ="ABM">ABM</option>
</select>
Related
I have a form with a select box where the user can select his gender. I succeed in getting the selected value and displaying this. But I want to use another term for the selected value.
For example, there are 2 values in the select box, named "man" and "vrouw".
<select name="geslacht" class="formulier-input">
<option value="" disabled selected hidden>Kies uw geslacht</option>
<option value="man">Man</option>
<option value="vrouw">Vrouw</option>
</select>
But I want to display Dhr. as the user selects for the value "man" and Mevr. as the user selects for the value "vrouw".
I guess I can do this in an if statement with:
if($geslacht === "the value"){ }
I don't know how I can finish this and I don't know what to put in the place of "the value".
How can I do this?
Is it possible to assign multiple values to select option drop down lists? I need to retrieve multiple pieces of data from each drop down when they are selected and I have only been able to get the "name" and an "id". The name is displayed for the user to select however it is the ID that is passed to be processed. Here is my code:
<?php
$att_1 = $att_1;
mysql_connect("xx.xx.xx.xx","xxxxxx","xxxxxx");
mysql_select_db("dezanjow_cf");
$sql=mysql_query("select id, name from model");
if(mysql_num_rows($sql)){
$select= '<select name="model">';
$select.='<option value="default">Select Model</option>';
while($rs=mysql_fetch_array($sql)){
$select.='<option value="'.$rs['id'].'">'.$rs['name'].'</option>';
}
}
$select.='</select>';
echo $select;
?>
This produces html that looks like:
<select name="model">
<option value="default">Select Model</option>
<option value="5">GH20</option>
<option value="6">GH21</option>
<option value="7">GH22</option>
</select>
I wish to display them like this (as example):
<select name="model">
<option value="default">Select Model</option>
<option value="5","abc">GH20</option>
<option value="6","def">GH21</option>
<option value="7","ghi">GH22</option>
</select>
Thus when the data is passed onto the next php script, I can use both data "5" and "abc" when the user selects "GH20". I have not seen much on Google about this and don't even know if this is possible. Please let me know if I am asking for the impossible! Many Thanks, Nick
Hey you cannot have multiple values like that because post or get will return just first value which in this case is 5
<option value="5","abc">GH20</option>
but you can make your value looks like this
<option value="5,abc">GH20</option>
and then in php script you can separate values by using explode function
explode(',', $_POST['model'])
which will return array that you can use
array( 0=> 5, 1=>abc)
Its better if you serialize your data then at next php script unserialize it.
$data=array($rs['id'], "abc"); //data to be stored
$serialized_data=serialize($data); //serialize data
$select.='<option value="'.$serialized_data.'">'.$rs['name'].'</option>';
IMPORTANT
//unserialize information
$unserialized_data=unserialize($serialized_data);
this will output the same array array('SOMEID', "abc")
I am currently working on a project where a user chooses an option from a select box and submits a form, the form is then processed by PHP, and the PHP code determines what the select box value is, and does something based on that value.
My select box is called combined_group and has two select values: philharmonic_orchestra and symphony_orchestra.
This is how I am checking the selected value:
if($_POST['combined_group'] == "philharmonic_orchestra"){
$_SESSION['semesterprice'] = "170";
$_SESSION['fullprice'] = "330";
}
if($_POST['combined_group'] == "symphony_orchestra"){
$_SESSION['semesterprice'] = "275";
$_SESSION['fullprice'] = "530";
}
But when PHP runs through this code, neither if statement is chosen. I know that the value of $_POST['combined_group'] is, in fact, either of those two values, just PHP isn't picking it up for some reason.
Anybody care to help?
EDIT: My HTML form code is as follows
<select name="combined_group" class="OBJ-1">
<option value="" selected="">Select One</option>
<option value="philharmonic_orchestra">Philharmonic Orchestra</option>
<option value="symphony_orchestra ">Symphony Orchestra</option>
</select>
Client side
<select name="combined_group">
<option value="">Select an option</option>
<option value="philharmonic_orchestra">Philharmonic Orchestra</option>
<option value="symphony_orchestra">Symphony Orchestra</option>
</select>
Server side
if (! isset($_POST["combined_group"]))
{
exit('not set');
}
if (trim($_POST["combined_group"]) == '')
{
exit('not selected');
}
if (trim($_POST["combined_group"]) == 'philharmonic_orchestra')
{
//business logic for 'philharmonic_orchestra'
}
else
{
//business logic for 'symphony_orchestra'
}
Most likely is a bad HTML syntax. Check if your option item has value attribute:
<option value="...">...</option>
The reason why your conditional statement is failing, is because of a space in your option's value.
<option value="symphony_orchestra ">
^ right there
What you will need to do is remove it:
<option value="symphony_orchestra">
^ deleted space
Technical sidenote:
Had your conditional statement been:
if($_POST['combined_group'] == "symphony_orchestra ")
^ notice the space
with the space before the quote, it would have worked.
Anything between quotes is considered and part of an element's value.
Let's say, that i have users stored in a database, identified by an id (an integer primary key). All the users have a multiple choice attribute, for example privileges on a site (Admin, Member, Administrator etc...). I would like to create a php admin page, where i can select for each user his/her privileges, and all users at one page (or at least more users at one page):
Tom [_privileges_v_]
Ed [_privileges_v_]
Lisa [_privileges_v_]
...
How would it efficient to do?
if i print the dropdowns with the name attribute set as the id of the record, then how could i access them through php?
<select name="12323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<select name="4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
Then after i post this form, my $_POST array will contain a $_POST["12323"] and a $_POST["4323"] member.
How could i make something like this, so i could iterate through the $_POST values?
Thanks for any kind of help!
You can output a select box like this
<select name="user_4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<input name="otherinp" value=""/>
in case your form has another input.... then in php code you can do
foreach($_POST AS $name=>$value) {
if (strpos($name, "user_")===0){
$userid = str_replace("user_","",$name);
//Do st with userid
}
}
//Work with orther input $_POST['']
Either fetch all the users again and loop through their IDs, then check if there is a post value linked to it, or loop through all the post values.
foreach($_POST AS $name=>$value) {
echo "Selection box with name {$name} has value {$value}";
}
Let's say I have this:
<select name="exposure">
<option value='1,0.005'>Micro</option>
<option value='2,0.007'>Mini</option>
</select>
<select name="clicks">
<option value="2500">2500 - Clicks</option >
<option value="500">500 - Clicks</option >
</select>
<label>Price:</label>
<div id="price"></div>
How can I do, so whenever I select something from select="exposure", it will automatically change the OPTIONS of select="clicks", and at last, take the value from select="clicks" and then multiply it with the second value of select="exposure".
Example
I have selected
<option value='2,0.007'>Mini</option>
And with this option, the only click package that is available is:
<option value="2500">2500 - Clicks</option >
Price: 0.007*2500 = 17,5
Is that possible?
Try this
$("select[name=clicks]").change(function(){
$("label").text( parseFloat($(this).val()) * parseFloat($("select[name=clicks]:option:eq(1)")[0].value) );
});
To "select value of select":
$('select[name="exposure"]').val('2,0.007');
To make only some options available:
$('select[name="clicks"] option[value="500"]').remove();
To extract data from value:
var splitResult = $('select[name="exposure"]').val().split(',');
alert(splitResult[0] * splitResult[1]);
But I personally suggest to not pass additional information via the attribute "value"; better solution would be to create a new attribute to such needs.