If-else using PHP, variable not working as intended - php

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()

Related

Select Box with Links, need code tweak, php + script

I'm trying to make a select box with links that a visitor can click, select an item from the list, and be directed to that page in a new tab. The select box should display "Select" by default which is just a placeholder and doesn't link to anything.
I have two codes that almost fit the need, but they're a little different and both come up short. The first one puts script in the head, and doesn't open in a new tab. The second uses script at the location and tries to find the "Select" page, which ends with 404.
Is there any advantage to using script in the header, versus
being part of the code snippet?
Can we somehow combine these,
using whichever version of code is better, to implement a "Select"
placeholder properly but open in a new window too.
What happens if a user has disabled scripts in his browser? Is there a way to account for that?
First:
<form action="dummy" method="post">
<select name="choice" size="1" onChange="jump(this.form)">
<option value="">Select...</option>
<option value="http://yoursite.com/test1/">Test 1</option>
<option value="http://yoursite.com/test2/">Test 2</option>
<option value="http://yoursite.com/test3/">Test 3</option>
</select></form>
Second:
<form name="form1">
<select name="menu1" id="menu1">
<option selected>Select...</option>
<option value="http://yoursite.com/test1/">Test 1</option>
<option value="http://yoursite.com/test2/">Test 2</option>
<option value="http://yoursite.com/test3/">Test 3</option>
</select>
<script type="text/javascript">
var urlmenu = document.getElementById('menu1');
urlmenu.onchange = function() {
window.open(this.options[this.selectedIndex].value);
};
</script>
I wouldn't recommend this, it's likely to be blocked by the browser. However:
$('#menu').on('change', function() {
window.open($(this).val(), '_blank');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="menu">
<option selected disabled>Select an option</option>
<option value="http://stackoverflow.com">StackOverflow</option>
<option value="http://google.com">Google</option>
</select>
If you open your console when you run the snippet you may see it's been blocked.

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>

How to access select boxes as array

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!

Getting a val from an option tag inside a form. CodeIgniter

The similar questions out there didnt really help me because they were too specific.
I have a form with a select input. I need the val that accompies the option tags.
When I var_dump the post data, I can see that the data being transferred is not the val of the option, but the name of the option.
i.e:
<select name="selector">
<option val="1">Option 1</option>
<option val="2">Option </option>
</select>
And my post data will say something like ["selector"]=> string(8) "Option 1"
I'm using CodeIgniter and I'm not sure how to get it to play along.
I want to avoid doing something convoluted with JavaScript and getting the .val() and assigning it to something else. That would be lame.
The attribute you use to set an option value is value not val
<option value="1">Option 1</option>
While using codeigniter, better practice would be settings selects using form helper, so if we have array of options we can pass it into view and generate out like:
<?php echo form_dropdown('selector', $optionsArray, 0); ?>
this one will generate select dropdown like this:
<select name="selector">
<option value="0" selected="selected">Option 1</option>
<option value="1">Option 2</option>
<option value="2">Option 3</option>
</select>
Please notice that I set default selected value to 0
Cheers, hope this helps

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