I'm doing a project where I have a dynamic HTML form. I have a Jquery script that adds new inputs, then I save this data in a data base. This all works perfectly. Now I want to write in an input the value of one mysql field. I give the input his value like this:
<?php
$name = $_SESSION['name'];
?>
<input type="text" name="name[]" value="<? echo $name ?>" />
add_form.js
<script>
$(document).ready(function(e){
var html= '<p /><table><tr><td><input type="text" name="field1[]" id="field1child"></td><td><input type="text" name="field2[]" id="field2child"></td></tr></table>';
$("#add").click(function(e){
$("#container").append(html);
});
});
How can I write value="" with echo $name insied it in the jquery variable? I want to have this name and save it every time I create a new input
save.php
<?php
session_start();
if(!$_SESSION){
header('Location: login.php');
}
if(isset($_POST['submit'])){
$mysqli = NEW MySQLi('localhost','user','pass','database');
$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
foreach($field1 AS $key => $value){
$query = "INSERT INTO facturas (field1,field2)VALUES ('"
. $mysqli -> real_escape_string($value) .
"')
";
$insert=$mysqli->query($query);
}
if($insert){
echo "Data saved";
}
$mysqli->close();
}
?>