注意:使用isset时未定义的索引

时间:2019-03-10 11:55:09

标签: javascript php jquery mysqli

我正在尝试创建客户门户(还在学习中)。

当用户要填写一些输入表格时,我想将这些值存储在数据库中。客户的好处是,如果他们想填写更多行,他们可以添加多行。我将它们存储在一个数组中。

var count = 1;

$(".add").click(function () {

    $(".containerAdd").append(addNewRow(count));
    count++;
});

function addNewRow(count) {
    var newrow =
        '<div class="row">' +
        '<input type="text" class="amount" placeholder="Aantal" />' +
        '<select class="unit" required="true">' +
        '<option id="euroPallet" value="1.4">Europallet</option>' +
        '<option id="chepPallet">Cheppallet</option>' +
        '<option id="blokPallet">Blokpallet</option>' +
        '</select>' +
        '<input type="text" class="length" placeholder="Lengte (cm)" />' +
        '<input type="text" class="width" placeholder="Breedte (cm)" />' +
        '<input type="text" class="height" placeholder="Hoogte (cm)" />' +
        '<a href="#" class="remove">' +
        '<i class="material-icons icon">' +
        'remove' +
        '</i>' +
        '</a>' +
        '</div>' +
        '<br>';
    return newrow;
}
.row {
    display: -ms-flexbox;
    /* IE10 */
    display: flex;
    width: 100%;
    margin-bottom: 0px;
}

.icon {
    padding: 6px;
    background: #00B3FD;
    color: white;
    min-width: 50px;
    text-align: center;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Cotrans Portal</title>

    <!-- Cotrans CSS -->
    <link rel="stylesheet" href="style/style.css">

    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">

    <!-- Google Icons -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

    <!-- JQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script>
        $(document).ready(function(e) {
            //Variables
            var html = '<p /><div><input type="text" name="amount[] id="childamount" class="amount" placeholder="Aantal" /> <select name="unit[]" class="unit" required="true" id="childunit"> <option name="euroPallet[]" id="childeuroPallet" value="1.4">Europallet</option> <option name="chepPallet[]" id="childchepPallet">Cheppallet</option> <option name="blokPallet[]" id="childblokPallet">Blokpallet</option> </select> <input type="text" name="length[]" id="childlength" class="length" placeholder="Lengte (cm)" /> <input type="text" name="width[]" id="childwidth" class="width" placeholder="Breedte (cm)" /> <input type="text" name="height[]" id="childheight" class="height" placeholder="Hoogte (cm)" /> <a href="#" id="remove"> <i class="material-icons icon">clear</i></a></div>';
            
            
            var maxRows = 5;
            var x = 1;
            
            //Rij toevoegen
            $("#add").click(function(e) {
                if(x <= maxRows) {
                $("#form-group").append(html);  
                    x++;
                }
                 
            });
            
            //Rij verwijderen
            $("#form-group").on('click','#remove',function(e) {
                $(this).parent('div').remove();
                x--;
            });
        });
    </script>
</head>
<body>
<div class="form-group" id="form-group">
	<form name="add_row" id="add_row" method="post">
		<div class="row" id="row">
			<input type="text" name="amount[]" id="amount" class="amount" placeholder="Aantal" />
			<select class="unit" required="true" id="unit">
				<option name="ValueA[]" id="ValueA">Value A</option>
				<option name="ValueB[]" id="ValueB">Value B</option>
				<option name="ValueC[]" id="ValueC">Value C</option>
			</select>
			<input type="text" name="length[]" id="length" class="length" placeholder="Lengte (cm)" />
			<input type="text" name="width[]" id="width" class="width" placeholder="Breedte (cm)" />
			<input type="text" name="height[]" id="height" class="height" placeholder="Hoogte (cm)" />
			<a href="#" id="add">
				<i class="material-icons icon">
                                    add
				</i>
			</a>
		</div>
	</form>
</div>
<body>

现在我想将这些值存储在数据库中,但出现错误“通知:未定义索引:第16行的C:\ xampp2 \ htdocs \ portal \ index.php中的金额 警告:在第16行“ ans”中,为C:\ xampp2 \ htdocs \ portal \ index.php中的foreach()提供了无效的参数,直到使用isset

<?php
$output = NULL;

// Connect DB

$mysqli = NEW MySQLi('localhost', 'portal', '', 'portal');

if (isset($_POST["submit"]))
{

//    $amount = $_POST["amount"];
//    $unit = $_POST['unit'];
//    $length = $_POST['length'];
//    $width = $_POST['width'];
//    $height = $_POST['height'];

foreach($_POST["amount"] AS $key => $value)
    {
    $sql = "INSERT INTO portalform(amount) VALUES ('" . $value . "')";
    $mysqli->query($sql);
    }

$mysqli->close();
}

?>

我在做什么错了?

谢谢。

0 个答案:

没有答案