Here i my form i am having four checkboxes with each having a textbox for its description
<form action="" method="post">
<input type="checkbox" name="days[]" value="Sunday"/>Sunday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Monday"/>Monday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Tuesday"/>Tuesday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Wednesday"/>Wednesday
<input type="text" name="description[]" />
<input type="submit" name="submit" value="submit">
</form>
Here the user can choose any textbox and can write its description...now at the time of insertion...i want to insert only those values where checkboxes are checked...
For e.g: If i choose monday and tuesday then monday and tuesday should be inserted along with their respectice textboxes....my problem is that when i am submitting then the checkbox values are going right but only single description is getting inserted....here is mmy php script...
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$i];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
?>
Can anyone help??
First of all, why aren't you making your life easier; and giving them a normal non array name?
What you can do is;
if ((isset($_POST["days"]) && isset($_POST["description"])) {
$days = $_POST["days"];
$desc = $_POST["description"];
$len = count($days);
for ($i = 0; $i <= $len-1; $i++) {
$noID = filter_var($days[$i], FILTER_SANITIZE_NUMBER_INT);
echo $days[$noID] . " " . $desc[$noID];
}
}
Check if that will work for you :)
Tested & Working:
HTML
<input type="checkbox" name="days[0][day]" value="Sunday"/>Sunday <input type="text" name="days[0][value]" />
<br/><input type="checkbox" name="days[1][day]" value="Monday"/>Monday <input type="text" name="days[1][value]" />
<br/><input type="checkbox" name="days[2][day]" value="Tuesday"/>Tuesday <input type="text" name="days[2][value]" />
<br/><input type="checkbox" name="days[3][day]" value="Wednesday"/>Wednesday <input type="text" name="days[3][value]" />
PHP
<?PHP
$arr = $_POST["days"];
for ($i = 0; $i < count($arr); $i++) {
if (!empty($arr[$i]['day'])) {
echo $arr[$i]['day'] . " and.... its value is " . $arr[$i]['value'];
}
}
?>
In php
<?php
if (isset($_POST['chk_group'])) {
$dayArray = $_POST['days'];
$decArray =$_POST['description'];
for ($i=0; $i<count($dayArray); $i++) {
$dayz=$dayArray[$i];
$description=$decArray[$i];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
?>
try to use description[dayname] in the form and use $description=$_POST["description"][$dayz] in processing, you have array indexing issue because checkbox are just not send if not checked.
here i my form i am having four checkboxes with each having a textbox for its description
<form action="" method="post">
<input type="checkbox" name="days[]" value="Sunday"/>Sunday <input type="text" name="description[Sunday]" />
<br/><input type="checkbox" name="days[]" value="Monday"/>Monday <input type="text" name="description[Monday]" />
<br/><input type="checkbox" name="days[]" value="Tuesday"/>Tuesday <input type="text" name="description[Tuesday ]" />
<br/><input type="checkbox" name="days[]" value="Wednesday"/>Wednesday <input type="text" name="description[Wednesday]" />
<input type="submit" name="submit" value="submit">
</form>
and php:
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$dayz];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
this may help
$str = '';
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$i];
$str .= "(".$dayz.", ".$description.")";
if ((count($_POST["days"])-1) < $i) {
$str .= ',';
}
}
mysql_query("insert into transport_two (transport_id,name) values ".$str) ;
Firstly name each text input and check-box with different name.
<form action="" method="post">
<input type="checkbox" name="days1" value="Sunday"/>Sunday
<input type="text" name="days1_1" /><br/>
<input type="checkbox" name="days2" value="Monday"/>Monday
<input type="text" name="days2_2" /><br/>
<input type="checkbox" name="days3" value="Tuesday"/>Tuesday
<input type="text" name="days3_3" /><br/>
<input type="checkbox" name="days4" value="Wednesday"/>Wednesday
<input type="text" name="days4_4" />
<input type="submit" name="submit" value="submit">
</form>
Use this code for checking which check-box are checked.
<?php
if(isset($_POST["submit"]))
{
for($i=0;$i<$number_of_days;$i++)
{
$dayz="days".$i;
$description=$dayz."_".$i;
if(isset($dayz))
{
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
}
?>
Related
I created a page for multiple rows submit data to mysql with php!
But, I need filter check the checkbox[] has been checked for submit current row data
In my demo,
If I checked the row2 and row3, I expected I will get id=2 & id=3
finally I get the id=1 & id=2
In the same situation, if I checked row3 only, I will get the id=1
I probably understand the principle, but I really can’t find a solution
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
if (#$_POST['checked'][$key] == "on") {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
}
print_r($row);
?>
<form action="" method="POST">
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">submit</button>
</form>
I try #CBroe
if checked row3, I still get a
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
$row[$key]['checkbox'] = $_POST['checkbox'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="checked[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">Submit</button>
</form>
#CBroe Thanks for your
<?php
$row = "";
if ($_POST) {
foreach ($_POST["id"] as $key => $v) {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="id[1]" value="1">
<input type="text" name="other_value[1]" value="a">
</p>
<p>
<input type="checkbox" name="id[2]" value="2">
<input type="text" name="other_value[2]" value="b">
</p>
<p>
<input type="checkbox" name="id[3]" value="3">
<input type="text" name="other_value[3]" value="c">
</p>
<button type="submit">submit</button>
</form>
How to calculate sum of PHP multiple checkboxes values array - Total Price of checked checkboxes?
For example, result should be displayed like:
Total Price of Selected Programming Languages : C++,Java = 1200$
<form method="post" action="#">
0<input type="checkbox" name="count[]" id="count[]" value="0"/>
<input type="hidden" name="language[]" id="language" value="C"/>C [$800]
<input type="hidden" name="price[]" id="price" value="800"> <br/>
1<input type="checkbox" name="count[]" id="count[]" value="1"/>
<input type="hidden" name="language[]" id="language" value="C++"/>C++ [$700]
<input type="hidden" name="price[]" id="price" value="700"> <br/>
2<input type="checkbox" name="count[]" id="count[]" value="2"/>
<input type="hidden" name="language[]" id="language" value="Assembler"/>Assembler [$600]
<input type="hidden" name="price[]" id="price" value="600"><br/>
3<input type="checkbox" name="count[]" id="count[]" value="3"/>
<input type="hidden" name="language[]" id="language" value="Java"/>Java [$500]
<input type="hidden" name="price[]" id="price" value="500"> <br/>
4<input type="checkbox" name="count[]" id="count[]" value="4"/>
<input type="hidden" name="language[]" id="language" value="PHP"/>PHP [$400]
<input type="hidden" name="price[]" id="price" value="400"> <br/>
<input type="submit" name="sbt" id="sbt" value="SUBMIT">
</form>
This is the PHP code:
<?php
if(isset($_POST['sbt'])){
$count = $_POST['count'];
$sub_menu = $_POST['sub_menu'];
$sub_price = $_POST['sub_price'];
$sub_price1 = $_POST['sub_price'];
//$total_price = array($sub_menu => $sub_price);
foreach($count as $j)
echo $sub_menu[$j] . '['.$sub_price[$j]. ']' ;
}
?>
$items = array();
$total = 0;
foreach($_POST['price'] as $k => $price) {
if(in_array($k, $_POST['count'])) {
$items[] = $_POST['language'][$k];
$total += intval($price);
}
}
$items = implode(", ", $items);
echo $items . " = $" . $total;
You need to make sure the array indexes are the same for each group:
0<input type="checkbox" name="count[0]" value="0"/>
<input type="hidden" name="language[0]" value="C"/>C [$800]
<input type="hidden" name="price[0]" value="800"> <br/>
1<input type="checkbox" name="count[1]" value="1"/>
<input type="hidden" name="language[1]" value="C++"/>C++ [$700]
<input type="hidden" name="price[1]" value="700"> <br/>
Then you can get the price and language for each count:
$price = array_intersect_key($_POST['price'], $_POST['count']);
$language = array_intersect_key($_POST['language'], $_POST['count']);
Then implode the text and sum the price:
echo "Total Price of Selected Programming Languages: "
. implode(',', $language) . ' = $'
. array_sum(price);
I have n number of text box (n may be any number) with same name. And
I want to access the value of all the text box of that name.
Ex-:
<form method="post" id="create">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
jQuery
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var code='<input type="text" name="user[]" />';
jQuery('#create').append(code);
</script>
or is there any other way to access the value of the text box. Either
by class or any other property..
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var count = document.getElementById('count').value;
count++;
var code = '<input type="text" name="user'+count+'" />';
jQuery('#create').append(code);
document.getElementById('count').value = count;
</script>
and your html like...
<form method="post" id="create">
<input type="hidden" id="count" value="0" name="count">
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
and in your php code...
<?php
if(isset($_POST))
{
$count = $_POST['count'];
for($i=1;$i<=$count;$i++)
{
$user.$i = $_POST['user'.$i];
}
}
?>
Try this one, it will show you the values :
<form action="#" method="post">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="submit" value="submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST['user'] as $key => $value)
{
echo $key." has the value = ". $value."<br>";
}
}
?>
See this in action: http://wistudat.be/try/array.php
I have had a look at a few answers but none relates to my specific problem. I have an array that takes user input and sorts the array in an ascending order. That's all good, I now want the user to enter a number for which I can locate the array index, if the number is not located, the function returns -1. I cannot put my finger on the error, I'm getting -1 at all times.
Any help is truly appreciated
This is my form that picks up the information.
<form action="welcome.php" method="POST" value="">
Name: <input type="text" name="fname" value="">
Age: <input type="text" name="age" value="">
Integer :<input type="number" name="integer" value="">
<div class="container">
<p> Please choose numbers: </p>
<label>First:</label>
<input type="number" name="name[]" ><br />
<label>2nd</label>
<input type="number" name="name[]"><br />
<label>3rd</label>
<input type="number" name="name[]" ><br />
<label>4th</label>
<input type="number" name="name[]"><br />
<label>5th</label>
<input type="number" name="name[]"><br />
<label>6th</label>
<input type="number" name="name[]"><br />
<label>7th</label>
<input type="number" name="name[]"><br />
<label>8th</label>
<input type="number" name="name[]"><br />
<label>9th</label>
<input type="number" name="name[]"><br />
<label>10th</label>
<input type="number" name="name[]"><br />
<label>Pos Number</label>
<input type="number" name="pos"><br />
<input type="submit">
And the index locator:
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
<?php
$tmp = trim($_POST['integer']);
if (!ctype_digit($tmp)) {echo "Input requires an inetger " ;}
else{
echo "The integer is ".$_POST["integer"]."<br>";
}
echo"You have entered the numbers below <br>";
$name=$_POST['name'];
sort($name);
foreach( $name as $v) {
print $v."<br>";
}
echo "You wish to find the position of ".$_POST['pos']."<br />";
function search($name,$K){
$l=0;
$size=sizeof($name);
$u=$size-1;
$m=0;
while ($l<=$u){
$m = ($l+$u)/2;
round($m);
echo "this is m ".$m;
if ($name[$m]<$K) {
$l=$m+1;
}
else if ($name[$m]==$K) {
return $m;
return $m;
} else {
$u=$m-1;
}
}
return -1;
}
$po=$_POST['pos'];
$b=search($name,$po);
echo "The position of the entered number is".$b;
?>
I've got a system where I allow a user to select multiple checkboxes from n amount of checkboxes, but also want two more inputs associated with each checkbox. This is for a message, a date and a time. When I post the data to be processed by a PHP script, I'd like to be able to access each of the sets of checkbox and two other inputs so I can see what date and time a user has filled in for each of the messages they've selected. I'm having trouble coming up with a method to associate the two other inputs with each checkbox.
Any ideas how to do this?
You can use arrays in your html inputs like so...
<input type="text" name="messages[1][message]" value="herp" />
<input type="text" name="messages[1][date]" value="24th April" />
<input type="text" name="messages[1][time]" value="13:00" />
<input type="text" name="messages[2][message]" value="derp" />
<input type="text" name="messages[2][date]" value="26th April" />
<input type="text" name="messages[2][time]" value="18:00" />
<?php
$messages = $_REQUEST['messages'];
foreach ($messages as $messageId => $value){
echo $value['message'];
echo $value['date'];
echo $value['time'];
}
Your HTML:
<input type="checkbox" name="car" />
<input type="text" name="msg_car" value="Car message" />
<input type="text" name="date_car" value="Car date" />
<input type="checkbox" name="bike" />
<input type="text" name="msg_bike" value="Bike message" />
<input type="text" name="date_bike" value="Bike date" />
<input type="checkbox" name="train" />
<input type="text" name="msg_train" value="Train message" />
<input type="text" name="date_train" value="Train date" />
<input type="checkbox" name="plane" />
<input type="text" name="msg_plane" value="Plane message" />
<input type="text" name="date_plane" value="Plane date" />
Your PHP script:
$array = array("car", "bike", "train", "plane");
for ($i = 0; $i < count($array); $i++) {
if (isset($_POST[$array[$i]])) {
//Checkbox was checked, get values
$msg = "";
$date = "";
$msg_id = "msg_" . $array[$i];
$date_id = "date_" . $array[$i];
if (isset($_POST[$msg_id]))
$msg = $_POST[$msg_id];
if (isset($_POST[$date_id]))
$date = $_POST[$date_id];
}
}
I think something like this should work. I didn't test it.. So forgive me if this example still contains some minor errors.