dqyz48470 2016-03-16 16:58
浏览 56

复选框不会将值传递给DB

I have created a contact form on my website using php and some of the values, more specifically the ones that have a checkbox-type input, are not send to my DB. I think there must be something wrong with the way I use the checkboxes or something. Upon submitting a form I get several notices like such:

Notice: Undefined index: ironing in /Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 79

Notice: Undefined variable: POST in /Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 94

The rest of the code seems to work fine and the data is loaded into my DB.

Here is some of my code in the demo-form.php file that I think is wrong:

<h4>i) Cleaningservice:</h4>
     <p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
     <p>Skills: <br>
       <input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
       <input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
       <input type="checkbox" name="laundry" value="yes"> Laundry<br>
       <input type="checkbox" name="ironing" value="yes"> Ironing<br>
       <input type="checkbox" name="window" value="yes"> Windows<br>
     <p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
     </p>

And here is the corresponding demo.php file that connects to the DB and stores the data there:

/* Cleaningservice data is gathered... */

$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];

/* ...and inserted into the table cleaningservice */

$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
        VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";

if (!mysql_query($sql)) {
 die('Error: ' . mysql_error());
}

What am I missing here ?? Thanks in advance

  • 写回答

2条回答 默认 最新

  • du5910 2016-03-16 17:04
    关注

    You must switch between name and values

         <p>Skills: <br>
           <input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
           <input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
           <input type="checkbox" value="laundry" name="yes"> Laundry<br>
           <input type="checkbox" value="ironing" name="yes"> Ironing<br>
           <input type="checkbox" value="window" name="yes"> Windows<br>
         <p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
         </p>
    

    and in PHP side, you will find your selected values in var_dump($_POST['yes'])

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制