how do I insert all the rows returned in the database via serialize in ajax

1

I am making a home page.php that brings information from me a second page function.php via ajax until all normal but in this return a table with the information of the bank and a button to register, in this button I call the function register which is in the first page.php that sends the form via serialize to the page cade.php to insert into another table in the bank but I can only insert the last line, I would have to loop like I would do the codes below:

initial.php:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){
        var teste = $("#teste").val();
        alert(teste);
        $.ajax({
            url: "function.php",
            type: "POST",
            data: {
                teste:teste
            },
            success: function(result){
            alert(result);
            $("#div1").html(result);
        }});
    });
});
</script>
<script>
    function cade(){
        var form = $("#form").serialize();
        var action = "cade";
        alert(form+' / '+action);

         $.ajax({
            url: "cade.php",
            type: "POST",
            dataType: "json",
            data: form,
            success: function(result){
            alert(result);
        }});
    }
</script>
</head>
<body>

<input type="text" id="teste" name="teste" value="" />
<button>Get External Content</button>
<br><br>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
</body>
</html>

function.php:

<?php 
$con = @mysqli_connect("localhost",'root','','test')or die("erro no banco");


$teste = @$_POST['teste'];
$select = @mysqli_query($con,"SELECT * FROM test.teste where serial = '$teste' ") or die ("erro na query 2");

    echo "<form id='form' name='form' method='post' action='' >";
    echo "<table border='1'>";
    echo "<tr>";
    echo "<th>SERIAL</th>";
    echo "<th>EQUIPAMENTO</th>";
    echo "<th>FOTO</th>";
    echo "</tr>";
while($result = mysqli_fetch_array($select)){
    echo "<tr>";
    echo "<td><input type='text' value='".$result['serial']."' id='serial'  name='serial'/></td>";
    echo "<td><input type='text' value='".$result['equipamento']."' id='eq' name='eq' /></td>";
    echo "<td><input type='text' value='".$result['foto']."' id='foto' name='foto' /></td>";
    echo "</tr>";
}
    echo "</table>";
?>
<input type="button" value="Cadastrar" onclick="cade();" />
</form>

cade.php:

<?php 
    $con = @mysqli_connect("localhost",'root','','test')or die("erro no banco");
    $serial = $_REQUEST['serial'];
    $eq = $_REQUEST['eq'];
    $foto = $_REQUEST['foto'];

    print_r(@$_POST);
    $inseri = @mysqli_query($con,"insert into test.nova (serial,equipamento,foto) values ('$serial','$eq','$foto') ") or die ("erro na query");

    //echo "<script>window.history.back();</script>";    
?>
    
asked by anonymous 15.04.2018 / 22:26

1 answer

0

You need to submit form fields as arrays. To do this, include the brackets [] in name s:

name='serial[]'
name='eq[]'
name='foto[]'

Looking like this:

echo "<td><input type='text' value='serial$result' id='serial'  name='serial[]'/></td>";
echo "<td><input type='text' value='equip$result' id='eq' name='eq[]' /></td>";
echo "<td><input type='text' value='foto$result' id='foto' name='foto[]' /></td>";

In PHP, you can make a foreach in the INSERT row to get each value from the index, and include [$i] in the values to insert into the database. Since all arrays are the same size, I have taken the array $serial to make foreach and get indexes in $i :

foreach($serial as $i => $valor){
    $inseri = @mysqli_query($con,"insert into test.nova (serial,equipamento,foto) values ('$serial[$i]','$eq[$i]','$foto[$i]') ") or die ("erro na query");
}
    
15.04.2018 / 23:31