Ok I know this has been answered a few times, but I just can't seem to get mine working!
I have 8 weeks in my database (set up as columns) and I display the value of these with php as follows
<form method="post" id="updating" action="<?php $_PHP_SELF ?>">
<?php
$count = 1;
foreach ($results as $v) {
while($count<9){
$week = Week.$count;
$checkvalue = $v -> $week;
?>
Week<?echo $count;?> <input type="checkbox" id="Week<?php echo$count;?>" name="week[]" value="<?php echo $checkvalue;?>" <?php if($checkvalue==1){?>
checked="checked"
<?php } ?>
/>
<?php
$count++;
}
}
?>
<input name="update" type="submit" id="update" value="Update">
</form>
I know it's probably not as graceful as it should be, but it does output the correct HTML for me and displays the information as it's presented in the database.
<form method="post" id="updating" action="">
Week1 <input type="checkbox" id="Week1" name="week[]" value="1" checked="checked"
/>
Week2 <input type="checkbox" id="Week2" name="week[]" value="0" />
Week3 <input type="checkbox" id="Week3" name="week[]" value="1" checked="checked"
/>
Week4 <input type="checkbox" id="Week4" name="week[]" value="1" checked="checked"
/>
Week5 <input type="checkbox" id="Week5" name="week[]" value="0" />
Week6 <input type="checkbox" id="Week6" name="week[]" value="1" checked="checked"
/>
Week7 <input type="checkbox" id="Week7" name="week[]" value="1" checked="checked"
/>
Week8 <input type="checkbox" id="Week8" name="week[]" value="1" checked="checked"
/>
<input name="update" type="submit" id="update" value="Update">
</form>
The problem I'm having is when I tick or untick those checkboxes, the updated values don't appear to be captured, and hence the database isn't being updated properly. Here's where the self post request is handled.
<?php if(isset($_POST['update']))
{
$count = 1;
if(isset($_POST['week']) && !empty($_POST['week']))
foreach($_POST['week'] as $w){ echo $w; //prints 1 all the time
$week = Week.$count;
echo $week;//prints out weeks I want to update correctly (database headings)
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
$week.'=' . $db->quote( $w ));
// Conditions for which records should be updated.
$conditions = array(
'UserID='. (int) $user_id);
$query->update($db->quoteName('tools_engage'))->set($fields)->where($conditions);
$db->setQuery($query);
try {
$result = $db->query(); // Use $db->execute() for Joomla 3.0.
echo ("Values saved and updated </br>");
$count++;
} catch (Exception $e) {
// Catch the error.
}
}
}?>
I'm doing this in Joomla, hence the weird database update syntax, but it does work and I've used them elsewhere in this program so I know they're not the problem
As I commented in the code, the array week[] just seems to contain the value 1 for everything, no matter what boxes I check or uncheck. Which then results in every value in the database being updated to 1/true which obviously isn't right.
I have a suspicion the problem lies in my HTML output for the update form, but can't seem to crack it, any help is much appreciated!
Update:
Ok so going on nvuono's suggestion I have updated my form to be like this
<?php
$count = 1;
foreach ($results as $v) {
while($count<9){
$week = Week.$count;
$checkvalue = $v -> $week;
?>
Week<?echo $count;?> <input type="checkbox" id="Week<?php echo$count;?>" name="week[<?php echo $count;?>]" value="1" <?php if($checkvalue==1){?>
checked="checked"
<?php } ?>
/>
<?php
$count++;
}
}
?>
So now each name has an index in the array does it not? If that is the case, should my
if(isset($_POST['update']))
section not now be printing out all the values in the array in the order they've been entered?
ie. My logic would be that it prints out 1,0,0,1,0,0,0 (box 1 and 4 checked) or what ever boxes are checked. But instead it just prints out 1,1 so I've no way of knowing what index they are, and subsequently what week I should be updating
I added some really hacky way of doing the update. You need to beware of SQL Injection and other bad things when using post... make sure you always validate and sanitize your data. The "hacky" way works because we're working with integer values and those are easily cleaned.
Screenshot:
This is my table... I called it so for stackoverflow...
+----+-------+-------+-------+-------+-------+-------+-------+-------+
| id | week1 | week2 | week3 | week4 | week5 | week6 | week7 | week8 |
+----+-------+-------+-------+-------+-------+-------+-------+-------+
| 1 | | | | | | | | |
| 2 | 1 | | | | | | | |
| 3 | 1 | | 1 | | | | | |
| 4 | 1 | | 1 | | 1 | | | |
| 5 | 1 | | 1 | | 1 | | 1 | |
+----+-------+-------+-------+-------+-------+-------+-------+-------+
This is my PHP file.... (stick it in an empty PHP document to run, it's self contained besides changing the db connection and query)
<?php
$mysqli = new mysqli("localhost", "root", "password", "test");
if (!empty($_POST)) {
print "<pre>POST VARS: \n".print_r($_POST,true)."</pre>";
foreach($_POST as $i => $data)
{
if (substr($i,0,3)=='row' && is_numeric(substr($i,3)))
{
$row_id = substr($i,3);
$data = array_flip($data);
$values = array();
for ($x=1; $x<9; $x++) {
$values[] = "week$x = ". ((isset($data[$x])) ? '1' : '0');
}
$stmt = "\nupdate so ".
"\n set ".implode(", \n ",$values).
"\n where id = $row_id; \n";
$update = $mysqli->query($stmt);
if ($update) { print "Row $row_id updated successfully.<br/>"; }
}
print "<br/>";
}
}
$result = $mysqli->query("select * from so");
$mysqli->close();
?>
<form method="post" id="updating" action="<?php $_PHP_SELF ?>">
<?php
while($row = $result->fetch_object())
{
$count = 1;
print "<div style='border:1px solid black; display:inline-block;'>\n";
print "Row ".$row->id."<br/>\n";
while($count < 9)
{
$week = "week$count";
$checkvalue = $row->{$week};
?>
Week<?php echo $count ?> <input type="checkbox" id="Week<?php echo$count;?>" name="row<?php echo $row->id
?>[]" value="<?php echo $count;?>" <?php if($checkvalue==1){
?> checked="checked" <?php } ?> />
<?php
$count++;
}
print "</div><br/><br/>\n\n\n";
}
?>
<input name="update" type="submit" id="update" value="Update">
</form>
This is the $_POST after submit:
POST VARS:
Array
(
[row2] => Array
(
[0] => 1
)
[row3] => Array
(
[0] => 1
[1] => 3
)
[row4] => Array
(
[0] => 1
[1] => 3
[2] => 5
)
[row5] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 7
)
[update] => Update
)
My "view source" of the form:
<form method="post" id="updating" action="">
<div style='border:1px solid black; display:inline-block;'>
Row 1<br/>
Week1 <input type="checkbox" id="Week1" name="row1[]" value="1" />
Week2 <input type="checkbox" id="Week2" name="row1[]" value="2" />
Week3 <input type="checkbox" id="Week3" name="row1[]" value="3" />
Week4 <input type="checkbox" id="Week4" name="row1[]" value="4" />
Week5 <input type="checkbox" id="Week5" name="row1[]" value="5" />
Week6 <input type="checkbox" id="Week6" name="row1[]" value="6" />
Week7 <input type="checkbox" id="Week7" name="row1[]" value="7" />
Week8 <input type="checkbox" id="Week8" name="row1[]" value="8" />
</div><br/><br/>
<div style='border:1px solid black; display:inline-block;'>
Row 2<br/>
Week1 <input type="checkbox" id="Week1" name="row2[]" value="1" checked="checked" />
Week2 <input type="checkbox" id="Week2" name="row2[]" value="2" />
Week3 <input type="checkbox" id="Week3" name="row2[]" value="3" />
Week4 <input type="checkbox" id="Week4" name="row2[]" value="4" />
Week5 <input type="checkbox" id="Week5" name="row2[]" value="5" />
Week6 <input type="checkbox" id="Week6" name="row2[]" value="6" />
Week7 <input type="checkbox" id="Week7" name="row2[]" value="7" />
Week8 <input type="checkbox" id="Week8" name="row2[]" value="8" />
</div><br/><br/>
<div style='border:1px solid black; display:inline-block;'>
Row 3<br/>
Week1 <input type="checkbox" id="Week1" name="row3[]" value="1" checked="checked" />
Week2 <input type="checkbox" id="Week2" name="row3[]" value="2" />
Week3 <input type="checkbox" id="Week3" name="row3[]" value="3" checked="checked" />
Week4 <input type="checkbox" id="Week4" name="row3[]" value="4" />
Week5 <input type="checkbox" id="Week5" name="row3[]" value="5" />
Week6 <input type="checkbox" id="Week6" name="row3[]" value="6" />
Week7 <input type="checkbox" id="Week7" name="row3[]" value="7" />
Week8 <input type="checkbox" id="Week8" name="row3[]" value="8" />
</div><br/><br/>
<div style='border:1px solid black; display:inline-block;'>
Row 4<br/>
Week1 <input type="checkbox" id="Week1" name="row4[]" value="1" checked="checked" />
Week2 <input type="checkbox" id="Week2" name="row4[]" value="2" />
Week3 <input type="checkbox" id="Week3" name="row4[]" value="3" checked="checked" />
Week4 <input type="checkbox" id="Week4" name="row4[]" value="4" />
Week5 <input type="checkbox" id="Week5" name="row4[]" value="5" checked="checked" />
Week6 <input type="checkbox" id="Week6" name="row4[]" value="6" />
Week7 <input type="checkbox" id="Week7" name="row4[]" value="7" />
Week8 <input type="checkbox" id="Week8" name="row4[]" value="8" />
</div><br/><br/>
<div style='border:1px solid black; display:inline-block;'>
Row 5<br/>
Week1 <input type="checkbox" id="Week1" name="row5[]" value="1" checked="checked" />
Week2 <input type="checkbox" id="Week2" name="row5[]" value="2" />
Week3 <input type="checkbox" id="Week3" name="row5[]" value="3" checked="checked" />
Week4 <input type="checkbox" id="Week4" name="row5[]" value="4" />
Week5 <input type="checkbox" id="Week5" name="row5[]" value="5" checked="checked" />
Week6 <input type="checkbox" id="Week6" name="row5[]" value="6" />
Week7 <input type="checkbox" id="Week7" name="row5[]" value="7" checked="checked" />
Week8 <input type="checkbox" id="Week8" name="row5[]" value="8" />
</div><br/><br/>
<input name="update" type="submit" id="update" value="Update">
</form>
edit
I came back to this answer to reference something and I just realized I was re-using ID values for the checkboxes. It wasn't a problem in this situation but it is always bad practice. Each checkbox should have an ID that is unique on the page. So.... it should really be id="row1week1", id="row1week2"... id="row8week1"... id="row8week8".
Related
I have an HTML form that I need to then reference to in PHP, so that I can eventually filter through data. Right now it is just echoing a bit of text for testing purposes.
I use the $_GET variable to get which values are equal to what, then use an if/else statement to tell me if each is checked.
If I say it only equals one value (== .25) it returns false.
However if I add one more or both more values (== .25 or .375 or .5) it will return the value I need.
How can I get it to return true with only one value?
<table stlye="width:100%">
<tr>
<td style="width:50%">
<form method="GET">
Tool Diameter: <br>
<input type="checkbox" name="Tool Diameter" value=.25 checked> .25<br>
<input type="checkbox" name="Tool Diameter" value=.375 checked> 3/8<br>
<input type="checkbox" name="Tool Diameter" value=.5 checked> 1/2<br><br>
Brand: <br>
<input type="checkbox" name="Brand" value="Lakeshore Carbide " checked> Lakeshore Carbide<br>
<input type="checkbox" name="Brand" value="AB Tools" checked> AB Tools<br>
<input type="checkbox" name="Brand" value="Helical Tools" checked> Helical Tools<br><br>
Flutes: <br>
<input type="checkbox" name="Flutes" value="2" checked> 2<br>
<input type="checkbox" name="Flutes" value="3" checked> 3<br>
<input type="checkbox" name="Flutes" value="4" checked> 4<br><br>
Tool Material: <br>
<input type="checkbox" name="Material" value="HSS" checked> HSS<br>
<input type="checkbox" name="Material" value="Carbide" checked> Carbide<br>
<input type="checkbox" name="Material" value="Cobalt" checked> Cobalt<br><br>
Coating: <br>
<input type="checkbox" name="Coating" value="Uncoated" checked> Uncoated<br>
<input type="checkbox" name="Coating" value="ZrN" checked> ZrN<br>
<input type="checkbox" name="Coating" value="TiCN" checked> TiCN<br><br>
Tool Type: <br>
<input type="checkbox" name="Type" value="Face Mill" checked> Face Mill<br>
<input type="checkbox" name="Type" value="Flat Endmill" checked> Flat Endmill<br>
<input type="checkbox" name="Type" value="Ball Endmill" checked> Ball Endmill<br>
<br><button>Filter</button><br>
</form>
</td>
<td style="width:50%">
<style type="text/css">
td
{
padding:0 50px 0 50px;
}
</style>
<?php
//while (true){
if ($_GET['Tool Diameter'] == .375) {
echo 'test = true';
}
else {
echo "false";
}
?>
</td>
</tr>
</table>
Convert the values to strings e.g.
<input type="checkbox" name="Tool Diameter" value=".375" checked> .375<br>
then check
if ($_GET['Tool Diameter'] == ".375"){
enter code here
}
try this
<input type="checkbox" name="Tool Diameter[]" value=".25" checked> .25<br>
<input type="checkbox" name="Tool Diameter[]" value=".375" checked> 3/8<br>
<input type="checkbox" name="Tool Diameter[]" value=".5" checked> 1/2<br><br>
.....
<?php
// for checking the condition 'atleast 1 ' should be checked
if(sizeof($_GET['Tool Diameter']) >=1){
echo 'test = true';
}
else {
echo "test = false";
}
?>
1st mistake
values have to be wrapped in parenthesise value=".25"
2nd mistake
names have to be unique, as a result you have only one value in the array $_GET['Tool Diameter']
you should have a slot in $_GET that contains i.e. array of your results, so lets say
$_GET['Dimensions'] = [
'Dimension 1' => '.25',
'Dimension 2' => '.375',
'Dimension 3' => '.5'
];
and then refer to each of them separately
I got a list of 20 questions with A or B answer, and for the result I need to count certain answers together to get a score, and its A and B mixed.
Question 1 <input type="radio" name="1" value="A" />
<input type="radio" name="1" value="B" />
Question 2 <input type="radio" name="2" value="A" />
<input type="radio" name="2" value="B" />
Question 3 <input type="radio" name="3" value="A" />
<input type="radio" name="3" value="B" />
$result1 = $1A + $2B + $4A + $7B etc (count total of these answers)
$result2 = $3B + $5B + $6B etc
the results should be numeric, with checkboxes it would be easy because of unique names so values can be 1, but with radios I don't know how other then a lot of 'if 1=B then $var++', but i'm sure there is an easier way.
Here you go:
<?php
var_dump($_POST);
/*
#param array $userInput - just the $_POST
#param array $expected must be in format : [
[1,'A'],
[2,'B']
]
#return int
*/
function countExpectedAnswers(array $userInput, array $expected) /*: int*/{
$result = 0;
foreach($expected as $ex){
if(isset ($userInput[$ex[0]]) && $userInput[$ex[0]] === $ex[1]){
$result ++;
}
}
return $result;
}
$expectedAnswers = [
[1,'A'],
[2,'A'],
[3,'B']
];
echo 'Answered correctly: ' . countExpectedAnswers($_POST,$expectedAnswers);
?>
<form method = 'post'>
<div>
Question 1 <input type="radio" name="1" value="A" >
<input type="radio" name="1" value="B" >
</div>
<div>
Question 2 <input type="radio" name="2" value="A" >
<input type="radio" name="2" value="B" >
</div>
<div>
Question 3 <input type="radio" name="3" value="A" >
<input type="radio" name="3" value="B" >
</div>
<input type = 'submit' value = 'go'>
</form>
I want to make a timetable with, Codeigniter
but I have a problem with looping.
My view
Class
`<input type="checkbox" name="class[]" value="1A" id="1a" /> 1A
<input type="checkbox" name="class[]" value="1B" id="1b" /> 1B
<input type="checkbox" name="class[]" value="1C" id="1c" /> 1C `
Hours
<input type="checkbox" name="hours[]" value="1" id="h1" /> hours 1
<input type="checkbox" name="hours[]" value="2" id="h2" /> hours 2
<input type="checkbox" name="hours[]" value="3" id="h3" /> hours 3
<input type="checkbox" name="hours[]" value="4" id="h4" /> hours 4
<input type="checkbox" name="hours[]" value="5" id="h5" /> hours 5
<input type="checkbox" name="hours[]" value="6" id="h6" /> hours 6
Day
`<input type="checkbox" name="day[]" value="1" id="Monday" /> Monday
<input type="checkbox" name="day[]" value="2" id="Tuesday" /> Tuesday
<input type="checkbox" name="day[]" value="3" id="Wednesday" /> Wednesday
<input type="checkbox" name="day[]" value="4" id="Thursday" /> Thursday
<input type="checkbox" name="day[]" value="5" id="Friday" /> Friday
<input type="checkbox" name="day[]" value="6" id="Saturday" /> Saturday `
room
` A
<input type="checkbox" name="room[]" value="B" id="b" /> B
<input type="checkbox" name="room[]" value="C" id="c" /> C
<input type="checkbox" name="room[]" value="C" id="d" /> D `
my model
function add(){
$day= $this->input->post('day');
$hours= $this->input->post('hours');
$class= $this->input->post('class');
$room= $this->input->post('room');
foreach ($hoursas $hr){
$data = array('day'=>$day,'hours'=>$hr,'class'=>$class,'room'=>$room);
$this->db->insert('t_jadual', $data);
}
}
how do I loop $class,$room and $day?
i want insert this form value to datanase :
<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>
these text box are get by php from a table in database and may be Variable
i want insert to database by this format
if brand 1 are checked $brand="1,";
and Finally like this :
insert($name,$brands); and $brands = "1,2,3,4,5,";
if write this by if and while but it doesn't work because if insert run in while {} Five times insert Done and if insert run out of while {} , $brand = "5,"
thanks for your help or idea for this problem
it's mean :
<form method="post" action="#">
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
?>
<input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
<?php }} ?>
Source Output:
<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>
when submit form , insert source is :
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = brand.stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
$brand_ids = "brand.$brand_id";
if($$brand_ids==1) {$brands="$brandid,"}
}} ?>
$db->add_submenu("$brands");
You should change the name of your checkboxes to brand[]. It will give you an array once submitted at $_POST['brand']
Ex.
<input type="checkbox" name="brand[]" value="1" ... />
<input type="checkbox" name="brand[]" value="2" ... />
<input type="checkbox" name="brand[]" value="3" ... />
<input type="checkbox" name="brand[]" value="4" ... />
<input type="checkbox" name="brand[]" value="5" ... />
on the other side you can either do something like the following:
// this will return '1, 2, 3, 4, 5' when all are selected.
$index = implode(", ", $_POST['brand']);
and at that point you will have the brands in comma delimited form.
I know this should be an easy one, but I'm failing to make it work.
I have the following form:
<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" />Blade PR-11 <br />
</form>
After submit I want to display the form again and to check the checkboxes that the user has selected before submission. My language of choice is PHP.
Thanks.
Something like this should work:
<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" <?=(in_array("2", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" <?=(in_array("5", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" <?=(in_array("10", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" <?=(in_array("1", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" <?=(in_array("66", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade PR-11 <br />
</form>
If I select the first three checkboxes, then $_POST will be:
Array ( [blades] => Array ( [0] => 2 [1] => 5 [2] => 10 ) )
If I select the first, third and fifth checkboxes:
Array ( [blades] => Array ( [0] => 2 [1] => 10 [2] => 66 ) )
This was done with:
print_r($_POST);
As you can see, $_POST['blades'] is an array with each selected value.
My full testing code was:
<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" />Blade PR-11 <br />
<input type="submit">
</form>
<?php print_r($_POST); ?>
<?php $blades = $_POST["blades"]?>
<form action="" method="post">
<input type="checkbox" name="blades[]" <?php if($blades[0]==2):?>checked="checked"<?php endif?> value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" <?php if($blades[1]==5):?>checked="checked"<?php endif?> value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" <?php if($blades[2]==10):?>checked="checked"<?php endif?> value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" <?php if($blades[3]==1):?>checked="checked"<?php endif?> value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" <?php if($blades[4]==66):?>checked="checked"<?php endif?> value="66" />Blade PR-11 <br />
</form>