I have a recipe book and in this register has a button that clones the fields "quantity of ingredients" and "combo box of ingredients". If I add only a quantity and ingredient, the registration is saved correctly. But the same does not happen with 2 or more ingredients. Any suggestions?
Inputs:
<div id= "clonardiv">
<div class="form-group">
<label class="col-md-4 control-label" for="idMedida">Quantidade:</label>
<div class="col-md-5">
<input id="idMedida" name="idMedida[]" type="text" placeholder="Insira aqui a quantidade de ingredientes necessários" class="form-control input-md" required="">
</div>
</div>
<div class= "form-group">
<label for="ingredientes" class="col-md-4 control-label">Escolha os ingredientes</label>
<select name="ingredientes[]" id="ingredientes" class="col-md-4 control-label" required="1">
<option>Selecione...</option>
<?php
while($dados3 = mysql_fetch_array($q1)) {
?>
<option value="<?= $dados3['id_ingredientes'] ?>">
<?= $dados3['nome'] ?>
</option>
<?php
}
?>
</select>
</div>
</div>
<div id= "clonar" ></div>
<div class="form-group">
<div class="col-md-4">
<input type="button" value="+" onclick="clonar()"; class="btn btn-outlined btn-success" />
</div>
</div>
<br><br><br>
PHP:
<html>
<head>
</head>
<body>
<?php
$titulo= $_POST["idTituloReceita"];
$imagem= $_FILES['imagem']['name'];
$culinaria= $_POST["culinaria"];
$tempopreparo= $_POST["idTempopreparo"];
$porcao=$_POST["idServePorcao"];
session_start();
$emailr = $_SESSION['emailLogin'];
$descricao =$_POST["idPreparo"];
$video = $_POST["idVideo"];
$con = mysqli_connect("localhost", "root", "usbw", "culinaria");
foreach($_POST["idMedida"] as $medida) {
foreach ($_POST["ingredientes"] as $ingredientes){
$sql="INSERT INTO receitas(email,id_tipoculinaria,serve_porcao,tempo_preparo,titulo ,imagem ,video)
VALUES ('$emailr','$culinaria','$porcao','$tempopreparo','$titulo','$imagem','$video')";
$query = $con->query($sql) or die( mysqli_error( $con ) );
$id= $con->insert_id;
//echo "$id";
$sql="INSERT INTO ingredientes_da_receita(id_receitas, id_ingredientes, quantidade, descricao)
VALUES ('$id' ,".$ingredientes.",".$medida.",'$descricao')";
$query = $con->query($sql) or die( mysqli_error( $con ) );
echo "<script>alert('Receita Cadastrada com sucesso');window.location='cadastroreceita.php' </script>";
}
}
mysqli_close($con);
?>
</body>
</html>
controleclone.js:
function clonar() {
var destino = document.getElementById("clonar");
var novadiv = document.createElement("div");
var conteudo = document.getElementById("clonardiv");
novadiv.innerHTML = conteudo.innerHTML;
destino.appendChild(novadiv);
}