0

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

Alexander
  • 599
  • 2
  • 14
  • 25

6 Answers6

1

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

}
?>
Ajesh VC
  • 635
  • 5
  • 13
1
<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);
    }  
}
?>
0

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
    }
}
?>
TBI
  • 2,789
  • 1
  • 17
  • 21
  • I am not quite sure what this line is doing: if(isset($_POST['submit']))I know what isset function returns, but why do we use the button name – Alexander Jul 15 '14 at 05:28
  • This will check that when you click your submit button then implement your insert query to store/get data. – TBI Jul 15 '14 at 05:46
0
<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);
    }
}
rack_nilesh
  • 553
  • 5
  • 18
0

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

?>
JayKandari
  • 1,228
  • 4
  • 16
  • 33
0

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
}
?>
mconkin
  • 121
  • 7