Trouble in getting multiple selected dropdown box values in php - php

My html code is like below
<select multiple="multiple" size="2" name="exServer[]">
<option value="1"> host.newcybage.alabanza.com </option>
<option value="2"> host2.newcybage.alabanza.com </option>
<option value="3"> host3.newcybage.alabanza.com </option>
<option value="5"> host3.devel.php5.qa.alabanza.com </option>
<option value="7"> centos5host2.alabanza.com </option>
<option value="8"> centos5host.template.alabanza.com </option>
</select>
<input type="submit" value="Apply" name="exSubmit" class="button">
And my php code is like below
$arr=$_POST['exServer'];
print_r($_POST['exServer']);
print_r($arr);
Both print statements are giving me no results.
I dont know where i am wrong in getting selected multiple dropdown values.
I verified using HttpFox that data is getting posted properly on server side.
When i am printing value of exSubmit, it is giving me proper values.
print "Exsubmit:".$_POST['exSubmit']; //Result Exsubmit:Apply
Please help.

you code is working. I tried this and got all the selected values in PHP
<?php
$arr=$_POST['exServer'];
print_r($_POST['exServer']);
print_r($arr);
?>
<form method="post">
<select multiple="multiple" size="2" name="exServer[]">
<option value="1"> host.newcybage.alabanza.com </option>
<option value="2"> host2.newcybage.alabanza.com </option>
<option value="3"> host3.newcybage.alabanza.com </option>
<option value="5"> host3.devel.php5.qa.alabanza.com </option>
<option value="7"> centos5host2.alabanza.com </option>
<option value="8"> centos5host.template.alabanza.com </option>
</select>
<input type="submit" value="Apply" name="exSubmit" class="button">
</form>

This should work
<select multiple="multiple" size="2" name="exServer[]">
<option value="1"> host.newcybage.alabanza.com </option>
<option value="2"> host2.newcybage.alabanza.com </option>
<option value="3"> host3.newcybage.alabanza.com </option>
<option value="5"> host3.devel.php5.qa.alabanza.com </option>
<option value="7"> centos5host2.alabanza.com </option>
<option value="8"> centos5host.template.alabanza.com </option>
</select>
My suggestion is to print the array $_POST and see the values passed in the backend.
<?php
print_r($_POST);
?>

Related

Strange form submit bug?

HTML:
<div class="select is-pulled-right" style="margin-top:-8px;"> <select id="pin_selection_1" name="pin_selection_1" class="is-pulled-right"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> <option value="3"> 3 </option> <option value="4"> 4 </option> <option value="5"> 5 </option> <option value="6"> 6 </option> <option value="7"> 7 </option> <option value="8"> 8 </option> <option value="9"> 9 </option> </select> </div><div class="select is-pulled-right" style="margin-top:-8px;"> <select id="pin_selection_2" name="pin_selection_2" class="is-pulled-right"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> <option value="3"> 3 </option> <option value="4"> 4 </option> <option value="5"> 5 </option> <option value="6"> 6 </option> <option value="7"> 7 </option> <option value="8"> 8 </option> <option value="9"> 9 </option> </select> </div><div class="select is-pulled-right" style="margin-top:-8px;"> <select id="pin_selection_3" name="pin_selection_3" class="is-pulled-right"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> <option value="3"> 3 </option> <option value="4"> 4 </option> <option value="5"> 5 </option> <option value="6"> 6 </option> <option value="7"> 7 </option> <option value="8"> 8 </option> <option value="9"> 9 </option> </select> </div><div class="select is-pulled-right" style="margin-top:-8px;"> <select id="pin_selection_4" name="pin_selection_4" class="is-pulled-right"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> <option value="3"> 3 </option> <option value="4"> 4 </option> <option value="5"> 5 </option> <option value="6"> 6 </option> <option value="7"> 7 </option> <option value="8"> 8 </option> <option value="9"> 9 </option> </select> </div><div class="select is-pulled-right" style="margin-top:-8px;"> <select id="pin_selection_5" name="pin_selection_5" class="is-pulled-right"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> <option value="3"> 3 </option> <option value="4"> 4 </option> <option value="5"> 5 </option> <option value="6"> 6 </option> <option value="7"> 7 </option> <option value="8"> 8 </option> <option value="9"> 9 </option> </select> </div><div class="select is-pulled-right" style="margin-top:-8px;"> <select id="pin_selection_6" name="pin_selection_6" class="is-pulled-right"> <option value="0"> 0 </option> <option value="1"> 1 </option> <option value="2"> 2 </option> <option value="3"> 3 </option> <option value="4"> 4 </option> <option value="5"> 5 </option> <option value="6"> 6 </option> <option value="7"> 7 </option> <option value="8"> 8 </option> <option value="9"> 9 </option> </select> </div>
PHP:
if ($request->input('pin_severity') != 'disabled') {
$user->pin_code =
$request->input('pin_selection_6') .
$request->input('pin_selection_5') .
$request->input('pin_selection_4') .
$request->input('pin_selection_3') .
$request->input('pin_selection_2') .
$request->input('pin_selection_1');
}
$user->save();
echo 'pin_selection_1: ' . $request->input('pin_selection_6') . '<br>';
echo 'pin_selection_2: ' . $request->input('pin_selection_5') . '<br>';
echo 'pin_selection_3: ' . $request->input('pin_selection_4') . '<br>';
echo 'pin_selection_4: ' . $request->input('pin_selection_3') . '<br>';
echo 'pin_selection_5: ' . $request->input('pin_selection_2') . '<br>';
echo 'pin_selection_6: ' . $request->input('pin_selection_1') . '<br>';
exit();
I use 6 different select dropdowns to pick a 6 digit pin code, when saving it using the PHP code I am receiving some issues. My database column is an int with length 6 NOT NULL.
When saving the pin as something like 123456 or 223344 it works perfectly and saves correctly. Earlier I tried doing something more complicated, 020304
It went through, but saved in the database as 20304, the strange thing is, my echo displayed it exactly as I entered it, but it saved in the database incorrectly?
Your echos are displayed correctly because they act as strings.
But when you save the pin in your database, Laravel/mysql converts the string to an integer, hence, the mismatch.
If you want it to be inserted as is, change the datatype of your pin_code column to varchar

HTML form select Output hide

I have a form where users have dropdowns.. The form have a get method so that the output values is used for processing.. But the problem is i get all the three values which is not able to be used..
<form method="get" action="finish.php">
<select name="case" >
<option value="">*Grimsborough Case*</option>
<option value="1" >001</option>
<option value="2" >002</option>
<option value="3" >003</option>
</select>
<select name="case" >
<option value="">*Pacific Bay Case*</option>
<option value="201" >PB01</option>
<option value="202" >PB02</option>
<option value="203" >PB03</option>
</select>
<select name="case" >
<option value="">*World Edition Case*</option>
<option value="301" >WE01</option>
<option value="302" >WE02</option>
<option value="303" >WE03</option>
</select>
These are the forms that I have the output while i select is something like this..
finish.php?case=&case=&case=302&submit=%3D%3D>+submit+<%3D%3D
I will like to get output like this
finish.php?case=302&submit=%3D%3D>+submit+<%3D%3D
Please help :)
You need to name your select elements differently
<form method="get" action="finish.php">
<select name="case1" >
<option value="">*Grimsborough Case*</option>
<option value="1" >001</option>
<option value="2" >002</option>
<option value="3" >003</option>
</select>
<select name="case2" >
<option value="">*Pacific Bay Case*</option>
<option value="201" >PB01</option>
<option value="202" >PB02</option>
<option value="203" >PB03</option>
</select>
<select name="case3" >
<option value="">*World Edition Case*</option>
<option value="301" >WE01</option>
<option value="302" >WE02</option>
<option value="303" >WE03</option>
</select>
And from your php script, you to do this:
$case1 = $_GET["case1"];
$case2 = $_GET["case2"];
$case3 = $_GET["case3"];
If i understood it right you want something like that:
<select name="case" >
<optgroup label="Grimsborough Case">
<option value="1" >001</option>
<option value="2" >002</option>
<option value="3" >003</option>
</optgroup>
<optgroup label="Pacific Bay Case">
<option value="201" >PB01</option>
<option value="202" >PB02</option>
<option value="203" >PB03</option>
</optgroup>
<optgroup label="World Edition Case">
<option value="301" >WE01</option>
<option value="302" >WE02</option>
<option value="303" >WE03</option>
</optgroup>
</select>

How to limit same answer in form

I have a form with dropdown choice
<select id="ubytovanie" name="ubytovanie">
<option value=""> </option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
<option value="5"> 5 </option>
<option value="6"> 6 </option>
<option value="7"> 7 </option>
<option value="8"> 8 </option>
<option value="9"> 9 </option>
<option value="10"> 10 </option>
<option value="11"> 11 </option>
<option value="12"> 12 </option>
</select>
In my form you can choose room number in which you would like to stay. But there can be only 6 people in one room, so I need to limit the option so max 6 people can choose same room number. After that, the option with room number would be disabled for other people filling out the form.
Is there a way to do this, ideally with PHP?
Thanks for answer

PHP form submit to sql database

I'm currently creating a custom 'survey'-like form but since I'm new to php, I am not entirely sure how to submit the entries to a database (and make a fieldset required).
I currently have 3 fieldsets, lets call them who, when and what. Who is a simple select field so no problem. When however is a group of radios but two of those have text fields associated that need to be filled if the radio was selected. What is just a textarea.
I understand this much:
Form action is a php file (which contains database connection and
submission)
Form method is post
With the database, how do I submit this data? Especially the second fieldset.
Current Form:
<form action="heroes.php" method="post">
<fieldset>
<h2 class="req">Which Hero?</h2>
<span class="help">We need to know what hero says what line.</span>
<label><span class="title">Hero: </span>
<select>
<?php include 'files/hero-select.php'; ?>
</select>
</label>
</fieldset>
<fieldset id="when">
<h2 class="req">When?</h2>
<span class="help">When is it said? Who is it said to?</span>
<label>To Boss: <input name="when" type="radio" value="boss" /> <input type="text" placeholder="Boss Name" id="bname" size="10" /></label>
<label>To Hero: <input name="when" type="radio" value="hero" /> <input type="text" placeholder="Hero Name" id="hname" size="10" /></label>
<label>Low Health: <input name="when" type="radio" value="lowhp" /></label>
<label>Defeat: <input name="when" type="radio" value="defeat" /></label>
<label>Boss Defeat: <input name="when" type="radio" value="bossd" /></label>
<label>Mob Defeat: <input name="when" type="radio" value="mob" /></label>
<label>Emote: <input name="when" type="radio" value="emote" /></label>
<label>Item Pickup: <input name="when" type="radio" value="item" /></label>
</fieldset>
<fieldset>
<h2 class="req">Quote</h2>
<span class="help">What was said.</span>
<textarea id="quote"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" /><input type="reset" value="Clear" />
</fieldset>
</form>
hero-select.php:
Black Panther
<option value="Black Widow">
Black Widow
</option>
<option value="Cable">
Cable
</option>
<option value="Captain America">
Captain America
</option>
<option value="Colossus">
Colossus
</option>
<option value="Cyclops">
Cyclops
</option>
<option value="Daredevil">
Daredevil
</option>
<option value="Deadpool">
Deadpool
</option>
<option value="Emma Frost">
Emma Frost
</option>
<option value="Gambit">
Gambit
</option>
<option value="Ghost Rider">
Ghost Rider
</option>
<option value="Hawkeye">
Hawkeye
</option>
<option value="Hulk">
Hulk
</option>
<option value="Human Torch">
Human Torch
</option>
<option value="Iron Man">
Iron Man
</option>
<option value="Jean Grey">
Jean Grey
</option>
<option value="Loki">
Loki
</option>
<option value="Luke Cage">
Luke Cage
</option>
<option value="Moon Knight">
Moon Knight
</option>
<option value="Ms Marvel">
Ms Marvel
</option>
<option value="Nightcrawler">
Nightcrawler
</option>
<option value="Punisher">
Punisher
</option>
<option value="Rocket Raccoon">
Rocket Raccoon
</option>
<option value="Scarlet Witch">
Scarlet Witch
</option>
<option value="Spider-Man">
Spider-Man
</option>
<option value="Squirrel Girl">
Squirrel Girl
</option>
<option value="Storm">
Storm
</option>
<option value="Thing">
Thing
</option>
<option value="Thor">
Thor
</option>
<option value="Wolverine">
Wolverine
</option>
I'm gonna do a short example, with different code (but it works the same).
<form method="POST" action="add.php">
Name: <input type="text" name="name">
Phone: <input type="text" name="phone">
</form>
method="POST" means that it Posts all the info to the action, the action is set to add.php so it posts all the info to add.php.
As you can see every input above has a different name="", what the name property does is:
It sends every value together with a name, this means that the phone input gets send as Phone = "123456" and the name value Name = "2324234".
The add.php code
<?php
if ($_SERVER['REQUEST_METHOD']) == 'POST') {
$name = $_POST['name'];
$phone = $_POST['phone'];
$sql = 'INSERT INTO user '.
'(name,phone) '.
'VALUES ( "$name", "$phone"NOW())';
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
}
First it check if the request is a post, if it is it turns the value's it get send into variables and then it inserts the values into the database.
You can use jQuery with the .post method:
https://api.jquery.com/jQuery.post/
$.post("submittodatabase.php", $( "#yourformid" ).serialize() );

dynamically adding sets of form elements n times

i'm working on an application that lets users enter the opening hours for a restaurant. i have the form code set up like this:
<div class="hourswrapper">
Days:
<input type="checkbox" name="day1[]" value="1" />M
<input type="checkbox" name="day1[]" value="2" />Tu
<input type="checkbox" name="day1[]" value="3" />W
<input type="checkbox" name="day1[]" value="4" />Th
<input type="checkbox" name="day1[]" value="5" />F
<input type="checkbox" name="day1[]" value="6" />Sa
<input type="checkbox" name="day1[]" value="7" />Su
<br />
Opening Time:
<select name="starthour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="startminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="startwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
Closing Time:
<select name="endhour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="endminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="endwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
<input type="checkbox" name="24hours[]" value="yes" />Open 24 Hours
</div>
so basically the person chooses a set of hours and marks which days those apply to. of course there might be multiple sets of hours (ex. if a restaurant is open 12-2 one day but 12-5 another). i'd like to have a "Add More Hours" button that duplicates this same code when pressed, however many times is necessary.
what's the best way to go about this? how do you dynamically add a form element without needing to have it hidden at the beginning? how do i set different numbered names for each set of hours inputs, and later know how many to access (using PHP post)? i assume i'll need a javascript function that keeps track of how many have been added.
alternatively, i am open to suggestions on how to present hours inputs differently/better, perhaps a way that doesn't require dynamic creation of form elements. i do want to restrict the user's inputs so i don't have to worry about parsing weird entries, which is why i don't want to just have an text input box for each day.
thanks.
Checkout http://www.w3schools.com/dom/dom_node.asp
Particularly the cloneNode and appendChild methods
Probably best to use JavaScript/JQuery to dynamically add new elements, then use foreach in your PHP script to process all the inputs?
Hope this helps.
Y.J.
Here's a quick little example of duplicating the hours input based on which checkbox is checked. Each hours div gets its' own unique id (see the attr('id',hrsDivID) part of the code below). Mimic this to rename your inputs/selects. See http://jquery.com/ and http://api.jquery.com/ for more.
<head>
<script type="text/javascript" src="/shared/javascript/jquery.js"></script>
</head>
<div id="hours" style='display:none'>
Opening Time:
<select name="starthour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="startminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="startwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
Closing Time:
<select name="endhour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="endminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="endwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
<input type="checkbox" name="24hours[]" value="yes" />Open 24 Hours
</div>
Days:
<div id='day1'>
<input type="checkbox" name="day1[]" value="1" onClick="addHours('day1');" >M
</div>
<div id='day2'>
<input type="checkbox" name="day1[]" value="2" onClick="addHours('day2');" />Tu
</div>
<div id='day3'>
<input type="checkbox" name="day1[]" value="3" onClick="addHours('day3');" />W
</div>
<div id='day4'>
<input type="checkbox" name="day1[]" value="4" onClick="addHours('day4');" />Th
</div>
<div id='day5'>
<input type="checkbox" name="day1[]" value="5" onClick="addHours('day5');" />F
</div>
<div id='day6'>
<input type="checkbox" name="day1[]" value="6" onClick="addHours('day6');"/>Sa
</div>
<div id='day7'>
<input type="checkbox" name="day1[]" value="7" onClick="addHours('day7');"/>Su
</div>
<script>
function addHours(divID){
var d = document.getElementById(divID);
var hrsDivID = divID+'_hours_div';
$('#hours').clone().attr('id',hrsDivID).css('display','').appendTo(d);
return false;
}
</script>

Categories