dongtaihui5131 2015-01-20 21:54
浏览 58

处理使用while循环填充的表单,为每个div分配一个复选框

Here is the code from my submit.php file that has the form mentioned in the title. The if and while statements work and all of this code works the way it looks.

<form action="file.php" method="post">
<?php //database connection stuff
...
if($result->num_rows > 0){
echo '<div class="pure-g">';

               while($row = $result->fetch_assoc()){

                echo '<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
                <fieldset id="' . $row[Name] . '">
                    <h3 class="content-subhead">'
                . $row[Name] . '
            </h3>
            <img src="' . $row[Picture] . '" width="100" height="100">                
            <input type="checkbox" name="check" id="check' . $row[Name] . '" value="' . $row[Name] . ',">
            </fieldset>
            </div>';

               }
echo '</div>';
}
else{
     echo "0 results";
}

?>

        </div>
        <input type="submit" value="Add Selected Games">
    </form>

The problem I'm getting is when I use the submit.php:

$conn = mysql_connect($host, $name, $pass);

if(!$conn){
    echo "cannot connect to server" . mysql_error();
}

$select = mysql_select_db($db);

if(!$select){
    echo "cannot select db" . mysql_error();
}

$list = $_REQUEST['check'];

echo " success $list";

There are currently 3 checkboxes showing on the file.php page that will list the fieldset objects. How should I name the checkbox to have it echo the right $row[Name]? It will echo one $row[Name] based on the a checkbox being checked and newest checkbox (created later in the while loop).

  • 写回答

1条回答 默认 最新

  • dongpiansui8755 2015-01-20 22:06
    关注

    When you just want to see an unformatted list or an array you must convert the array to text.

    $list = var_export($_REQUEST,false);
    echo "sucess $list";
    

    The checkbox value should "1"
    The checkbox name should include the identifier

    <input type="checkbox" name="check' .$row[Name] . '" id="check' .$row[Name] . '" value="1">
    

    When the form is submitted you get the check value like this:

    $list = "<ul>
    ";
    foreach $_POST as $key => $val){   
      if (substr($key,0,5)=='check'){
         $list .= "
    <li>" . substr(($key,5);
       } 
     }
     echo "sucess
    $list
    </ul>";
    

    The Browser only posts the check boxes that are checked. Some Browsers do not pass the Value. The value that is passed with the checkbox key name varies from Browser to Browser.

    I also cleaned up the form routine:

    <?php 
    header('Content-Type: text/html; charset=utf-8');
    header('Connection: Keep-Alive');
    header('Keep-Alive: timeout=50, max=100');
    header('Cache-Control: max-age=120');
    echo <<<EOT
    <!DOCTYPE HTML>
    <html lang="en"><head><title>Title</title><style type="text/css"></style></head><body><div>
    <form action="file.php" method="post">
    EOT;
    
    if($result->num_rows > 0){
      echo '<div class="pure-g">';
    
      while($row = $result->fetch_assoc()){
        $name=$row[`Name`];
        $pic = $row[`Picture`];
        echo <<<EOT
    <div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
    <fieldset id="$name">
    <h3 class="content-subhead">$name</h3>
    <img src="$pic" width="100" height="100">                
    <input type="checkbox" name="check$name" id="check$name" value="1">
    /fieldset>
    </div>
    
    EOT;
      }
    echo '</div>';
    }
    else{
      echo '<p>0 results</p>';
    }
    echo '</div><input type="submit" value="Add Selected Games"></form></body></html>';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上