How to get values from a multiple checkbox and insert into database? - php

I am working on a certain form that accepts multiple values using checkbox and I find it hard to get all the values of the checked boxes and store them into the database.
Everytime my insertion query is being executed.. the only value that is stored in the database is the last checkbox the user selects thus ignoring the others..
here's my code:
----form-----------------------------------------------------------------'
<form name="addRes" method="post" action="addReservation_code.php">
<table>
<tr><td>Activity Date:</td>
<td><select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value"December">December</option>
</select>
<select name="day">
<?php for($ctr=1;$ctr<=31;$ctr++){ ?>
<option value="<?php print($ctr); ?>"><?php print($ctr); ?></option>
<?php } ?>
</select> -
<select name="day2">
<?php for($ctr=1;$ctr<=31;$ctr++){ ?>
<option value="<?php print($ctr); ?>"><?php print($ctr); ?></option>
<?php } ?>
</select>
<input type="text" name="year"/></td>
</tr>
<tr><td>Activity Name:</td><td><input type="text" name="act_name" /></td></tr>
<tr><td>Activity Time:</td><td><input type="text" name="act_time" /></td></tr>
<tr><td>Room:</td>
<td>
<select name="room">
<?php
include 'RRSdbconnect.php';
$query = "SELECT room_name from room";
$result=mysql_query($query) or die('Invalid query'.mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
echo "<option>$row[room_name]";
}
?>
</option>
</select>
</td>
</tr>
<tr>
<td>Resources Needed:</td>
<td><input type="checkbox" name="resources" value="cpu" />CPU<br />
<input type="checkbox" name="resources" value="lcd" />LCD<br />
<input type="checkbox" name="resources" value="sounds" />Sounds<br />
<input type="checkbox" name="resources" value="sounds" />Others, Pls. specify<input type="text" name="others" /></td>
</tr>
<tr><td>No. of Person</td><td><input type="text" name="noOfPerson"/></td></tr>
<tr><td>Reserved by:</td><td><input type="text" name="reservedby" /></td></tr>
<tr><td></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"/><input type="reset" name="clear" value="Clear"/></td></tr>
</table>
</form>
------------action code----------------------------------------------------
<?php
include 'RRSdbconnect.php';
session_start();
$date = $_POST['month'] . ' ' .$_POST['day']. '-' .$_POST['day2']. ', ' . $_POST['year'] ;
$name=$_POST['act_name'];
$time=$_POST['act_time'];
$room=$_POST['room'];
$resources=$_POST['resources'];
$noOfPerson=$_POST['noOfPerson'];
$reservedby=$_POST['reservedby'];
//insertions for add reservations
$query="insert into reservation (activity_date, activity_name, activity_time, room, resources_needed, no_person, reserved_by) values('$date','$name','$time','$room','$resources','$noOfPerson','$reservedby')";
$result=mysql_query($query,$link) or die("Error!".mysql_error());
?>
PS.
I am using checkbox on the column resources needed..
any help will be appreciate..tnx

If you change the name attribute of the checkbox to name="resources[]" you should get an array in the $_POST['resources']
Once you have the array in $_POST['resources']you can store/retrieve it in the database how you like. For example either as a string using implode() / explode() or serialize() / unserialize().

Related

Get value from each select multiple which have same name using php

HTML :
...
<form method="post">
<tr>
<td>
<input type="text" name="text[]">
<select name="list[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="text" name="text[]">
<select name="list[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
... and another row addition using jquery dynamic rows.
<input type="submit" name="submit" value="submit">
</form>
PHP :
<?php
if (isset($_POST['submit'])) {
$cnt = count($_POST['text']);
for($i=0;$i<$cnt;$i++) {
echo $_POST['text'][$i]; //correct code
foreach ($_POST['list'] as $item) { //wrong code!
echo $item; //wrong code!
} //wrong code!
}
}
?>
//note: "wrong code!" generate all $_POST['list'] should be each $_POST['list'][$i]
I'm stuck on wrong code above, how to get each $_POST['list'][$i] value ?.
Note: for certain reason we can't use auto increment ID if needed for each select element, unique random ID seems ok.

How to set timestamp with this php script

I have this script. When I select the date then I get the data by selecting date, but I want to set this with timestamp
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th> Birtday </th>
<th> Name </th>
<th> Gender </th>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1="0000-00-00"; };
$result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
$result->bindParam(':a', $d1);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
How I set timestamp with this script
Just use date() to echo out today's date as the value of your d1 input field:
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="<?php echo date("m/d/Y"); ?>" />
<input type="submit" value="Search">
</form>
Ya the date - function will give all the information you need. If you havn't worked with it or just in case you are new to PHP, have a look at the documentation for finding the right date parameters.
For example:
date("d.m.Y") //outputs: 13.03.2014
date("n.d.Y") //outputs: 3.13.2014
date("m.d.y") //outputs: 03.13.14
The whole documentation can be found here: http://php.net/manual/en/function.date.php
EDIT::
So if I got you right you are looking for something simular like this:
<select class="Calendar" id="cdate_Month_ID" >
<option value="1" <?=(date('n')==1?'selected':'')?>>Jan</option>
<option value="2" <?=(date('n')==2?'selected':'')?>>Feb</option>
<option value="3" <?=(date('n')==3?'selected':'')?>>Mar</option>
<option value="4" <?=(date('n')==4?'selected':'')?>>Apr</option>
<option value="5" <?=(date('n')==5?'selected':'')?>>May</option>
<option value="6" <?=(date('n')==6?'selected':'')?>>Jun</option>
<option value="7" <?=(date('n')==7?'selected':'')?>>Jul</option>
<option value="8" <?=(date('n')==8?'selected':'')?>>Aug</option>
<option value="9" <?=(date('n')==9?'selected':'')?>>Sep</option>
<option value="10" <?=(date('n')==10?'selected':'')?>>Oct</option>
<option value="11" <?=(date('n')==11?'selected':'')?>>Nov</option>
<option value="12" <?=(date('n')==12?'selected':'')?>>Dec</option>
</select>
<select class="Calendar" id="cdate_Day_ID" >
<option <?=(date('d')==1?'selected':'')?>>1</option>
<option <?=(date('d')==2?'selected':'')?>>2</option>
<option <?=(date('d')==3?'selected':'')?>>3</option>
<option <?=(date('d')==4?'selected':'')?>>4</option>
<option <?=(date('d')==5?'selected':'')?>>5</option>
<option <?=(date('d')==6?'selected':'')?>>6</option>
<option <?=(date('d')==7?'selected':'')?>>7</option>
<option <?=(date('d')==8?'selected':'')?>>8</option>
<option <?=(date('d')==9?'selected':'')?>>9</option>
<option <?=(date('d')==10?'selected':'')?>>10</option>
<option <?=(date('d')==11?'selected':'')?>>11</option>
<option <?=(date('d')==12?'selected':'')?>>12</option>
<option <?=(date('d')==13?'selected':'')?>">13</option>
<option <?=(date('d')==14?'selected':'')?>>14</option>
<option <?=(date('d')==15?'selected':'')?>>15</option>
<option <?=(date('d')==16?'selected':'')?>>16</option>
<option <?=(date('d')==17?'selected':'')?>>17</option>
<option <?=(date('d')==18?'selected':'')?>>18</option>
<option <?=(date('d')==19?'selected':'')?>>19</option>
<option <?=(date('d')==20?'selected':'')?>>20</option>
<option <?=(date('d')==21?'selected':'')?>>21</option>
<option <?=(date('d')==22?'selected':'')?>>22</option>
<option <?=(date('d')==23?'selected':'')?>>23</option>
<option <?=(date('d')==24?'selected':'')?>>24</option>
<option <?=(date('d')==25?'selected':'')?>>25</option>
<option <?=(date('d')==26?'selected':'')?>>26</option>
<option <?=(date('d')==27?'selected':'')?>>27</option>
<option <?=(date('d')==28?'selected':'')?>>28</option>
<option <?=(date('d')==29?'selected':'')?>>29</option>
<option <?=(date('d')==30?'selected':'')?>>30</option>
<option <?=(date('d')==31?'selected':'')?>>31</option>
</select>
<input class="Calendar" type="text" size="4" maxlength="4" title="Year" value="<?=date('Y'?>">

PHP Form Multiple Lines into MySQL

i've searched and searched and maybe i found actually the right post but nothing worked for me so sorry for any duplication but maybe someone can help me out.
I have a php that gets me a form with multiple rows (based on an form input before).
Here's the php that creates the input table:
<html>
<head></head>
<body>
New report with EEM Number:<input type="text" name="eemnumber" />
<tr>
<td>Expense Date</td>
<td>Category</td>
<td>Vendor</td>
<td>Receipt Amount</td>
<td>Currency</td>
<td>Payment Amount</td>
<td>Currency</td>
<td>Payment Type</td>
<td>Country</td>
</tr>
<?php
$rows = $_POST['rows'];
$n = 0;
while($n < $rows)
{
$n++;
?>
<tr>
<td><input type="date" name="expensedate"/><br /></td>
<td><select name="category">
<option value="AirFare">AirFare</option> />
<option value="AutoRental">Auto Rental - Employee</option> />
<option value="DailyAllowance">Daily Allowance Standard</option> />
<option value="Gasoline">Gasoline - Auto Rental</option> />
<option value="Lodging">Lodging (Hotel) - Room / Tax</option> />
<option value="Meals">Meals / Entertain - HP only, Travel</option> />
<option value="Misc">Miscellaneous</option> />
<option value="Taxi">Taxi / Subway / Bus / Train</option> />
<br/>
</td>
<td><input type="text" name="vendor"/><br /></td>
<td><input type="text" name="receiptamount"/>
</td>
<td><select name="currrec">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><input type="text" name="paymentamount"/>
</td>
<td><select name="currpay">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><select name="paytype">
<option value="cash">Cash</option> />
<option value="cc">Credit Card</option> />
<br/>
</td>
<td><select name="country">
<option value="DE">Germany</option> />
<option value="SE">Sweden</option> />
<option value="US">USA</option> />
<br/>
</td>
</tr>
<?PHP
}
?>
</table>
</div>
<p>
<input type="submit"/>
</form>
<!-- End of FORM -->
Back
</body>
</html>
So once all data was entered I click on Submit and would of course like to get every line into my DB. Here's the script i tried do use but only gets me the last...
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$eemnumber=$_POST['expensedate'];
$expensedate=$_POST['expensedate'];
$category=$_POST['category'];
$vendor=$_POST['vendor'];
$receiptamount=$_POST['receiptamount'];
$currrec=$_POST['currrec'];
$paymentamount=$_POST['paymentamount'];
$currpay=$_POST['currpay'];
$paytype=$_POST['paytype'];
$eemnumber=$_POST['eemnumber'];
$country=$_POST['country'];
$sql="INSERT INTO tbl_eem_data
(ExpenseDate, Category, Vendor, ReceiptAmount, CurrRec, PaymentAmount, CurrPay, PayType, EEMNumber, Country)
VALUES
$expensedate,
$category,
$vendor,
$receiptamount,
$currrec,
$paymentamount,
$currpay,
$paytype,
$eemnumber,
$country)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header("location:index.php");
?>
Anyone any clue?
Thanks!!!
You need to change form Structure and form Fields
<?php
$rows = $_POST['rows'];
$n = 0;
while($n < $rows)
{
$n++;
?>
<tr>
<td><input type="date" name="expensedate[<?echo $n?>]"/><br /></td>
<td><select name="category">
<option value="AirFare">AirFare</option> />
<option value="AutoRental">Auto Rental - Employee</option> />
<option value="DailyAllowance">Daily Allowance Standard</option> />
<option value="Gasoline">Gasoline - Auto Rental</option> />
<option value="Lodging">Lodging (Hotel) - Room / Tax</option> />
<option value="Meals">Meals / Entertain - HP only, Travel</option> />
<option value="Misc">Miscellaneous</option> />
<option value="Taxi">Taxi / Subway / Bus / Train</option> />
<br/>
</td>
<td><input type="text" name="vendor[<?echo $n?>]"/><br /></td>
<td><input type="text" name="receiptamount[<?echo $n?>]"/>
</td>
<td><select name="currrec">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><input type="text" name="paymentamount[<?echo $n?>]"/>
</td>
<td><select name="currpay[<?echo $n?>]">
<option value="EUR">EUR</option> />
<option value="SEK">SEK</option> />
<option value="USD">USD</option> />
</td>
<td><select name="paytype[<?echo $n?>]">
<option value="cash">Cash</option> />
<option value="cc">Credit Card</option> />
<br/>
</td>
<td><select name="country[<?echo $n?>]">
<option value="DE">Germany</option> />
<option value="SE">Sweden</option> />
<option value="US">USA</option> />
<br/>
</td>
</tr>
<?PHP
}
?>
<input type="hidden" name="rows" value="<?echo $n?>" />
Then apply similar loop to fetch those values.
You also need to have hidden variable which stores total number of rows you have.
Thanks
You seem to be missing an open parenthesis ( in your SQL right after VALUES.
Fix it and hopefully it will work: VALUES ( . . . )
Basically, run this test:
HTML:
<input type="text" name="key" value="1" />
<input type="text" name="key" value="2" />
PHP:
<?
print_r($_POST);
?>
And then run this:
HTML:
<input type="text" name="key[]" value="1" />
<input type="text" name="key[]" value="2" />
PHP:
<?
print_r($_POST);
?>
See if you can make any conclusions with this, and let me know what you think.
You are generating form fields based on the number of $rows received from the Request.
<form ... <!- this line was missing in your code -->
$rows = $_POST['rows'];
while($n < $rows)
...
</form>
And after submit, you are not reading parameters into arrays.
$eemnumber=$_POST['expensedate']; /* this is wrong again */
$expensedate=$_POST['expensedate'];
$category=$_POST['category'];
$vendor=$_POST['vendor'];
Which lead the last value from the submitted array values assigned to the variable in concern.
And hence you see that only last row read and pushed into DB.
Change your code accordingly and it should be working.
Refer to:
PHP: Reading POST data with arrays

php form isset validation

i have been trying to implement isset form validation on a signup form i am developing for my site......
basically i need all fields filled in correctly or form wont $_post.....
as im posting to self (form action="") i will need my form to reload and display error messages above form if not filled correctly....
here is my form code ....
<?php
if(!isset($_POST['submit'])) {
echo '
<form action="" method="post" name="signup">
<table border="1" width="100%">
<tr><td>
<p style="text-align: right;">First Name: </p>
</td><td>
<input name="first_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Last Name: </p>
</td><td>
<input name="last_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Desired Username: </p>
</td><td>
<input name="username" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Password: </p>
</td><td>
<input name="password" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Confirm Password: </p>
</td><td>
<input name="confpassword" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Date Of Birth: </p>
</td><td>
<select name="dob_day">
<option value="000">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="dob_month">
<option value="000">Month</option>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
Year:
<input name="dob_year" type="text" maxlength="4" size="10" value="eg: 1964">
</td></tr>
<tr><td>
<p style="text-align: right;">Email Address: </p>
</td><td>
<input name="email" type="text" maxlength="50">
</td></tr>
<tr><td>
<p style="text-align: right;">Gender: </p>
</td><td>
Male: <input type="radio" name="gender" value="male" />
Female: <input type="radio" name="gender" value="female" />
</td></tr>
</table><p>
<center><input type="submit" name="submit" value=" Sign-Up "></center>
</font>
';}
else
{
echo "Form Submitted";
}
?>
i know this form code looks garbled, and i hate messy code, but i had to truncate it to post on here, or i would be typing 4 spaces before hundreds of code lines lol
if anyone can come up with a solution to this (would presume pretty simple but cannot figure how to code it") i will be very greatful again :D
thanks guys
:bow: STACK EXCHANGE ALL THE WAY :bow:
else
{
$accept=true;
if(!isset($_POST["value1"])){
$accept=false;
$_SESSION["error"] .= "Please fill in value1\n";
}
//.... Other values
if(!$accept){
header("Location: Current file name"); //Check if the session has the error value filled in and display it on the form.
exit;
}
echo "Form Submitted";
}
Full example:
<?php
session_start();
if(!isset($_POST['submit'])) {
echo '
<form action="" method="post" name="signup">
<table border="1" width="100%">
<tr><td colspan="2">' . $_SESSION['error'] . ' </td></tr>
<tr><td>
<p style="text-align: right;">First Name: </p>
</td><td>
<input name="first_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Last Name: </p>
</td><td>
<input name="last_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Desired Username: </p>
</td><td>
<input name="username" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Password: </p>
</td><td>
<input name="password" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Confirm Password: </p>
</td><td>
<input name="confpassword" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Date Of Birth: </p>
</td><td>
<select name="dob_day">
<option value="000">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="dob_month">
<option value="000">Month</option>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
Year:
<input name="dob_year" type="text" maxlength="4" size="10" value="eg: 1964">
</td></tr>
<tr><td>
<p style="text-align: right;">Email Address: </p>
</td><td>
<input name="email" type="text" maxlength="50">
</td></tr>
<tr><td>
<p style="text-align: right;">Gender: </p>
</td><td>
Male: <input type="radio" name="gender" value="male" />
Female: <input type="radio" name="gender" value="female" />
</td></tr>
</table><p>
<center><input type="submit" name="submit" value=" Sign-Up "></center>
</font>
';}
else
{
$accept=true;
if(!isset($_POST["first_name"])){
$accept=false;
$_SESSION["error"] .= "Please fill in your first name.<br />";
}
//.... Other values
if(!$accept){
header("Location: " . $_SERVER["PHP_SELF"]); //Check if the session has the error value filled in and display it on the form.
exit;
}
echo "Form Submitted";
}
?>
Form validation can be done using javascript.
HTML
<input type="submit" onclick="return validate();" />
JAVASCRIPT
<script type="text/javascript">
function validate()
{
if(document.getElementsByName('username') == "")
{
alert("Please Enter username");
return false;
}
...
}
</script>
all your code currently does it verify someone clicked the submit button, it doesnt actually verify they entered valid values. Understand that isset doesnt check that a variable has a value, just that the variable exists, which it will since all form elements will exist in the post array regardless of what, if any value is posted.
2 tips for you:
1) use a foreach loop to evaluate each element of the post array. You can create a seperate array of expected value conditions to check against in the loop. There are plenty of ready to use validation filter scripts out there you can just plug in to check that your expected value is char, int, float etc. PLEASE always validate your user input before inserting into your database or your asking for SQL injection attack.
2) use print_r($_POST) to debug your form submissions if you dont understand what exactly is being submitted....
also I find its easier to put the PHP at the top of the file and leave the html below. If you arent using PHP values to prefill out form values, there is no need to echo your html. This makes it much easier to edit your html... observe the php tags in the start and end of the else statement:
<?php
if(isset($_POST['submit'])) {
echo "Form Submitted";
}
else{ ?>
<form action="" method="post" name="signup">
<table border="1" width="100%">
<tr><td>
<p style="text-align: right;">First Name: </p>
</td><td>
<input name="first_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Last Name: </p>
</td><td>
<input name="last_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Desired Username: </p>
</td><td>
<input name="username" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Password: </p>
</td><td>
<input name="password" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Confirm Password: </p>
</td><td>
<input name="confpassword" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Date Of Birth: </p>
</td><td>
<select name="dob_day">
<option value="000">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="dob_month">
<option value="000">Month</option>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
Year:
<input name="dob_year" type="text" maxlength="4" size="10" value="eg: 1964">
</td></tr>
<tr><td>
<p style="text-align: right;">Email Address: </p>
</td><td>
<input name="email" type="text" maxlength="50">
</td></tr>
<tr><td>
<p style="text-align: right;">Gender: </p>
</td><td>
Male: <input type="radio" name="gender" value="male" />
Female: <input type="radio" name="gender" value="female" />
</td></tr>
</table><p>
<center><input type="submit" name="submit" value=" Sign-Up "></center>
</font>
<?php } ?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if (isset($_GET['submit']))
{
$name = $_GET['name'];
$age = $_GET['age'];
echo "Welcome ".$name."<br />" ;
echo "You are ".$age." years old<br/>";
}
else
{
echo "enter the appropriate details";
exit();
}
?>
</body>
</html>
<html>
<body>
if
(isset($_GET['actie']) && $_GET['actie']== "registreren") {
//registreerformulier
echo "<form method='post' action='".$_SERVER['PHP_SELF']."?actie=aanmaken'>
Voornaam <input name='voornaam' type='text' /> <br />
Familienaam <input name='familienaam' type='text' /> <br />
Wachtwoord <input name='wachtwoord' type='password' /> <br />
Wachtwoord-controle <input name='wachtwoordControle' type='password' /> <br />
<input name='submit' type='submit' value='Maak gebruiker aan' />
</form>";
</body>
</html>

How to get multiple selected values of select box in php?

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.
If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …
Then you can acces the array in your PHP script
<?php
header("Content-Type: text/plain");
foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";
$_GET may be substituted by $_POST depending on the <form method="…" value.
Change:
<select name="select2" ...
To:
<select name="select2[]" ...
You can use this code to retrieve values from multiple select combo box
HTML:
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
PHP:
<?php
$values = $_POST['ary'];
foreach ($values as $a){
echo $a;
}
?>
Use the following program for select the multiple values from select box.
multi.php
<?php
print <<<_HTML_
<html>
<body>
<form method="post" action="value.php">
<select name="flower[ ]" multiple>
<option value="flower">FLOWER</option>
<option value="rose">ROSE</option>
<option value="lilly">LILLY</option>
<option value="jasmine">JASMINE</option>
<option value="lotus">LOTUS</option>
<option value="tulips">TULIPS</option>
</select>
<input type="submit" name="submit" value=Submit>
</form>
</body>
</html>
_HTML_
?>
value.php
<?php
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
You can iterate it directly like this
foreach ($_GET['select2'] as $value)
echo $value."\n";
or you can do it like this
$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";
This will display the selected values:
<?php
if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}
?>
// CHANGE name="select2" TO name="select2[]" THEN
<?php
$mySelection = $_GET['select2'];
$nSelection = count($MySelection);
for($i=0; $i < $nSelection; $i++)
{
$numberVal = $MySelection[$i];
if ($numberVal == "11"){
echo("Eleven");
}
else if ($numberVal == "12"){
echo("Twelve");
}
...
...
}
?>
You could do like this too. It worked out for me.
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHP code below. It print the selected multiple values accordingly.
$shift=$_POST['selectDuration'];
print_r($shift);
I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:
for(i=0; i < form.select.options.length; i++)
if (form.select.options[i].selected)
form.hidden.value += form.select.options[i].value;
Next, i get by post that field and get all the string ;-)
I hope it'll be work for somebody more. Thanks to all.
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
form submit with enctype="multipart/form-data"

Categories