How to access select boxes as array - php

I have four fields, out of them 3 is textboxes and 1 is dropdown. I am cloning them dynamically whenever user clicks on add more.
For textboxes i have given its name as array and its working fine (name="xyz[]").
Now the problem I am facing is with dropdown. I have tried same way to deal with it but its not working it seems. I can't access the values through PHP, it keeps giving me the values of the first select box only.
<div class="cust-input">
<select id="head_of_institute" name="head_of_institute[]">
<option>Select Management</option>
<option value="Principal">Principal</option>
<option value="Director">Director</option>
<option value="Other">Other</option>
</select>
<select id="head_of_institute" name="head_of_institute[]">
<option>Select Management</option>
<option value="Principal">Principal</option>
<option value="Director">Director</option>
<option value="Other">Other</option>
</select>
</div>
<div class="cust-input">
<input type="text" name="head_name[]">
<input type="text" name="head_name[]">
</div>
Can you please suggest how to solve this problem?
Thanks in advance

Your dropdown is not working only because, I'm sure that you are adding new dropdown with the same ID.
Yes, ID must be unique on a page. If you add same ID more than one then javascript will ignore another.
So, my suggestion is to add new dropdown with name="head_of_institute[]" and id="head_of_institute_1" here 1 can be a counter for next downdown it'll be head_of_institute_2.
:) enjoy!

Related

If-else using PHP, variable not working as intended

I am using OSTicket, a ticket support system. Using a dropdown menu in PHP, I would like to make a form which displays either one or two options, depending on the user. A regular user only shows scenario 1. Special users will see scenario 2.
Scenario 1 for regular users
<select name="filter" class="form-control">
<option value="">Please select</option>
<option value="1">Option 1</option>
Scenario 2 for special users
<select name="filter" class="form-control">
<option value="">Please select</option>
<option value="1">Option 1</option>
<option value="1">Option 2</option>
Every user is linked to a organisation in my database. Each organisation has it's own value (org_id). There is a connection to the database and the query is succesful.
In OSTicket, this function exists: $thisclient->getOrgId, which I can use to list the org_id for a user. When I echo it (echo $thisclient->getOrgId;) I see the org_id from that user, so it does work. But no matter what I try, whenever I enter it into the form, it does not work.
<select name="filter" class="form-control">
<option value="">Please select</option>
<option value="1">Option 1</option>
<?php if($thisclient->getOrgId == 3); {echo "<option value=\"2\">Option 2</option>";} ?>
If I change the if-statement above to 4, 5, 999, 203232, or whatever: it always seems 'true' so the second option is displayed. I only want to display the second option whenever the value equals a specific value in the database(e.g. 3). I hope someone can help me out.
Solution
getOrgId()
The code of
if($thisclient->getOrgId == 3);
contains two problems:
The ; specifies that nothing should be done if the condition is true. You need to remove this.
getOrgId is a method, you need to call it, like $thisclient->getOrgId()

Drop down on PHP form only working on first 2 items

I've made a landing page with form. And made 4 options from drop down select. The form send fine, but for some reason, only the 2 values of $499 and $999 work, the $1999 and $4999 don't work. I must be missing something? The from works and sends fine, I receive it and the info is there except for the 2 last options on drop down...
Code is here:
<select name="budget_selection" class="form-style-10-drop-down" required='required'>
<option value="">- select -</option>
<option value="$499">$499</option>
<option value="$999">$999</option>
<option value="$499">$1999</option>
<option value="$999">$4999+</option>
</select>
PHP for the form is over here:
http://pastebin.com/Ksznvmnq
Form in question is over here:
http://satearn.com/landing-page-jul-21/landing_page.html
Your option values have the same values as the first two.
You repeated the same values
<option value="$499">$1999</option>
<option value="$999">$4999+</option>
It should be
<option value="$1999">$1999</option>
<option value="$4999+">$4999+</option>

Dropdown always lists all values in codeigniter

My dropdown always lists all values all are visible every time.
view
<label>Choose Employees</label>
<select name="select_employee[]" multiple="multiple">
<option disabled="disabled" selected>Select</option>
<? foreach ($employee->result() as $var)
{?>
<option value="<?echo $var->emp_id;?>"><?echo $var->emp_name;?></option>
<?}?>
</select>
This is the permanent view. Is this a css issue..?
Removing the array symbol it exists proper view..but I want to keep it as an array.
initially vie this way
<select name="select_employee[]" multiple="multiple" size="1"> will display two options.
Default value is 1 for single select. If the multiple attribute is present, the default value is 4
You have multiple="multiple" in your code, which makes all the options visible because your user needs to be able to select more than one option.
Here is the documentation: http://www.w3schools.com/tags/att_select_multiple.asp

PHP associate form fields with other fields

I have a form that will be used for quoting items. The user will be able to add blocks of fields to the form which I have working at the moment. Consider the code below
<div class="fields">
<select name="landscaping[]">
<option value="1">Rocks</option>
<option value="2">Other Rocks</option>
</select>
<select name="veneer[]">
<option value="1">Veneer Rocks</option>
<option value="2">Other Veneer Rocks</option>
</select>
<input type="text" name="quantity[]">
</div>
The problem is the user may select either veneer or landscaping but not both but no matter what there will be a quantity associated with the choice. And likely an additional parameter. I need a way to associate quantity with veneer or landscaping in each instance of this section.
Since you are using javascript to hide one of the elements at a time, I would also include a hidden field which holds the value of the select field which you want to grab the value from. You could also just look for the one which has a value, but since they can probably each have a value, it's safer to send a value which tells you for sure which field you should pull from.
<div class="fields">
<select name="landscaping[]">
<option value="1">Rocks</option>
<option value="2">Other Rocks</option>
</select>
<select name="veneer[]">
<option value="1">Veneer Rocks</option>
<option value="2">Other Veneer Rocks</option>
</select>
<input type="text" name="quantity[]">
<!-- This value should change depending on which one is currently visible -->
<input type="hidden" name="selectedField" value="landscaping">
</div>
Then in your form processing page use something like the following:
$selection = $_POST[$_POST['selectedField']];
This will pull the value of your selectedField post dynamically and use it to then grab the correct select list. Using this method can be risky though... Make sure that you sanitize your POST data!

make select box values faded out/unclickable

I have a normal HTML select dropdown box
<select id="day" name="day">
<option value="0">All</option>
<option value="1">Mon</option>
<option value="2">Tue</option>
<option value="3">Wed</option>
<option value="4">Thu</option>
<option value="5">Fri</option>
</select>
But on occassion I want to make some options not clickable, e.g. the Text faded out slightly if possible, and then nothing to happen if the text/value is selected.
Anyone know how?
I'm writing my page in PHP.
Simply give the option tag an attribute 'disabled'.
<select>
<option value="1" disabled>1</option>
<option value="2">2</option>
</select>
So in this example, 1 will be faded out and un-selectable but 2 will be selectable.
IE 6 requires javascript to disable an item. There is a bug that prevents it from disabling individual items.
See here for details on how to implement this so that it functions in IE6:
http://www.lattimore.id.au/2005/07/01/select-option-disabled-and-the-javascript-solution/
There is a javascript solution here
http://www.lattimore.id.au/2005/07/01/select-option-disabled-and-the-javascript-solution/
There is no way to do this in IE without a JS hack sadly.

Categories