doutang0335
doutang0335
2016-02-01 09:38
浏览 56

使用循环在php页面中显示html复选框多个复选框内容

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douman6679
    douman6679 2016-02-01 09:41
    <?php echo "$f6","<br>"; ?>
    

    wrong concatenation

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

    use this

    点赞 评论
  • douren6035
    douren6035 2016-02-01 09:47

    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> 
    
    点赞 评论
  • dtd14883
    dtd14883 2016-02-01 09:48

    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.

    点赞 评论
  • douhu1990
    douhu1990 2016-02-01 10:02

    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

    </div>
    
    点赞 评论

相关推荐