简体   繁体   English

如何在 HTML 和 PHP 中使用 select 标签

[英]How to work with select tag in HTML and PHP

I made a "productType" select tag.我制作了一个“productType”select 标签。 I added in phpmyadmin but when I click the submit button it gives me an error message我添加了phpmyadmin但是当我点击提交按钮时它给了我一条错误消息

productType is undefined

and if add ?? null如果添加?? null ?? null I'm bypassing the error but the selected value is null in phpmyadmin . ?? null我绕过了错误,但选择的值是 phpmyadmin 中的phpmyadmin Here's my html :这是我的html

<form action="connect.php" method="post">
    <select id="productType" name="productType">
        <option value="">Select Type</option>
        <option value="d">DVD</option>
        <option value="b">Book</option>
        <option value="f">Furniture</option>
    </select>
</form>

And this is my PHP code:这是我的PHP代码:

<?php 
    $productType = $_POST["productType"];

    $conn = new mysqli('localhost', 'root', '', 'test');
    if($conn->connect_error){
        die("Connection Failed: " .$conn->connetion_error);
    }else{
        $stmt = $conn->prepare("Insert into ScandiwebTest(productType) values(?)");
        $stmt->bind_param("s", $productType);
        $stmt->execute();
        echo "Creation was successful!!";
        $stmt->close();
        $conn->close();
    }
?>

How to do you submit your form?你如何提交你的表格? in your snippet there is no submit button to php.在您的代码段中,没有 php 的提交按钮。 The first option should be第一个选项应该是

    <option selected disabled>Select Type</option>

and add a submit button to submit the form并添加提交按钮以提交表单

    <form action="connect.php" method="post" >
        <select id="productType" name="productType">
            <option selected disabled>Select Type</option>
            <option value="d">DVD</option>
            <option value="b">Book</option>
            <option value="f">Furniture</option>
        </select>
        <button type="submit">
            submit
        </button>
    </form>

and result is结果是

array(1) { ["productType"]=> string(1) "b" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM