This is my second post on the same issue, in my first post I was using mysql
instead of mysqli
so I have made some updates according to recommendations.
I have a form with variable sets of fields (1 to 20 sets) controlled by the user via javascript.
Without the dynamic content all works well. After I changed my html form from city
to city[]
it obviously stopped passing the data to my database.
I have tried all sorts of ways to make this work with no success. Any ideas?
Form manipulation:
var i=0; //Global Variable i
//function for increment
function increment(){
i +=1;
}
$(document).ready(function(e){
/// Variables
var html ='<p /><div>';
html+='<label for="city">City:</label>';
html+=' <input type="text" name="childcity[]" id="city">';
html+=' <label for="date">Date:</label>';
html+=' <input type="text" name="childdate[]" id="date">';
html+='<a href="#" id="removecity">Remove City</a>';
html+='</div>';
var maxRows = 20;
// var x = 1;
// Rows
$("#addcity").click(function(e){
if(i <= maxRows){
$("#container").append(html);
i++;
}
});
// Remove Rows
$("#container").on('click','#removecity', function(e){
$(this).parent('div').remove();
i--;
});
});
Form data retrieval and querying:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "root", "", "pb1646a_jos1");
// Check connection
// Escape user inputs for security
$city = $_POST['city'];
$date = $_POST['date'];
foreach($city AS $key => $value){
// attempt insert query execution
$sql = "INSERT INTO employe_table(name,age)
VALUES (
'".$mysqli->real_escape_string($value)."',
'".$mysqli->real_escape_string($date['$key'])."'
)";
$insert = $mysqli->query($query);
}
$mysqli->close(); // Close connection
header("location: http://www.orastravelagency.com/modules/mod_pari/test/index.html")
?>
Form:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="java.js" type="text/javascript"></script>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="insert2.php" method="post">
<div>
<p>
<label for="name">Trip Name:</label>
<input type="text" name="name" id="name">
</p>
</div>
<div id="container">
<p>
<label for="">City:</label>
<input type="text" name="city" id="city[]">
<label for="date">Date:</label>
<input type="text" name="date" id="date[]">
<a href="#" id="addcity">Add City</a>
</p>
</div>
<p />
<input type="submit" value="submit">
</form>
</body>
</html>