Is that possible to get option text instead of value?
Somehow I need to use same option for 2 purpose. one of them need to get value which will be id of selected option and for another purpose i need to get text of that option.
here is how it looks like in HTML:
<select class="form-control" name="address_id">
<option value="1">Street 3.</option>
<option value="2">Street 4.</option>
</select>
I saw solutions base on Jquery and JavaScript but that's not gonna work for me I need PHP solution.
here is my saving method:
// codes....
$order->address = $request->input('address_id'); // basically will save value
//codes...
Auth::user()->orders()->save($order); //saving
Related
So I'm using a form with the POST method and I have no trouble getting the value="" of the option chosen by the user. Got my math working and everything, no problem. Then I'm going over everything and realized that I need to show the user their Dilution Ratio, for example 1:1, based on what they chose.
<select name="yourDilution" id="yourDilution">
<option value=""></option>
<option value="2">1:1</option>
<option value="3">1:2</option>
<option value="4">1:3</option>
<option value="5">1:4</option>
So how do I get that done?
Thank you for your time.
Oh and please no .js, that language and I aren't currently on speaking terms.
You have a couple options:
1 - Pass the value you need
You can simply pass '1:1' as the value and make your form handler based on this so that it is available for outputting later.
<option value="1:1">1:1</option>
2 - Use logical values
You could also simply pass the defining part of the ratio. If all the ratios are 1:x then pass the value of x and know that the resulting ratio will alway be 1:x
<option value="1">1:1</option>
Then in your form handler
$display = "1:".$_POST['yourDilution'];
3 - Use value like an ID
Re-define the value and display value relationship in your form handler with a switch statement
switch($_POST['yourDilution']){
case 2:
$display = "1:1";
break;
//etc ...
}
Personally, I like Option 2 since it's simple and allows you to pass the least amount of data and requires less work if the form values change. Hope this helps.
You can simply handle this with using a array example,
<?php
// Here you put your options value that you want to show the user
$array = ["1:1","1:2","1:3"];
// Here you get the selection value of the user
$postValue = 2; // in your case $_POST['yourDilution']
// here you show it to the user
print_r($array[$postValue]);
?>
My select box that is generated by PHP:
<select>
<option selected>Select something</option>
<option data-value="test"></option>
</select>
So far I have tried the following ways to retrieve the value within the data-value attribute:
$('select').children(":selected").data('value');
$('select option:selected').data('value')
$('select option:selected').attr('data-value');
$('select').find(':selected').data('value');
Of course there are a couple of more ways to do this. Once the one of these options gets executed I "sometimes" get the value within the data-value attribute. Even using
$('select').on('change', function () { // code });
gives me the same result. Am I missing some underlying thing?
FIXED:
After every select I use the data information for something else. Then I refresh the select box data by retrieving the remaining data information from the database. At that point I forgot to add the attribute data-value to every option..
Is there a way to mimic the C input/select box where you have a pull down with a blank text-entry at the top? Users would either type a new value or select from the list. Has anyone figured a way to do this with PHP/Javascript? An AJAX type of solution would even be better.
I'm not sure what to call this type of box, I don't think it is a "combo box" like most people think of.
You have a few options.
Here's a quick function I threw together in jQuery that will do what you want. This option required the client have JavaScript enabled to work.
http://jsfiddle.net/QrA4N/25/
If you don't want to make JavaScript a requirement
If you want a solution without JavaScript (client side code). You are stuck with placing an input box with the same "name" as your select box next to it or after it and adding an "Other" option to your select box.
<select name="selAnimals" id="selAnimals">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="bird">Bird</option>
<option value="guineapig">Guinea Pig</option>
<option value="">Other</option>
</select>
<input type="text" name="selAnimals_Other" id="selAnimals_Other" />
Now your PHP will have to check both $_POST["selAnimals"] and $_POST["selAnimals_Other"] to derive the correct value.
The best option is to combine the HTML above and the JavaScript above that to create a gracefully degrading solution for those with JavaScript enabled or disabled.
http://jsfiddle.net/QrA4N/26/
I added the extra HTML INPUT tags to the jsfiddle from the top of the answer and only changed 1 line of the jQuery function (makeInputSelect).
var $inp = $("#" + id + "_Other");
You can use famous Chosen library for that :)
BTW, see the second example of countries.
I have two <select> one is category and the second is subcategory.
here is the first <select> for category.
<select name="category" size="10">
<?php foreach($categories->fetch(array('table' => 'categories')) as $category) { ?>
<option value="<?php echo $category['id']; ?>"><?php echo $category['name']; ?></option>
<?php } ?>
</select>
now the second <select> i.e subcategory should be hidden initially and when a user click on category <select> based on the value it should populate the value in subcategory.
One way of doing this is via AJAX by passing categoryId as POST Request and getting HTML as response.
however i would like to know if there is any other alternative so that it automatically pass the categoryId value to PHP and unhide the second <select> here is the code of second <select>
<select name="subcategory" size="10">
<?php foreach($categories->fetch(array('table' => 'subCategories', 'categoryId' => $categoryId)) as $subCategory) { ?>
<option value="1"><?php echo $subCategory['name']; ?></option>
<?php } ?>
</select>
the only thing i need here is $categoryId to be populated dynamically. is there any way of doing this?
thank you..
No, there is no way to do what you are suggesting. PHP is only run on the server, so by the time the page is rendered on the client the PHP has already been run.
Your best bet would be what you already suggested, running some AJAX after the first select is changed, sending back the category ID to the server and retrieving what you need to build the second select.
Is there a reason why you don't want to do it this way?
Sukumar has probably suggested the best and most intuitive solution to make it appear as if the data is being loaded dynamically to the user.
The other alternative would be to submit the form when the select box is changed. Once the form has been submitted PHP would pick up the ID from the POST array and then re-populate the sub-category select box. This is often used as a fallback in case the user doesn't have JavaScript enabled.
Structurally, there are three choices to solve this problem:
Use an ajax call to fetch the required data when a user selection is made as jbruno has described.
Submit the whole page to the server, let your PHP see the newly selected option and fill in the newly desired data in a returned page. This will cause the page to refresh so is less ideal than option 1.
Pre-populate the page with all possible data in a javascript data structure so you can use Javascript to just look up the desired category ID in a local data structure, modify the page and never have to talk to the server in order to update the page.
In my opinion, option 3) is the most desirable if the data set required for local lookup is not too large (say under 100k) and it's not too expensive on the server to collect all that data for inclusion in the original page and if the data doesn't change real-time or having data as of the page load time is OK.
If option 3) isn't feasible for any reason, then option 1) is next best. Option 2) is not as good a user experience so it should only be the last resort if you really can't implement options 1) or 3).
You asked more specifically about option 3. I don't really yet understand what the whole data you need looks like. If you really only have four total data types residential_plot, residential_apartment, office_space and showroom, then you can just make those be four keys on an object and store their data that way:
var data = {
"residential_plot": 1,
"residential_apartment": 2,
"office_space": 3,
"showroom": 4
};
The 1, 2, 3 and 4 are just whatever data you want to store for that type. It can be numbers, strings, arrays of data, other objects of data, anything.
To access this, you would do like this:
var id = data.residential_plot;
or
var index = "residential_plot";
var id = data[index];
If you wanted to store the notion of categories and sub-categories, you would need an extra level of objects:
var data = {
"residential": {"residential_plot": 1, "residential_apartment": 2},
"commercial": {"office_space": 3, "showroom": 4}
};
Then, you would access it like this:
var id = data.residential.residential_plot;
or like this:
var category = "residential";
var catType = "residential_plot";
var id = data[category][catType];
I have a select list like such:
<select name="taxonomy[1][]" multiple="multiple" class="form-select" id="edit-taxonomy-1" size="7">
<option value="13">Glaciers</option>
<option value="14">Lake Ice</option>
<option value="17">Permafrost</option>
<option value="16">River Ice</option>
<option value="15">Sea Ice</option>
<option value="12">Snow</option>
</select>
This list is being dynamically created and I'm not sure how to change the output display. I have seen tutorials like this: http://www.netvivs.com/convert-regular-select-into-a-drop-down-checkbox-list-using-jquery/ which convert a select into a dropdown check list. Is there a way using php,javascript,jquery, to convert the select to display as a checklist? Preferably in table form so I get 2 rows with 3 columns?
You could do it in a custom way like so:
$('#edit-taxonomy-1').parent().append('<table id="checkboxes"><tbody><tr></tr></tbody></table>');
$('#edit-taxonomy-1 option').each(function() {
var label = $(this).html();
var value = $(this).attr('value');
$('#checkboxes tbody tr').append('<td><input type="checkbox" value="'+value+'">'+label+'</td>');
});
Or just use the plugin: http://code.google.com/p/dropdown-check-list/
Looks like the example link does it using jquery...
Just make sure you have a source to jquery.js library, as well as a source to the library that site has offered as a download.
Then just call,
$("#edit-taxonomy-1").dropdownchecklist();
I havent tried it, but thats what the tutorial says to do...
Good luck.
PS - I had a hard time trying to find the download of the actual library from that site, so here it is. http://code.google.com/p/dropdown-check-list/