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>";
?>
Thanks brother your dirty worked out Thank you very much
– 123testando
You are welcome. I saw that you are new on the site. You should mark in the answer you found more correct.
– Sam
is on the left side of the answer, below the voting arrows. More information is important to have a look at the Tour... Abs!
– Sam