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

Asked

Viewed 31 times

1

I’m making a home page.php that brings me information a second page Function.php via ajax until then everything normal but in this return brings a table with the database information and a button to register, in this button I call the registration function that is in the first page.php that sends the form via serialize to the page Cade.php to insert in another table in the bank but I can only insert the last line, I would have to make a loop like I would follow 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>

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>";    
?>

1 answer

0

You need to send form fields as arrays. To do this, include brackets [] us names:

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

Thus remaining:

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 at the INSERT line to take each value by the index, and include [$i] values to be entered into the database. Since all arrays have the same size, I took the array $serial to do the foreach and take the 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");
}
  • Thanks brother your dirty worked out Thank you very much

  • You are welcome. I saw that you are new on the site. You should mark in the answer you found more correct.

  • is on the left side of the answer, below the voting arrows. More information is important to have a look at the Tour... Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.