0
//I extracted data from database like
<form action="print.php" method="post">
 <?php include('connection.php') ?>
                      <?php

// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia"); 
$num = mysql_numrows($sql);
?>

<?php

$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");                  
   <input type="checkbox" name="list[]"  value="<?php echo "$f6";?>" /><?php echo "$f6","<br>"; ?>//display result in htmlpage

 <?php
$i++;
$d=$f6;
}

?>

<input type="submit" value="submit"/>   
</form> 

print.php
//after submitting in php page 

<?php include('connection.php') ?>
                      <?php


if(isset($_POST['submit']))
 {
if(!empty($_POST['list']))//name of checkbox in html
  {
foreach($_POST['list'] as $selected){
echo $selected."</br>";
}
}
}

?>

 //display nothing in php...how I solve this problem

I want to display content of checkbox from html form to php page.I extracted the checkbox contents from database.problem is how I display the content in php page selecting multiple checkbox.

2
  • you should ON your error_reporting. Commented Feb 1, 2016 at 9:40
  • if u got your solution from listed below answer than chose the best answer and mark as accepted. meta.stackexchange.com/questions/5234/… Commented Feb 3, 2016 at 9:37

4 Answers 4

1

First of all add error_reporting in your code:

ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);

You have few issues in your HTML/PHP combination:

Issues:

  1. You are using <input type="checkbox" .. inside the PHP.
  2. your concatenation is wrong "$f6","<br>"
  3. Also need to add name for like name="submit" in button.

Modified Code:

<?php
$i=0;
while ($i < $num) {
    $f6=mysql_result($sql,$i,"item_name");                  
    ?>
    <input type="checkbox" name="list[]"  value="<?php echo $f6;?>" /><?php echo $f6; ?><br/>
<?php
    $i++;
    $d = $f6;
}

?>

Side note:

Suggestion of error_reporting is only for development and staging not for production.

Please use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.

Sign up to request clarification or add additional context in comments.

Comments

0
<?php echo "$f6","<br>"; ?>

wrong concatenation

<?php echo "$f6"."<br>"; ?>

use this

Comments

0

Your code should be:-

 <?php 
    // This line should be first.
    // You have missed semicolon here.
     include('connection.php'); 

        // Query member data from the database and ready it for display
        $sql = mysql_query("SELECT * FROM nokia"); 
        $num = mysql_numrows($sql);
    ?>
    <form action="print.php" method="post">
    <?php
    $i=0;
    while ($i < $num) {
    $f6=mysql_result($sql,$i,"item_name");  
?>  
       <!--  You should write below line as  -->              
       <input type="checkbox" name="list[]"  value="<?php echo $f6;?>" /><?php echo $f6; ?> </br>
    <?php
        $i++;
        $d=$f6;
    }
    ?>

    <input type="submit" value="submit"/>   
    </form> 

2 Comments

add php closing tag after // You should write below line as
@monace19: Thanks for your suggestion. I have missed it ;)
0

there are couple of errors in your codes. please find the modified codes below with my comment started with //seeyouu:

//I extracted data from database like
<form action="print.php" method="post">
 <?php include('connection.php');

// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia"); 
$num = mysql_num_rows($sql);//seeyouu: used wrong function>>mysql_numrows

$i=0; //seeyouu: after my amendment, probably can remove this
while ($row = mysql_fetch_array($sql)) {
//seeyouu: no such way of doing, your $sql already a result >> $f6=mysql_result($sql,$i,"item_name"); 
//seeyouu: forgot to close your php tag here
?>
   <input type="checkbox" name="list[]"  value="<?php echo $row["item_name"]; ?>" />
<?php //seeyouu: wrong>>echo "$f6","<br>"; 
//correct way: 
echo $row["item_name"]."<br />";

//$d=$f6;i don't know what is this line for, so i leave it to you.
}

?>

<input type="submit" name="submit" value="submit"/>   <!-- seeyouu: forgot to set name: submit -->
</form> 

print.php
//after submitting in php page 

<?php include('connection.php');

if(isset($_POST['submit']))
 {
  if(!empty($_POST['list']))//name of checkbox in html
  {
    foreach($_POST['list'] as $selected)
    {
      echo $selected."</br>";
    }
  }
 }

?>

 //display nothing in php...how I solve this problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.