Skip to main content

inicial.php:

$(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);}});});});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);}});}Get External Content

<!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>

Let jQuery AJAX Change This Text

function.php:

";echo "";echo "";echo "SERIAL";echo "EQUIPAMENTO";echo "FOTO";echo "";while($result = mysqli_fetch_array($select)){echo "";echo "";echo "";echo "";echo "";}echo "";
<?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:

window.history.back();";?>
<?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>";    
?>

inicial.php

$(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);}});});});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);}});}Get External Content

Let jQuery AJAX Change This Text

function.php

";echo "";echo "";echo "SERIAL";echo "EQUIPAMENTO";echo "FOTO";echo "";while($result = mysqli_fetch_array($select)){echo "";echo "";echo "";echo "";echo "";}echo "";?>

cade.php

window.history.back();";?>

inicial.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>";    
?>
Fonte Link

como faço para inserir todas as linhas retornadas no banco via serialize no ajax

estou fazendo uma pagina inicial.php que trás informação de eu uma segunda página function.php via ajax até ai tudo normal porém nesse retorno trás uma tabela com as informações do banco e um botão para cadastrar, nesse botão chamo a função cadastrar que se encontra na primeira pagina inicial.php que envia o formulario via serialize para a pagina cade.php para inserir em uma outra tabela no banco porém só consigo inserir a ultima linha, eu teria que fazer um loop como que eu faria segue os códigos abaixo:

inicial.php

$(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); }}); }); }); 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); }}); } Get External Content

Let jQuery AJAX Change This Text

function.php

"; echo ""; echo ""; echo "SERIAL"; echo "EQUIPAMENTO"; echo "FOTO"; echo ""; while($result = mysqli_fetch_array($select)){ echo ""; echo ""; echo ""; echo ""; echo ""; } echo ""; ?>

cade.php

window.history.back();"; ?>