html select list value not passing on to PHP - php

I have a form in which values get inserted and passed to PHP, everything was working till I thought about inserting a select value, and this one always gets passed on to the database as 0. This is the form:
<form name="myForm" id="register" action="register.php" method="post" accept-charset="utf-8">
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
This is how I retrieve all my variables, but this select one doesn't seem to work.
PHP:
$value7 = mysql_real_escape_string($_POST['Year']);
Thank you!

Change this
<option value="year1">1</option>
to this
<option value="1">year1</option>
PHP Code
<?php
if(isset($_POST))
echo mysql_real_escape_string($_POST['Year']);
?>
HTML code
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
<input type='submit' >
</form>

change this
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
to
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>

Related

can I $_POST multiple assoc. arrays

I am.working on a user input availability database. I order to standardize the information, I want to use more than 1 mulitiple select inputs.
<form method="post" action=''example.php">
Gamerid:<input ="text" name ="gamerid"/></br>
Monday:<select name="monday[]" multiple="multiple">
<option>Select your availability</option>
<option value="4">4pm</option>
<option value="5">5pm</option>
<option value="6">6pm</option>
<option value="7">7pm</option>
<option value="8">8pm</option>
<option value="9">9pm</option>
<option value="10">10pm</option>
<option value="11">11pm</option>
<option value="12">12pm</option>
<option value="1a">1am</option>
</select> </br>
Tuesday:<select name="tuesday[]"multiple="multiple">
<option>Select your availability</option>
<option value="4">4pm</option>
<option value="5">5pm</option>
<option value="6">6pm</option>
<option value="7">7pm</option>
<option value="8">8pm</option>
<option value="9">9pm</option>
<option value="10">10pm</option>
<option value="11">11pm</option>
<option value="12">12pm</option>
<option value="1a">1am</option>
</select> </br>
Wednesday:<select name="wednesday[]" multiple="multiple">
<option>Select your availability</option>
<option value="4">4pm</option>
<option value="5">5pm</option>
<option value="6">6pm</option>
<option value="7">7pm</option>
<option value="8">8pm</option>
<option value="9">9pm</option>
<option value="10">10pm</option>
<option value="11">11pm</option>
<option value="12">12pm</option>
<option value="1a">1am</option>
</select> </br>
etc for every day of the week.. so far, I believe my code is correct but POST seems to not appreciate it. Is it even possible..?
The end goal it to serialise the $_POST arrays, into strings, then store them in sql db.
Any help here would be appreciated.
In action page you will get array then by using implode you can convert to string then you can insert in to database.
try for this code.
<form method="post">
Tuesday:<select name="tuesday[]"multiple="multiple">
<option>Select your availability</option>
<option value="4">4pm</option>
<option value="5">5pm</option>
<option value="6">6pm</option>
<option value="7">7pm</option>
<option value="8">8pm</option>
<option value="9">9pm</option>
<option value="10">10pm</option>
<option value="11">11pm</option>
<option value="12">12pm</option>
<option value="1a">1am</option>
</select>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
$var=$_POST['tuesday'];
echo implode(",",$var);
}
?>

How do i get the select tag value with codeigniter?

how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');

php form several select with same name only the one selected

I have a form with several <select> that have the same name and an onChange="document.forms['form_mes_reservations'].submit();" on all of them. Therefore, when I select an option from one of them, the page reloads and there is some PHP treatment done.
This is an example :
<form method="post" id="form_process">
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="1_choose">Please choose...</option>
<option value="1_yes">Yes</option>
<option value="1_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="2_choose">Please choose...</option>
<option value="2_yes">Yes</option>
<option value="2_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="3_choose">Please choose...</option>
<option value="3_yes">Yes</option>
<option value="3_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="4_choose">Please choose...</option>
<option value="4_yes">Yes</option>
<option value="4_no"></option>
</select>
</form>
What I'd like to know is this : is there a way to know which select has triggered the onChange ? Let me explain : in the $_POST['processed'], I get ALL the selected options, so if I have only selected "3_yes", I'll get something like this :
1_choose
2_choose
3_yes
4_choose
Is it possible to only have "3_yes" as a result ? Potentially, this is going to be a very long list of selects and I don't want to check all of them for changes...
Just make all select elements as array and check which is selected:
HTML:
<form method="post" id="form_process" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="1_choose">Please choose...</option>
<option value="1_yes">Yes</option>
<option value="1_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="2_choose">Please choose...</option>
<option value="2_yes">Yes</option>
<option value="2_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="3_choose">Please choose...</option>
<option value="3_yes">Yes</option>
<option value="3_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="4_choose">Please choose...</option>
<option value="4_yes">Yes</option>
<option value="4_no"></option>
</select>
</form>
PHP:
<?php
foreach($_POST['processed'] AS $key => $value){
if(strpos($value, 'choose') !== false){
unset($_POST[processed][$key]);
}
}
print_r($_POST['processed']);
?>
Output:
Array
(
[1] => 2_yes
)

How to know what select is submitted in a form if i have two

I have a form with two select field. The select use onChange="form1.submit()" to submit form. And how I know what select field in the form is selected by user? I have tried to do this with two Submit Button and I can know what button have submitted easily.
<?php
if(isset($_POST["select"])) {
echo "submit";
}
if(isset($_POST["select2"])) {
echo "submit2";
}
?>
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="1">Select a</option>
<option value="1">Select b</option>
</select>
<select name="select2" id="select" onChange="this.form.submit()">
<option value="2">Select2 a</option>
<option value="2">Select2 b</option>
</select>
</form>
Both the <select> values are sent to the server. What you need to check is for their emptiness. So kindly change your code to:
<?php
if(!empty($_POST["select"])) {
echo "submit";
}
if(!empty($_POST["select2"])) {
echo "submit2";
}
?>
I do believe you should change the value of the select too, here:
Select a
Select b
Select2 a
Select2 b
Generally, to check what's happening / getting posted to the code, you can do it in two ways:
PHP Way
Use var_dump() to check what has been submitted. The best one would be:
var_dump($_REQUEST);
var_dump($_POST);
var_dump($_GET);
Network Tab
If you are using Chrome or Firebug, you can use the Developer Tools' Network tab to check the FORM Data.
Firebug
Chrome
Unrelated Note: You have <select> tags with same id, which is totally wrong, but that doesn't affect it. Checking using the PHP way, will work for you.
You can use a default value, such as an empty string:
<?php
if(!empty($_POST["select"])) {
echo "submit";
} elseif(!empty($_POST["select2"])) {
echo "submit2";
}
?>
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="">Choose</option>
<option value="1a">Select a</option>
<option value="1b">Select b</option>
</select>
<select name="select2" id="select2" onChange="this.form.submit()">
<option value="">Choose</option>
<option value="2a">Select2 a</option>
<option value="2b">Select2 b</option>
</select>
</form>
Try This:
Your HTML (Adding one more option with 0 value):
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="0">Select</option>
<option value="1">Select a</option>
<option value="1">Select b</option>
</select>
<select name="select2" id="select" onChange="this.form.submit()">
<option value="0">Select</option>
<option value="2">Select2 a</option>
<option value="2">Select2 b</option>
</select>
</form>
Your PHP Code:
if($_POST['select'] > 0){
echo "submit";
}
if($_POST['select2'] > 0){
echo "submit2";
}

how to handle multiple select's selection

This supposedly simple but for some reason i cant figure out the value of a multiple select.
Here is an example:
<form action="" method="post">
<select name="cars" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Now in the php side i am trying to echo or print_r($_POST['cars']);
However it doesnt print an array it only prints one value from the picked options.
Change the name of your <select> to an array:
<form action="" method="post">
<!-- change cars to cars[] -->
<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>

Categories