I have something like this in my view:
<form .....>
<select>
<option value="five">5</option>
<option value="ten">10</option>
<option value="fifteen">15</option>
</select>
</form>
As I do in the controller to take the values of the options?
PD: It is a manual select , do not use types that actually genre with jquery .
You need to give your select a name, so it will be submitted in the form.
<select name="whatever">
Then in PHP, it will be in
$_POST['whatever'];
Related
How can I fill a MySql query using php's $_POST from more than two dropdown lists?
Lets say I have the following three dropdowns in a HTML form:
<form method="post" action="myphpscript.php">
<select name="country">
<option value="1">Belgium</option>
<option value="2">Canada</option>
<option value="3">Spain</option>
</select>
<select name="year">
<option value="2001">First</option>
<option value="2002">Second</option>
<option value="2003">Third</option>
</select>
<select name="virus">
<option value="hiv">HIV</option>
<option value="hcv">HCV</option>
<option value="hbv">HBV</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
Then I have my query:
<?php
$query=sprintf("select country, year, virus from f_report where country='%s' and year ='%s' and virus ='%s'",
mysqli_escape_string($con,$_POST["country"]),
mysqli_escape_string($con,$_POST["year"]),
mysqli_escape_string($con,$_POST["virus"]));?>
How can I get this query to work if, for example, the user selected only "country"?
There are two ways you can go about this.
you can assign default values by verifying $_POST['key'] is empty for example
$_POST['year'] = isset($_POST['year']) ? $_POST['year'] : 'default value';
or
Set the optional fields to null in your database.
I hope it helps.
I'm using Codeigniter 3.
The countries select box is an optional field on a form:
<select name="countries">
<option>Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
I have the following line for the countries field validation:
$this->form_validation->set_rules('year', 'Year', 'trim|integer');
When I submit the form with the 1st option (Choose a Country), even without a value defined the validator sees something like this:
echo '<pre>';
print_r($this->input->post('year'));
echo '</pre>';
>> Choose a Country
.. when I am expecting something like false or null.
How can I make CI ignore the value when there isn't one selected?
Thanks in advance!
First of all, you paste the wrong line, I think it should be countries instead of year.
You can make it by these ways:
Give the default selection a value, that can be caught. And handle when the value equals to none
<select name="countries">
<option value="none">Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
Change your validation rule that only allows options in the list. If the value returns isn't in the list, it would make error.
$this->form_validation->set_rules('countries', 'Countries', 'required|trim|in_list[en|fr|de]');
I am using codeigiter's form_dropdown() and would like to make the selection alter data farther down in the same view without submitting the form.
In the view php file:
echo form_dropdown('department_select',$options,'1');
<?php $test_list = $this->trainingmodel->get_dept_tests($deptselected); ?>
I would like $deptselected to reflect what the user has selected in the form_dropdown(). I would like this to update every time the user changes their dropdown selection but without submitting the form.
Things like the following would work if the form was submitted, but not before:
$deptselected = $_POST['department_select'];
or
$deptselected = $this->input->post('department_select');
There must be a way to do what I want using javascript, or onchange or the 4th input parameter to form_dropdown().
You can use the chained select jquery plugin http://www.appelsiini.net/projects/chained
A simple example taken from the website:
<select id="mark" name="mark">
<option value="">--</option>
<option value="bmw" selected>BMW</option>
<option value="audi">Audi</option>
</select>
<select id="series" name="series">
<option value="--">--</option>
</select>
<select id="engine" name="engine">
<option value="--">--</option>
</select>
<select id="transmission" name="transmission">
<option value="--">--</option>
</select>
And on
$("#transmission").remoteChained({
parents : "#engine",
url : "/api/transmissions.json",
depends : "#series"
});
You can set the ajax url to your Codeigniter controller that will serve the data to the view in json format.
I have generated drop-down box using loop. Now I want to get all value using J-Query for pass these value for insertion through Ajax.
My HTML CODE is:
<select name="travel[]" id="travel[]" >
<option value="1">Car</option>
<select>
<select name="travel[]" id="travel[]" >
<option value="2">Train</option>
<select>
<select name="travel[]" id="travel[]" >
<option value="3">Bus</option>
<select>
After click on Save button, how to collect all values of travel?
I don't understand why you define three <select> elements with an index-based name, each containing a single <option>. You do know that a <select> element is exactly that - it gives the user a selection?
With your current markup, the user doesn't get a choice about their selection - rather, travel[] will always be an array containing: 1,2,3.
You'd be better to modify your markup as follows:
<select name="travel" id="select_travel">
<option value="1">Car</option>
<option value="2">Train</option>
<option value="3">Bus</option>
<select>
Now you can easily access the value using:
$('#select_travel').val();
You can do this:
var data = $('select[name="travel[]"]').map(function () {
return $(this).val();
}).get();
console.log(data); // ["1", "2", "3"]
This will return you an array with the value for all the dropdowns.
Fiddle demo
why you are making name and id both as array type... i think one will enough if you really want to use array type.... then make id as unique for all...
<select name="travel[]" id="abc">
</select>
<select name="travel[]" id="def">
</select>
<select name="travel[]" id="xyz">
</select>
now you can access dropdown list easily..
$("#abc").val();
$("#def").val();
$("#xyz").val();
or else you can define uniq class for them if you really want array type id and name
<select name="travel[]" id="travel[]" class="abc">
</select>
<select name="travel[]" id="travel[]" class="xyz">
</select>
and try like this...
$(".abc").val();
$(".xyz").val();
hope it may help you
you should try js like-
var selectOption=$('#select_travel').val()
var Id=selectOption.getAttribute('id');
var name=selectOption.getgetAttribute('id');
now you can take output on button click
i want to know that how to get multiple selectbox values in php
means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user
i think an array should be used but i dont know how??
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
like in above example
how do i get know that how many selectboxes were used and how to get value of each box
code for adding the new dropdown
function addRow()
{
var newRow = document.all("tblGrid").insertRow();
var oCell = newRow.insertCell();
oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.
So you can do it something like this:
<select name="myselect[]" id="select1">
<select name="myselect[]" id="select2">
<select name="myselect[]" id="select3">
After form post you can get all select box values:
print_r( $_POST['myselect'] );
For number of select boxes on page you can try:
echo count( $_POST['myselect'] );
Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.