如何使用PHP在具有多个文本框的表单中从select标记中获取值?

时间:2018-12-09 04:44:30

标签: php html

这是我的示例代码,它接受来自select标记和文本框的输入。问题是,我在Google上看到的大多数示例仅在表单标签内使用单个选择标签,并将数据发布到php变量中。我需要同时获取这两个值到查询中。我应该使用javascript来获取select标记的值,还是有任何方法可以将文本框中的数据和select标记以单一形式发布?

employee.php

     <form name = "AddEmployee"method="post" action ="php/employeetabsubmit.php" >      
            <input type="text" name="surname" id = "surname" placeholder="Surname Here">
            <select name="empStanding">
              <option value="ADMIN">ADMIN</option>
              <option value="EMPLOYEE">EMPLOYEE</option>
              <option value="SUPERVISOR">SUPERVISOR</option>

            </select> 
             <button class="add" type="submit" name="add">Add</button>
       </form>

employeetabsubmit.php

   <?php

    $conn=mysqli_connect('localhost','root','','employee');
    if(!$conn){
    die('Could not Connect My Sql:' .mysql_error());
    }

    else{

        echo '<script>console.log("Connected to DATABASE")</script>';
    }


     if (isset($_POST['add'])){
      $surname_string = mysqli_real_escape_string($conn, $_POST['surname']);//sample input
      $employee_standing = $_POST['empStanding'];

      $sql="INSERT INTO employee_table (emp_surname, emp_standing) VALUES ('$surname_string','$employee_standing');


       if (!mysqli_query($conn,$sql)) {
          die('Error: ' . mysqli_error($conn));
        }
        echo "1 record added";

        mysqli_close($conn);
     }
    ?>

2 个答案:

答案 0 :(得分:1)

将按钮置于select标签之外,它应该可以工作-其他所有内容都正确

答案 1 :(得分:0)

在表单属性中添加enctype =“ multipart / form-data”。

   <form name = "AddEmployee"method="post" action ="php/employeetabsubmit.php" enctype="multipart/form-data">      
        <input type="text" name="surname" id = "surname" placeholder="Surname Here">
        <select name="empStanding">
          <option value="ADMIN">ADMIN</option>
          <option value="EMPLOYEE">EMPLOYEE</option>
          <option value="SUPERVISOR">SUPERVISOR</option>

        </select> 
         <button class="add" type="submit" name="add">Add</button>
   </form>