Update Table if value not exists - php

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

Related

HTML select Tag Title using Codeigniter 3

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]');

how to insert value from select list into mysql database

<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.

populate form based on multiple select elements

I want a form to have multiple drop down selects that change based on the previous selection. This is how I want it to work.
<select id="format">
<option selected="selected" value="NULL">-- Select a product Format --</option>
<option value="PS3">PS3</option>
<option value="Xbox 360">Xbox 360</option>
<option value="Wii U">Wii U</option>
</select>
So if you select PS3 you are then faced with more options for PS3 product type
<select id="ps3-product-type">
<option selected="selected" value="NULL">-- Select a product PS3 product type --</option>
<option value="chargers">chargers</option>
<option value="controllers">controllers</option>
<option value="headsets">headsets</option>
</select>
So then you select headsets for example and you get another set of final options of products
<select id="ps3-headsets">
<option selected="selected" value="NULL">-- Select a PS3 headset --</option>
<option value="product-1">product 1</option>
<option value="product 2">product 2</option>
<option value="product 3">product 3</option>
</select>
How can I do this with jquery or PHP? Please bare in mind that I will also have selects for Xbox 360 and Wii U too.
UPDATE PROGRESS:
http://jsfiddle.net/maximus83/r7MN9/639/
Here is my HTML, I have got the first set of arrays working for selecting product type, But I cant get the 3rd box working, I need 3rd box to say the product, where do I go from here.
<select id="cat">
<option val="PS3">PS3</option>
<option val="Xbox360">Xbox360</option>
<option val="WiiU">WiiU</option>
<option val="Multiformat">Multiformat</option>
</select>
<select id="item">
</select>
<select id="product">
</select>
Here is my script
PS3=new Array('Headsets(PS3)','Controllers(PS3)','Chargers(PS3)','Cables(PS3)');
Xbox360=new Array('Headsets(360)','Chargers(360)');
WiiU=new Array('Controllers(WiiU)','Headsets(WiiU)');
Multiformat=new Array('Headsets(Multi)','Chairs(Multi)','Cables(Multi)');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
eval(cat).forEach(function(t) {
$('#item').append('<option val="#item">'+t+'</option>');
});
}
This should do the trick for you. It's a previously answered thread on the exact same topic - how to populate a drop down based on the value in another. It takes advantage jQuery (And I've used it before myself as reference).

Showing the previously selected value in a multi-select option

Thanks for reading.
I am using the following multiselect option for a users 'interests'. They can pick one or many. The interests are then stored in the database as a string, which is created from the array that the code below supplies. The thing is, when the user returns to modify his/her interests I want the box to display whats already in the database. The value for this is $user->interests (which is a string). I can't see how I can perform this. You can see I added $user->interest below to the value of the . Obviously this doesn't work. Any ideas...?
<select name="interests[]" value="<?php echo $user->interests;?>" style="width:390px;" data-placeholder="Select your specialist interests" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<option value="Acute critical care">Acute critical care</option>
<option value="Allergic diseases">Allergic diseases</option>
<option value="Asthma">Asthma</option>
<option value="Bronchiectasis">Bronchiectasis</option>
<option value="Cardiology">Cardiology</option>
</select>
The select element doesn't have a value attribute.
You need to add a selected attribute to each of the <option>s that should be selected by default.
You should get the list of values in an array and then check to see if each option is in that array as you output them.
Try this variant.
<select name="interests[]" style="width:390px;" data-placeholder="Select your specialist interests" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<option value="Acute critical care"
<?php if(strpos( $user->interests,"Acute critical care")){echo 'selected="selected"'} ?> >
Acute critical care
</option>
//Similarly for other options.
//....
</select>

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>';
}

Categories