如何从所选复选框向表中插入数据?

时间:2017-10-04 08:46:15

标签: php forms checkbox

我正试图通过从一个或两个复选框中选择它来确定名称和小时在哪个表格中的方式。

例如,如果我有一个新名称和小时,我可以选择是通过复选框选择“标准”还是“高级”或两者都是。

#myform .plus, .minus {
  text-decoration: none;
  color: #fff;
  background-color: #fdd818;
  text-align: center;
  letter-spacing: .5px;
  -webkit-transition: .2s ease-out;
  transition: .2s ease-out;
  cursor: pointer;
}

.collapsible-header{
  color: #ffffff;
}



.row{
	margin: 0;	
	width: 100%;
}
.uren{
	display: -webkit-inline-box;
    padding-left: 10px;
    padding-right: 10px;
}
.btn {
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  background: #ffcc00;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}
.btn:hover {
  background: #dbaf00;
  text-decoration: none;
}


/* Pop up */
.letspop{
  background-color: #fff;
  width: 120px;
  display: block;
  margin: 5% auto;
  padding: 20px;
  position: relative; 
  text-align: center;
  float: right;
  margin-right: 20px;
  margin-top: 0px;
}
.letspop:hover{
  cursor: pointer;
}
.overlay{
  display: none; /* Default Hidden */
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1;
  background-color: rgba(0,0,0,0.6);
  height: 100%;
  width: 100%;
} 
.popup{
  display: none; /* Default Hidden */
  position: fixed;
  left: 50%; 
  top: 5%;
  z-index: 2;
  background-color: #fff;
  height: 450px;
  width: 300px;
  overflow: hidden;
  padding: 40px;
  transform: translate(-50%, 0);
}
#new_module{
  width: 195px;
}

h1, h2{
  color: steelblue ;
  font-family: sans-serif;
  text-align: center;
  text-transform: uppercase;
  line-height: 1;
}
h2{
  color: dodgerblue ;
}
small{
  color: #444 ;
  font-size:0.4em;
  display: block;
  margin: 0.2rem auto 0;
}
.close{
  position: absolute;
  top: 8px;
  right: 8px;
  display: block;
  color: #666666;
  font-family: sans-serif;
}
.close:hover{
  cursor: pointer;
  color: #444444;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" rel="stylesheet"/>
    <form action="#" method="POST">
      <div class="row">
        <div class="input-field col s6">
          <input id="new_module" type="text" class="validate">
          <label class="active" for="new_module">Name</label>
        </div>
      </div>
      <form action="#">
        <p class="range-field">
          <input type="range" id="test" min="0" max="20" />
        </p>
      </form>
      <p>
        <input type="checkbox" class="filled-in" id="standard"/>
        <label for="standard">Standard</label>
      </p>
      <p>
        <input type="checkbox" class="filled-in" id="advanced"/>
        <label for="advanced">Advanced</label>
      </p>    
      <hr>
        <button class="btn waves-effect waves-light" type="submit" name="action">
          <i class="material-icons right">send</i>
        </button>
      <div class="close">X</div>
    </form>

2 个答案:

答案 0 :(得分:0)

您需要为要提交给服务器的html元素设置名称。 尝试以下更改:

<form action="checkbox.php" method="post">
        <p>
            <input type="checkbox" class="filled-in" id="standard" name="chk[]" value="standard"/>
            <label for="standard">Standard</label>
          </p>
          <p>
            <input type="checkbox" class="filled-in" id="advanced" name="chk[]" value="advanced"/>
            <label for="advanced">Advanced</label>
          </p>  
          <input type="submit" />
    </form>

并尝试使用以下php代码查找所选复选框

foreach($_POST['chk'] as $key => $chk){
  if($chk == 'standard'){
      // do some code
  }
if($chk == 'advanced'){
      // do some code
  }
}

答案 1 :(得分:-1)

快速回答开始帮助你。

在你的SQL操作中执行“indatabase.php”。之后,执行以下操作:

<form action="/indatabase.php" method="POST">

见:

  

https://stackoverflow.com/a/22836074/8695939

如果您想要更高的安全性,请改用PHP PDO。
你可以在这里找到sqli和PDO例子:

  

https://www.w3schools.com/php/php_mysql_insert.asp