0
I have a register of recipes and this register has a button that clones the fields "amount of ingredients" and the "combo box of ingredients". If I add only a quantity and ingredient, the registration is saved correctly, but not 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);
}
can post this clone function()?
– user60252
@Clear leocaracciolo, this in the body of the question.
– Malfus
boy I have to leave now but in your select is missing Multiple see this tutorial http://www.criaweb.com/artigos/180.php
– user60252
@Leocaracciolo I will see, thank you very much
– Malfus
I realized what makes the function clone, so Multiple in select has nothing to do with
– user60252
@Leocaracciolo some other tip?
– Malfus
Some things I didn’t understand. Why a foreach in the recipes table? In this table there is an auto increment field?
– user60252
@Leocaracciolo Cara, was that the problem did not duplicate more but the problem is now in quantity, it does not update correctly. Example: 300 g of rice and 500 g of meat. In the table is something like this: 300 g of rice and meat. What could be?
– Malfus
Let’s go continue this discussion in chat.
– user60252
chat
– user60252