get select box value Laravel - php

I use laravel 5.4.
<select name="type" id="type">
<option value="0">title1</option>
<option value="1" selected>title2</option>
<option value="2">title3</option>
</select>
I record the data to the database. But I can not get the value of the select box tag. I am using the following codes in Controller and as a result I get text.
$type = $request->get('type');
or
$type = Input::get('type');
print_r($type) ;
result: "title2"
I did not find any results in my research. Please help me.Is there a recommendation?

change $request->get('type') to $request->input('type')

the easiest way is $request->type to get value of select option in your controller ;)

Related

Select Random options from Select drop down

I want to be able to select randomly from the select tag using php,
i tried using rand(),it didnt seem to work
<select name="symbols" class="form-control" multiple="multiple">
<option value="BCH/BTC">BCH/BTC</option>
<option value="BCH/EUR">BCH/EUR</option>
<option value="BCH/GBP">BCH/GBP</option>
<option value="BTC-EOS">BTC/EOS</option>
<?php
$symbols = mysqli_real_escape_string($conn, $_POST['symbols']);
$rand001= shuffle($symbols);
?>
you have to use javascript for this,
because html has been created already before and php cannot change it:
in Jquery Syntax:
let options = $("option");
let toSelect = options[Math.floor(Math.random() * options.length)];
$(toSelect).attr("selected","selected");

How to assign HTML form multiple selected options to PHP variable?

I have HTML form with multiple select element to let the users select multiple options from the list for submission. The form method is POST and it posts the form to PHP file. I figured it out how to echo multiple selected options through out reading this How to get multiple selected values of select box in php? but I am unable to write those values to a variable as string, and having space between each option-value. Is it possible to achieve this without using array?
Here is my HTML code:
<select name="place[]" id="place" multiple>
<optgroup label="Ottawa">
<option value="London">London</option>
<option value="Toronto">Toronto</option>
<option value="Windsor">Windsor</option>
</optgroup>
<optgroup label="Alberta">
<option value="Brooks">Brooks</option>
<option value="Calgary">Calgary</option>
<option value="Cold Lake">Cold Lake</option>
</optgroup>
</select>
Here is the PHP code:
<?php
foreach ($_GET['place'] as $selectedOption)
echo $selectedOption."\n";
?>
The above code only echo the values but not assigning it to variable as string. Can I simply use $locations = instead of echo?
Note: There are many Questions and Answers that shows how to assign select option value to PHP variable but not for multiple option.
Since you said you were using a post request, $_GET['place'] won't work. So using $_POST['place'] instead, you can glue the values of the array together using implode():
$locations = implode(' ', $_POST['place']);

PHP Multiple Hidden Values Select Option

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

Form 'SELECT' element returning incorrect value

Hello I have a form select element as follows:
<select name="color" id="color">
<option value="1" label="Red">Red</option>
<option value="2" label="Green">Green</option>
<option value="3" label="Blue">Blue</option>
</select>
When I submit the form, and check the $_POST, I get:
array('color' => 'Red')
Where it should be:
array('color' => '1')
I am a little confused, would it have something to do with the label attribute?
---- Edit ----
dojo.addOnLoad(function() {
dojo.forEach(zendDijits, function(info) {
var n = dojo.byId(info.id);
if (null != n) {
dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
}
});
dojo.parser.parse();
});
var zendDijits = [{"id":"color","params":{"autocomplete":"true","required":"true","dojoType":"dijit.form.ComboBox"}},...
Your code appears to be valid, but the label may be interfering with something. Since you don't need it (you use the same text as the text between the option tags), I suggest you remove it.
Try it this way:
<select name="color" id="color">
<option value="1">Red</option>
<option value="2">Green</option>
<option value="3">Blue</option>
</select>
Some more info on the label attribute.
Definition and Usage
The label attribute specifies a shorter version of an option.
The shorter version will be displayed in the drop-down list.
Although the information was not provided in the question for anyone to answer, the solution to the problem was this:
The form element was being created in Zend Framework as a Zend_Dojo_Form_Element_ComboBox, and I found the following information in the documentation.:
ComboBoxes return the label values, and not the option values, which
can lead to a disconnect in expectations. For this reason, ComboBoxes
do not auto-register an InArray validator (though FilteringSelects
do).
Changed the element to a Zend_Dojo_Form_Element_FilteringSelect, and the problem was resolved, working fine now.
Thanks to #devdRew who asked the right question that tipped me off on the thought of dojo/dijit changing the value of what is posted.

Get data from SELECT

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.

Categories