php form several select with same name only the one selected - php

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
)

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 to check if a dropdown list, multiple=multiple, is selected in php?

I have a multiple=multiple drop down list and I want to check if a user has selected any option or not using PHP code. If not, then I want to display an error message. How can I do that?
<select name="ddlskill" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
You should declare your html like this :
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
And then in PHP, you'll get an array of values :
if( !empty($_POST['ddlskill']) ){
print_r($_POST['ddlskill']);
// [PHP, JSP, Java, ...]
}
Maybe helpful,
<?php
if( isset($_POST) && !empty($_POST['ddlskill'])){
echo "<pre>";print_r($_POST['ddlskill']);
// [PHP, JSP, Java, ...]
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
<button type="submit"> Submit </button>
</form>
<?php
if(!empty($_POST['submit'])){
if(is_array($_POST['ddlskill']))
{
foreach($_POST['ddlskill'] as $key=>$value)
{
print_r($value);
}
}
else
{
print_r($_POST['ddlskill']);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
<button type="submit" name="submit"> Submit </button>
</form>

I have several dropdown list - I want to print out selected options of these dropdown lists

This is what I got so far, it's just the dropdown list. It's a form for making a reservation for appointment. I want to print out these selected values (= selected options of dropdown)
But I have no clue since I'm very new with PHP and HTML. Something with $_POST and $_GET ? Could I have some suggestions of solution? Much gratitude.
<html>
<head>
<title>title</title>
</head>
<body>
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</body>
</html>
You're missing the <form> tag. It should be wrapped around all your form-data (select, input) etc. Then you will fetch it using either $_GET or $_POST or even $_REQUIRE
Example (change site.php to whatever you please):
<html>
<head>
<title>title</title>
</head>
<body>
<form method="post" action="site.php">
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</form>
</body>
</html>
PHP example:
if(isset($_POST['Ar'])) { // Make sure it's sent
$year = $_POST['Ar'];
echo "År: " . htmlentities($year); // Output (htmlentities for safety when outputting)
$month = $_POST['Manad'];
echo "Månad: " . htmlentities($month);
}

Passing checkbox values to php - missing values

I am having an issue where we cannot join up our checkboxes with the other fields being passsed.
If we only check the second checkbox it will take the value of the first row select box.
The rows are joined up by the date e.g.2014-06-02, 2014-06-03
HTML
<input type="checkbox" name="date_choice[]" value="2014-06-02">
<select id="event_start" name="event_start[2014-06-02][]" onchange="area_choice_click()">
<option value="">Start Time</option>
<option value="01:00">1.00AM</option>
<option value="02:00">2.00AM</option>
<option value="03:00">3.00AM</option>
</select>
<select id="event_end" name="event_end[2014-06-02][]" onchange="area_choice_click()">
<option value="">End Time</option>
<option value="01:00">1.00AM</option>
<option value="02:00">2.00AM</option>
<option value="03:00">3.00AM</option>
</select>
<input type="checkbox" name="date_choice[]" value="2014-06-03">
<select id="event_start" name="event_start[2014-06-03][]" onchange="area_choice_click()">
<option value="">Start Time</option>
<option value="01:00">1.00AM</option>
<option value="02:00">2.00AM</option>
<option value="03:00">3.00AM</option>
</select>
<select id="event_end" name="event_end[2014-06-03][]" onchange="area_choice_click()">
<option value="">End Time</option>
<option value="01:00">1.00AM</option>
<option value="02:00">2.00AM</option>
<option value="03:00">3.00AM</option>
</select>
PHP
foreach($_POST['date_choice'] as $key=>$team_id){
$event_date = $_POST['date_choice'][$key];
echo"$event_date";
$event_start=$_POST['event_start'][$event_date][$key];
}

Re load dropdown menu on page load

I have an html form that I have been using to POST to a php form, this saves the posted data into the database. I am now loading the data back into the form when I click edit, this works fine for the text fields as I can just use
value="<?php echo $somefield ?>"
The problem is im not sure how to re select my dropdown menu. The menu looks like this and is used for selecting a category, but when the page reloads nothing happens and the dropdown isn't reloaded.
<select name="category" id="category">
<option>- Select A Category -</option>
<optgroup label="Tractors">
<option value="Tractor">Tractor</option>
<option value="Compact">Compact</option>
<option value="Forklift">Forklift</option>
<option value="Load All">Load All</option>
<option value="Loader">Loader</option>
<option value="Maniscopic">Maniscopic</option>
<option value="Masted">Masted</option>
<option value="Piviot">Piviot</option>
<option value="Telescopic">Telescopic</option>
<option value="UTV">UTV</option>
</optgroup>
<optgroup label="Machinery">
<option value="Attachments">Attachments</option>
<option value="Bale Handling">Bale Handling</option>
<option value="Cultivation Equipment">Cultivation Equipment</option>
<option value="Drills">Drills</option>
<option value="Feeders">Feeders</option>
<option value="Fertlizer Spreaders">Fertlizer Spreaders</option>
<option value="Grass Harrows">Grass Harrows</option>
<option value="Grassland Equipment">Grassland Equipment</option>
<option value="Hedge Cutters">Hedge Cutters</option>
<option value="Mowers">Mowers</option>
<option value="Muck Spreaders ">Muck Spreaders </option>
<option value="Ploughs">Ploughs</option>
<option value="Post Hammers">Post Hammers</option>
<option value="Power Harrows">Power Harrows</option>
<option value="Rakes">Rakes</option>
<option value="Rolls">Rolls</option>
<option value="Rotovators">Rotovators</option>
<option value="Shredders">Shredders</option>
<option value="Splitters">Splitters</option>
<option value="Sprayers">Sprayers</option>
<option value="Spreaders">Spreaders</option>
<option value="Tankers">Tankers</option>
<option value="Tedders">Tedders</option>
<option value="Toppers">Toppers</option>
<option value="Trailers">Trailers</option>
<option value="Wrappers">Wrappers</option>
</optgroup>
<optgroup label="Forklifts">
</optgroup>
</select>
<?php
echo '<select>';
foreach( $names as $name )
echo '<option value="'.$name.'" '.($name==$selected?' selected="selected"':'').'>'.$name.'</option>'
echo '</select>';
?>
When building the list the selected option should have the attribute selected="selected".

Categories