This question already has answers here:
Getting checkbox values on submit
(10 answers)
Closed 8 years ago.
I have the following code which belongs to a form, I am trying to get the values of the checkbox. The problem is that it was working before, but now that im back to add more features to to my program I get the following error:
Notice: Undefined index: twitter in C:\xampp\htdocs\Dropbox\SportsMedia\proj2final\tw.php on line 3
<input class="twGroup" type="checkbox" name="twitter[]" value="x0" />
<input class="twGroup" type="checkbox" name="twitter[]" value="x1"/>
<input class="twGroup" type="checkbox" name="twitter[]" value="x2"/>
<?php
require_once('db.php');
$selected = $_POST['twitter'];
$img = $_POST['picLink'];
$comments = $_POST['postContent'];
$link = $_POST['postLink'];
foreach ($selected as $key => $value) {
$values = queryTWTable($value);
Please can anyone help me
Try this... You need to use a foreach loop to output your options as follows
<?php
require_once('db.php');
$selected = $_POST['twitter'];
$img = $_POST['picLink'];
$comments = $_POST['postContent'];
$link = $_POST['postLink'];
foreach ($selected as $twitter){
echo $twitter."<br />";//displays your checkbox selections
}
?>
<form method="post" action="index.php">
<input type="checkbox" name="twitter[]" value="x0" /> X0
<input type="checkbox" name="twitter[]" value="x1"/> X1
<input type="checkbox" name="twitter[]" value="x2"/> X2
<input type="submit" name="Submit" value="Check">
</form>
<?php
if(isset($_POST['submit']) && isset($_POST['twitter'])) {
$selectedItems = $_POST['twitter'];
foreach($selectedItems as $key => $value) {
print_r($value);
}
}
?>
You are recieving the undefined index notice because the index 'twitter' of the $_POST array isn't declared (assuming the form is using post). If the form uses get the twitter array would be initialized as an index of $_GET. As other answers suggest, check if that index is set using the php isset method
<?php
If(isset ($_POST ['twitter'] )) {
// your code here
}
?>
Use isset() function,
<?php
require_once('db.php');
if(isset($_POST['submit'])) //submit button name
{
$selected = $_POST['twitter'];
foreach ($selected as $key => $value) {
// your code here
}
}
?>
<input class="twGroup" type="checkbox" name="twitter[]" value="x0" />PHP<br />
<input class="twGroup" type="checkbox" name="twitter[]" value="x1" />HTML<br />
<input class="twGroup" type="checkbox" name="twitter[]" value="x2" />Java<br />
<input type="submit" value="submit" />
<?php
require_once('db.php');
$img = $_POST['picLink'];
$comments = $_POST['postContent'];
$link = $_POST['postLink'];
if ( isset($_POST['twitter']) ){
foreach($_POST['twitter'] as $value)
{
$values = queryTWTable($value);
}
}
You will get it as an array of items.
<?php
$all_checkboxes = $_POST['twitter'];
foreach($all_checkboxes as $check){
echo $check."<br>";
# or do whatever here....
}
?>
Related
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>
if (isset($_POST['approve']))
{
$con=mysql_connect("localhost","root","");
mysql_select_db("shoolin",$con);
$arr=$_POST['check'];
foreach($arr as $selected)
{
echo $selected."</br>";
}
}
i need to print the value of checkbox which is checked,so please suggest some solution to solve this problem of printing value of checkbox
This code shows an error in the foreach loop
Invalid argument supplied for foreach() in C:\xampp\htdocs\xampp\user.php on line 8
Try this way..
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['submit'])) { //to run PHP script on submit
if (!empty($_POST['check_list'])) {
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo $selected."</br>";
}
}
}
?>
The error is saying that $arr is not an array
if (is_array($arr))
{
foreach ($arr as $selected)
{
echo $selected."</br>";
}
}
else
{
echo $arr."</br>";
}
I have a form created by a while loop in php like this :
<form action='#' method='post'>
<input type='hidden' name='form_valid' id='form_valid'>
<?php
$i=-1;
$result_array = array();
//the while is from a simple mysql query
while( $line = $results->fetch() )
{
$i++;
echo"<input type='checkbox' class='checkbox' value='".$line->nid."' id='".$i."'>";
echo $line->title;
echo'<br>';
$result_array[$i] = $line->nid;
}
<input type='submit'>
?>
</form>
Then later on the code I'd like to store the values of the checked checkboxes only in a new array :
if (isset($_REQUEST['form_valid'])) //checking is form is submitted
{
foreach($result_array as $result)
{
if($result == $_REQUEST[$j]) <<<< ERROR
{
$final_array[$j] = $result;
}
}
}
Surprisingly, this code does not work at all.
The error message is "Notice : Undefined offset: 0", then offset 1 then 2 etc ...
The line where the message says theres an error is the marked one.
I really have no idea how to do this. Someone ? =)
Don't try to do it this way, this just makes it hard to process, just use a grouping name array: name="checkboxes[<?php echo $i; ?>]", then on the submission, all values that are checked should simply go to $_POST['checkboxes']. Here's the idea:
<!-- form -->
<form action="" method="POST">
<?php while($line = $results->fetch()): ?>
<input type="checkbox" class="checkbox" name="nid[<?php echo $line->nid; ?>]" value="<?php echo $line->nid; ?>" /><?php echo $line->title; ?><br/>
<?php endwhile; ?>
<input type="submit" name="submit" />
PHP that will process the form:
if(isset($_POST['submit'], $_POST['nid'])) {
$ids = $_POST['nid']; // all the selected ids are in here
// the rest of stuff that you want to do with it
}
This question already has answers here:
Get checked and unchecked checkboxes value
(3 answers)
Closed 8 years ago.
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[<?php echo $group['id']; ?>]" <?php echo $checked; ?>>
<?php
}
?>
Will output:
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[1]">
<input type="checkbox" name="delete_user[2]">
<input type="checkbox" name="delete_user[3]">
<input type="checkbox" name="delete_user[4]" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
And when I check inputs as wanted, then this will process input.
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
is_checked($del); //#todo
}
}
?>
I am looking for way to check if check-box is checked and return proper value ( Y or N ). In this point I declared is_checked() function for this purpose.
I would do it like this:
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[]" value="1">
<input type="checkbox" name="delete_user[]" value="2">
<input type="checkbox" name="delete_user[]" value="3">
<input type="checkbox" name="delete_user[]" value="4" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
Now in PHP you can loop over the $_POST['delete_user'] and it will contain the values of the selected items.
foreach($_POST['delete_user'] as $item)
{
// code to delete $item
}
As the comments already mentioned, only the checked checkboxes will be set
So you either have to take the index as ID or add a val to each input element.
The latter would probably be cleaner (in my opinion)
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[]" <?php echo $checked; ?> value="<?php echo $group['id'];?>">
<?php
}
?>
And the code which does something with the inputs:
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
// do something with $del - the id
}
}
?>
Use the checkbox name just as an array and then give the id as value. See the below code.
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[]" value="<?php echo $group['id']; ?>" <?php echo $checked; ?>>
<?php
}
?>
Then on the action page place this code
<?php
foreach($_POST['delete_user'] as $del) {
is_checked($del); //#todo
}
}
?>
This will work fine.
I have an issue where I need to loop the number of check boxes on a form submit. Foreach check box that is looped I need to then insert data into the database.
How Would I go about looping over the amount of check boxes that have being passed via form submit?
My code is as follows:
Form:
<form action="createChallenge.php" method="post" name="chalCreate">
Challenge Name:<input type="text" name="chalName" />
<br />
Challenge Target:<input type="text" name="chalTarget"/>
<br />
End Date:<input type="text" name="chalDate">
<br />
<!-- Needs a jquery datepicker -->
Select Friends: <br />
<?php
$selFriend = $conn->prepare("SELECT * FROM Friends WHERE UserID = '$userID' AND Friend = 'y' ORDER BY FriendName ASC");
$selFriend->execute();
foreach($selFriend as $row){
?>
<input type="checkbox" name="test" value="<?php echo $row['FriendID'] ?>"><?php echo $row['FriendName'] ?><br>
<?php
}
?>
<br />
<button type="submit">Create Challenge</button>
</form>
PHP to handle the form:
<?php
if(isset($_POST['test']))
{
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
echo $name = $_POST['chalName'];
echo $target = $_POST['chalTarget'];
echo $date = $_POST['chalDate'];
echo $friend = $_POST['test'];
echo $setby = $_COOKIE['userID'];
$create = $conn->prepare("INSERT INTO Challenge ( chalSetBy, chalName, chalTarget, chalDate ) VALUES ('$setby', '$name', '$target', '$date') ");
$create->execute();
if($create)
{
echo "Challenge made successfully";
}
else
{
echo "There was a problem";
}
}
?>
I thought doing the following would echo out data, but it didn't, it only selected the last check box:
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
Make an array of your checkbox in HTML page like as below,
<form name="frm" method="post">
<input type="checkbox" value="1" name="test[]">
<input type="checkbox" value="2" name="test[]">
<input type="checkbox" value="3" name="test[]">
<input type="checkbox" value="4" name="test[]">
<input type="checkbox" value="5" name="test[]">
<input type="checkbox" value="6" name="test[]">
<input type="submit">
</form>
<?php
foreach($_POST['test'] as $key=>$value)
{
echo $value."<br>";
}