insert span values in database with PHP Prepared Statements

这一生的挚爱 提交于 2020-01-16 19:36:48

问题


I have a contact form where I have some checkboxes. After the user submits the form I want to get their entries in the database. I can do this properly, but today I wanted to change my form to be responsive. I changed my simple checkboxes to span tags, but now I can not get the values in the database. It did not give me any error but also did not show anything in the database.

<html>
  <body>
    <form action="" method="POST" >
<section>
  <div class="wrapper-checkbox">

    <div class="checkbox">
      <div class="text">
        <span name="check1" value="rent">rent</span>
      </div>
    </div>

    <div class="checkbox">
      <div class="text"> 
        <span   name="check2" value="buy">buy</span>
      
      </div>
    </div>
  </div>
</section>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js-checkbox/index.js"></script>
      </form>

<?php
$check1 = !empty($_POST['check1']) ? $_POST['check1'] : '';
$check2 = !empty($_POST['check2']) ? $_POST['check2'] : '';
if ($stmt = mysqli_prepare($connect, "INSERT INTO $db_table VALUES (?, ?)"))
{
mysqli_stmt_bind_param($stmt, "ss",$check1,$check2);
?>
   </body>
 </html>

回答1:


You can also done it by using hidden field within a form tag EX-

<input type="hidden" name="check1" value="rent">

and access the value via

<?php
    $check1 = !empty($_POST['check1']) ? $_POST['check1'] : '';
    $check2 = !empty($_POST['check2']) ? $_POST['check2'] : '';
 ?>


来源:https://stackoverflow.com/questions/37795589/insert-span-values-in-database-with-php-prepared-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!