how to insert value from select list into mysql database - php

<select id='selectCat' name='selectCat'>
<option disabled selected value='0'>Select Type</option>
<option value='book'>Book</option>
<option value='txtbook'>Text Books</option>
<option value='notes'>Notes</option>
</select>
Above is the code in html.
I want the user to select one option from the select list and want to store that value into my database. But before storing it into database i am just retrieving the selected value as shown below.
$userCaT = mysqli_real_escape_string($conn,$_POST['selectCat']);
echo $userCat;
But it is showing an empty page.
So plz get me out from this problem.
Thanks.

Related

Update Table if value not exists

I have two select box which values are
<select>
<option value="">I</option>
<option value="">II</option>
<option value="">III</option>
</select>
<select>
<option value="">I</option>
<option value="">II</option>
<option value="">III</option>
<option value="">IV</option>
<option value="">V</option>
</select>
My Scenario is when i select a class from first select box then after i select class from second select box.It will update the student Class "If value not exist in table." otherwise i will show error like this "Class already exist".
For Example
If i want to update student from "I" to "II" then value of 2nd select box should not in table.. please somebody help me.
There is one problem your not setting value and its empty. By which the values you select wont be added or updated to your database.
The second is you have not given the name to your select dropdown by which even if you select the values it wont be posted to your action.
<form action="update_student.php" method="post">
<select name="from_class">
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
</select>
<select name="to_class">
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
<option value="IV">IV</option>
<option value="V">V</option>
</select>
<input type="submit" value="Upgrade">
</form>
Suggestion:
And even you need to validate that the student must be promoted to the next class and not the class which he is already completed.
Eg: If your upgrading the student from II to III which is totally fine but you must not allow II to I.
In case if the user has asked for this kind of workflow then atleast you must prompt with confirmation for degrading the class from II to I

Get all selected values from bootstrap multiselect using PHP?

Having an off-day today and can't seem to figure this one out for some reason (feeling like a blonde moment).
I am trying to use the bootstrap (3.3.7) multiselect field
EX:
<select multiple class="form-control" id="select_example">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
</select>
When I submit (submits via ajax), the MySQL db receives all the data, but for multiselect fields, it is only the value of the last selected option in the options list.
How would I go about getting all of the selected option values and putting them into an array or comma separated string?
Selected: 2,4,5
Current value to db: five
Expected: two,four,five (or something of the like)
Is there a PHP solution for this, or jquery (or combo of the two?)
if($_POST['aircraft']){
$_POST['aircraft'] = implode(', ', $_POST['aircraft']);
}
with a select multiselect with the name aircraft the above code converts the returned array of the multiselect, and converts it into a comma separated string.
<select multiple class="form-control" id="select_example" name="options[]">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
$options = implode(',', $_POST['options']);

How to get the select option value in Laravel

I have a select option just like below.
<select name = "type">
<option value="">Select one</option>
<option value="1">Abc</option>
<option value="2">Xyz</option>
</select>
I tried to get the value using Input::get('type')But getting the labels Abc. I need to get the value as 1 or 2 or 3 depending on the one that I'm selecting. I even tried this
<select name = "type" onchange="document.getElementById('typeId').value=this.options[this.selectedIndex].value;">
But same thing happened. How can I do this?
Just
document.getElementById('typeId').value=this.value;

set select <option> as selected in form

I have a form wich inserts some data in a mysql database.
This form contain a select and some options with their respective values like
<select name="car_type">
<option value="sport">Sports car</option>
<option value="van">Van</option>
<option value="large">Large family sedan</option>
<option value="small">Small city car</option>
</select>
The form can also be used to update a car's details in the database, it does so by loading the values from the database and fills the form automatically but I am stuck at making the <option> in the select, selected by default based on the value already set in the DB.
So if the user chooses to edit a car, lets say a car that already has Sports type filled in the DB, I want the form to automatically set the <option value="sport">Sports car</option> as selected, <option selected="selected" value="sport">Sports car</option>. By not doing this, the user has to choose again the type every time he submits the form, otherwise the first <option> and its value (sport) is sent by POST.
I am able to retrieve the value from the database by using $data['type'] but I did not find the exact php code to set the selected <option> to that in the database, can you guys help ?
Although the code looks messy, you can do something like this:
<select name="car_type">
<option value="sport" <?php if($data['type']=='sport') echo "selected='selected'"; ?> >Sports car</option>
<option value="van" <?php if($data['type']=='van') echo "selected='selected'"; ?>>Van</option>
<option value="large" <?php if($data['type']=='large') echo "selected='selected'"; ?>>Large family sedan</option>
<option value="small" <?php if($data['type']=='small') echo "selected='selected'"; ?>>Small city car</option>
</select>
<option value="large" <?php echo ($dbvalue=="large") ? "selected=\"selected\"" : "" ;?>>Large family sedan</option>
Work out the sql query bit yourself and replace my $dbvalue with the column data from your db
If you're trying to mark the active car type I would do it like this:
foreach ($car_types as $car_type){
echo '<option value="'.$car_type.'" '.($data['type']==$car_type?'selected':'').'>'.$car_type.'</option>';
}

how to get values from multiple select/listboxes in php?

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.

Categories