inserting multiple checkboxes names with codeigniter in to mysql - php

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?

Related

Get values from Dynamically created textboxes and checkboxes linked to textbox

I want to know how to capture values of dynamically created textboxes and checkboxes linked to each other.
My HTML elements would be like this:
<div class="data_class">
<input class="textbox_class" name="textbox[]" type="text" />
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
</div>
The checkboxes are linked to input with type="text"(i.e textbox)
Through JQuery i can clone the HTML elements in the "data_class" and append into it.
But while posting, how can we get the checkbox values related to each textbox.
i.e if i create 5 such elements like:
<div class="data_class">
<input class="textbox_class" name="textbox[]" type="text" value="A"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="B"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="C"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="D"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="E"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
</div>
How to get values that are checked for textbox with value "A", "B", "C", "D", "E" seperatly???
What I would do is to group them by sets, meaning:
One set given A:
A textbox, and 1 or more checkboxes:
So I would do it create a name="" attribute that would group them accordingly. Example:
This would be the initial markup:
<form method="POST">
<div class="data_class">
<div class="fieldset" data-group="A">
<input class="textbox_class" name="form_values[A][textbox]" type="text" />
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="1" />1
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="2" />2
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="3" />3
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="4" />4
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="5" />5
</div>
</div>
<button type="button" id="spawn">Spawn More</button><br/>
<input type="submit" name="submit" />
</form>
Spawning other fieldsets:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#spawn').on('click', function(){
var $initial = $('div.fieldset:last').clone();
// or just increment a number instead
var next = String.fromCharCode($initial.attr('data-group').charCodeAt(0) + 1);
$initial.children('.textbox_class').attr('name', 'form_values['+next+'][textbox]');
$initial.children('.checkbox_class').attr('name', 'form_values['+next+'][checkbox][]');
$('.data_class').append('<div class="fieldset" data-group="'+next+'">'+$initial.html()+'</div>');
});
});
</script>
So in PHP, after submission, it would come up with something like this:
if(isset($_POST['submit'])) {
$values = $_POST['form_values'];
echo '<pre>';
print_r($values);
echo '</pre>';
}
Output:
Array
(
[A] => Array
(
[textbox] => test1
[checkbox] => Array
(
[0] => 1
[1] => 2
)
)
[B] => Array
(
[textbox] => test2
[checkbox] => Array
(
[0] => 4
[1] => 5
)
)
)
Sample Demo
Note: This is just an example, if you want a simple key, just use a numeric one instead.

Updating SQL Database With Checkbox Values

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".

Undefined index in every part? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I have spent around three days on this problem and have no idea how to fix it i'm getting...
Notice: Undefined index: surname in C:\xampp\htdocs\Locus.php on line 25
Notice: Undefined index: prison in C:\xampp\htdocs\Locus.php on line 26
Notice: Undefined index: NI in C:\xampp\htdocs\Locus.php on line 27
Notice: Undefined index: Q1 in C:\xampp\htdocs\Locus.php on line 28
Notice: Undefined index: Q2 in C:\xampp\htdocs\Locus.php on line 29
Notice: Undefined index: Q3 in C:\xampp\htdocs\Locus.php on line 30
Notice: Undefined index: Q4 in C:\xampp\htdocs\Locus.php on line 31
Notice: Undefined index: Q5 in C:\xampp\htdocs\Locus.php on line 32
....etc
here is my HTML...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="Locus.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Locus of control test</title>
<link href="Locus.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="Locus.php" id="Locus" method="post" name="Locus" onsubmit="return validateForm()"
<h4>Surname: <input type="text" name="surname"/> <br/>Prison: <input type="text" name="prison"/><br/>National Insurance: <input type="text" name="NI"/></h4>
<h3> Please carefully read all the instructions given on the left hand panel.</h3>
<h1 class="Header"> LOCUS OF CONTROL</h1>
<h2>Please click submit ONLY when all questions have been completed <br/> <input name="submit" id="submit" type="submit" value="Check & Submit" /></h2>
<p>1. I can anticipate difficulties and take action to avoid them. <br />
<input type="radio" name="Q1" value="4" /> Always Agree <input type="radio" name="Q1" value="3" /> Agree <input type="radio" name="Q1" value="2" /> Unsure <input type="radio" name="Q1" value="1" /> Disgree <input type="radio" name="Q1" value="0" /> Always Disagree
</p>
<p>2. A great deal of what happens to me is just a matter of chance. <br />
<input type="radio" name="Q2" value="0" /> Always Agree <input type="radio" name="Q2" value="1" /> Agree <input type="radio" name="Q2" value="2" /> Unsure <input type="radio" name="Q2" value="3" /> Disgree <input type="radio" name="Q2" value="4" /> Always Disagree
</p>
<p>3. Everyone knows that luck or chance determines the future. <br />
<input type="radio" name="Q3" value="0" /> Always Agree <input type="radio" name="Q3" value="1" /> Agree <input type="radio" name="Q3" value="2" /> Unsure <input type="radio" name="Q3" value="3" /> Disgree <input type="radio" name="Q3" value="4" /> Always Disagree
</p>
<p>4. I can control my problems only if I have outside support. <br />
<input type="radio" name="Q4" value="0" /> Always Agree <input type="radio" name="Q4" value="1" /> Agree <input type="radio" name="Q4" value="2" /> Unsure <input type="radio" name="Q4" value="3" /> Disgree <input type="radio" name="Q4" value="4" /> Always Disagree
</p>
<p>5. When I make plans I am almost certain I can make them work. <br />
<input type="radio" name="Q5" value="4" /> Always Agree <input type="radio" name="Q5" value="3" /> Agree <input type="radio" name="Q5" value="2" /> Unsure <input type="radio" name="Q5" value="1" /> Disgree <input type="radio" name="Q5" value="0" /> Always Disagree
</p>
<p>6. My problems will dominate all my life. <br />
<input type="radio" name="Q6" value="0" /> Always Agree <input type="radio" name="Q6" value="1" /> Agree <input type="radio" name="Q6" value="2" /> Unsure <input type="radio" name="Q6" value="3" /> Disgree <input type="radio" name="Q6" value="4" /> Always Disagree
</p>
<p>7. My mistakes and problems are my responsibility to deal with. <br />
<input type="radio" name="Q7" value="4" /> Always Agree <input type="radio" name="Q7" value="3" /> Agree <input type="radio" name="Q7" value="2" /> Unsure <input type="radio" name="Q7" value="1" /> Disgree <input type="radio" name="Q7" value="0" /> Always Disagree
</p>
<p>8. Becoming a success is a matter of hard work, luck has little or nothing to do with it. <br />
<input type="radio" name="Q8" value="4" /> Always Agree <input type="radio" name="Q8" value="3" /> Agree <input type="radio" name="Q8" value="2" /> Unsure <input type="radio" name="Q8" value="1" /> Disgree <input type="radio" name="Q8" value="0" /> Always Disagree
</p>
<p>9. My life is controlled by outside actions and events. <br />
<input type="radio" name="Q9" value="0" /> Always Agree <input type="radio" name="Q9" value="1" /> Agree <input type="radio" name="Q9" value="2" /> Unsure <input type="radio" name="Q9" value="3" /> Disgree <input type="radio" name="Q9" value="4" /> Always Disagree
</p>
<p>10. I believe people are victims of circumstances beyond their control. <br />
<input type="radio" name="Q10" value="0" /> Always Agree <input type="radio" name="Q10" value="1" /> Agree <input type="radio" name="Q10" value="2" /> Unsure <input type="radio" name="Q10" value="3" /> Disgree <input type="radio" name="Q10" value="4" /> Always Disagree
</p>
<p>11. To continually manage my problems I need professional help. <br />
<input type="radio" name="Q11" value="0" /> Always Agree <input type="radio" name="Q11" value="1" /> Agree <input type="radio" name="Q11" value="2" /> Unsure <input type="radio" name="Q11" value="3" /> Disgree <input type="radio" name="Q11" value="4" /> Always Disagree
</p>
<p>12. When I am under stress the tightness in my muscles is due to things outside my control.<br />
<input type="radio" name="Q12" value="0" /> Always Agree <input type="radio" name="Q12" value="1" /> Agree <input type="radio" name="Q12" value="2" /> Unsure <input type="radio" name="Q12" value="3" /> Disgree <input type="radio" name="Q12" value="4" /> Always Disagree
</p>
<p>13. 1 believe a person really can be master of his own fate.<br />
<input type="radio" name="Q13" value="4" /> Always Agree <input type="radio" name="Q13" value="3" /> Agree <input type="radio" name="Q13" value="2" /> Unsure <input type="radio" name="Q13" value="1" /> Disgree <input type="radio" name="Q13" value="0" /> Always Disagree
</p>
<p>14. It is impossible to control irregular fast breathing when I am having difficulties.<br />
<input type="radio" name="Q14" value="0" /> Always Agree <input type="radio" name="Q14" value="1" /> Agree <input type="radio" name="Q14" value="2" /> Unsure <input type="radio" name="Q14" value="3" /> Disgree <input type="radio" name="Q14" value="4" /> Always Disagree
</p>
<p>15. I understand why my problems vary so much from one occasion to another. <br />
<input type="radio" name="Q15" value="4" /> Always Agree <input type="radio" name="Q15" value="3" /> Agree <input type="radio" name="Q15" value="2" /> Unsure <input type="radio" name="Q15" value="1" /> Disgree <input type="radio" name="Q15" value="0" /> Always Disagree
</p>
<p>16. I am confident of being able to deal successfully with future problems.<br />
<input type="radio" name="Q16" value="4" /> Always Agree <input type="radio" name="Q16" value="3" /> Agree <input type="radio" name="Q16" value="2" /> Unsure <input type="radio" name="Q16" value="1" /> Disgree <input type="radio" name="Q16" value="0" /> Always Disagree
</p>
<p>17. In my case maintaining control over my problems is mainly due to luck. <br />
<input type="radio" name="Q17" value="0" /> Always Agree <input type="radio" name="Q17" value="1" /> Agree <input type="radio" name="Q17" value="2" /> Unsure <input type="radio" name="Q17" value="3" /> Disgree <input type="radio" name="Q17" value="4" /> Always Disagree
</p>
<p>18. I have often been blamed for events beyond my control. <br />
<input type="radio" name="Q18" value="0" /> Always Agree <input type="radio" name="Q18" value="1" /> Agree <input type="radio" name="Q18" value="2" /> Unsure <input type="radio" name="Q18" value="3" /> Disgree <input type="radio" name="Q18" value="4" /> Always Disagree
</p>
<h5>Please click 'submit' at the start of this test</h5>
</form>
</body>
</html>
and here is my PHP ....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="Locus.css" rel="stylesheet" type="text/css" />
<title>Locus Test Complete</title>
</head>
<body>
<?php
/*variable declaration*/
$username="root";
$password="";
$database_server="localhost";
/*server connection*/
$database="Locus";
mysql_connect($database_server,$username,$password) or die("cannot connect");
/*database connection*/
#mysql_select_db($database) or die( "Unable to select database");
/*create values from Locus to PHP variables*/
$Surname = $_POST['surname'];
$Prison = $_POST['prison'];
$NI = $_POST['NI'];
$Q1 = $_POST['Q1'];
$Q2 = $_POST['Q2'];
$Q3 = $_POST['Q3'];
$Q4 = $_POST['Q4'];
$Q5 = $_POST['Q5'];
$Q6 = $_POST['Q6'];
$Q7 = $_POST['Q7'];
$Q8 = $_POST['Q8'];
$Q9 = $_POST['Q9'];
$Q10 = $_POST['Q10'];
$Q11 = $_POST['Q11'];
$Q12 = $_POST['Q12'];
$Q13 = $_POST['Q13'];
$Q14 = $_POST['Q14'];
$Q15 = $_POST['Q15'];
$Q16 = $_POST['Q16'];
$Q17 = $_POST['Q17'];
$Q18 = $_POST['Q18'];
/*assign the values to the locus of control scores table*/
$query = "INSERT INTO locus of control scores VALUES ('','$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')";
mysql_query($query);
/*create query*/
mysql_query($query);
/*close server connection*/
mysql_close();
?>
</body>
</html>
Can anyone offer any insight as to why i'm getting these notices...?
any help would be greatly appreciated.
You can't reference keys from an array that doesn't have them, and that's what you're trying to do from the $_POST array, since the request didn't have those values posted.
To get rid of the notices, change all of your declarations where you pull values from $_POST directly from:
$Surname = $_POST['surname'];
To:
$Surname = isset( $_POST['surname']) ? $_POST['surname'] : ""; // Some default value
Also, in your code you're inserting the same query twice it looks like. And, VDP brings up a good point, you don't want to insert empty rows, so to fix that, you can wrap the whole logic for inserting with an if statement, check if the $_POST is empty, or check if every variable has a valid value before inserting.
Edit: OK, a more thorough explanation.
When you try to access keys from an array that the array does not have, you get this notice. So, your $_POST array is likely empty, because when you accessed the page, you didn't submit a POST request to it, or if you did, you didn't send the variables it's expecting.
So $_POST looks like:
$_POST = array( );
And you're trying to do:
$Surname = $_POST['surname']; // DOESN'T EXIST!
Clearly, you can't do that. So, instead of directly grabbing things from $_POST, you check if that key actually exists in the $_POST array:
$Surname = isset( $_POST['surname']) ? $_POST['surname'] : "";
However, this is just shorthand for:
if( isset( $_POST['surname'])) {
$Surname = $_POST['surname'];
} else {
$Surname = "";
}
It's called the ternary operator, and you can look it up on the PHP documentation to read more about it.
Instead of
$Surname = $_POST['surname'];
Do
if(isset($_POST['surname']))
{
$Surname = $_POST['surname'];
}
Or
$Surname = (isset($_POST['surname'])) ? $_POST['surname'] : $some_string; /* or something */
You could also just use extract() for localising all those post variables.
If you are COMPLETELY SURE that those notices won't screw your application and want a quick fix use
error_reporting(0);
or use #$variable;
I repeat, this is not really a solution but a temporal quick fix.
For really fixing it you should always check for the existence of the variables.
echo $myvariable; // will output notice
$myvariable = null;
echo $myvariable; // will output nothing but nor the notice
A good way is to use :
if(isset($_POST['myvariable'])){
$myvariable = $_POST['myvariable'];
} else {
$myvariable = null;
}

how to group radio buttons based on same class instead of same name

Is there a way to group the set of radio buttons that share the same class.
I want to be able to be able to check the radio button from each set.
Currently it allows me to check only one radio button (as I know because of same name)
Is there a JQuery way either ?
Example:
SET A
<input type="radio" name="item[]" class="a" value="1"><br>
<input type="radio" name="item[]" class="a" value="2"><br>
<input type="radio" name="item[]" class="a" value="3"><br>
<input type="radio" name="item[]" class="a" value="4"><br>
SET B
<input type="radio" name="item[]" class="b" value="5"><br>
<input type="radio" name="item[]" class="b" value="6"><br>
<input type="radio" name="item[]" class="b" value="7"><br>
<input type="radio" name="item[]" class="b" value="8"><br>
Ok so you are trying to have multiple array entries, but using 1 array.
Since html interprets them as having the same name, you will have to add the numbers into the array key yourself.
So something like this should work.
<input type="radio" name="item[0]" class="a" value="1" />
<input type="radio" name="item[0]" class="a" value="2" />
<input type="radio" name="item[0]" class="a" value="3" />
<input type="radio" name="item[0]" class="a" value="4" />
<input type="radio" name="item[0]" class="a" value="5" />
<input type="radio" name="item[1]" class="b" value="6" />
<input type="radio" name="item[1]" class="b" value="7" />
<input type="radio" name="item[1]" class="b" value="8" />
<input type="radio" name="item[1]" class="b" value="9" />
<input type="radio" name="item[1]" class="b" value="10" />
$('#container').on('click', 'input[type="radio"]', function(){
this.name = this.className;
});​
This should solves it.
Live DEMO
Can you not name the sets differently?
SET A
< input type="radio" name="item1[]" class="a" value="1">
< input type="radio" name="item1[]" class="a" value="2">
< input type="radio" name="item1[]" class="a" value="3">
< input type="radio" name="item1[]" class="a" value="4">
SET B
< input type="radio" name="item2[]" class="b" value="5">
< input type="radio" name="item2[]" class="b" value="6">
< input type="radio" name="item2[]" class="b" value="7">
< input type="radio" name="item2[]" class="b" value="8">

Displaying selected values after form submission

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>

Categories