Reputation: 417
need your advice, I have a form with some many fields, i use it to post to database, it's simply if i post it one by one. But it's so heavy if we have thousands data to post. i wanna post it cumulatively.
For now i use javascript, i add it to an array for temporary storage then show it with table under form. i want to use this array then post it into array variable of PHP. Then i can post to database. But i don't know how to do it if it on the same page. Because javascript is client side, and PHP is server side.
Is there any idea?
EDITED : this my code, I have not made it in the table:
<!DOCTYPE html>
<html>
<body>
<label>Fruit Name</label> :<br>
<input type="text" id="text1"/><br>
<label>Nominal</label> :<br>
<input type="text" id="text2"/><br><br>
<button onclick="myFunction()">Add</button><button onclick="tableCreate()">Add</button>
<p id="demo"></p>
<?php $a = array(); ?>
<script>
var products = [];
function myFunction() {
var text1 = document.getElementById("text1").value;
var text2 = document.getElementById("text2").value;
var temp = text1 + "-" + text2;
products.push(temp);
document.getElementById("demo").innerHTML = products;
}
</script>
<hr>
<form action="" method="post">
<button type="submit" name="test" value="Test">Save</button>
</form>
<?php
if(isset($_POST['test'])){
// get array products from javascript, isn't possible?
}
?>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
Upvotes: 1
Views: 1409
Reputation: 2857
You can make use of php for submitting a form and inserting the record to database.Write php script on the same page which will be executed on form post.
Save the following code snippet to a php file name as currentPhpFileName.php and execute it over localhost.
<!DOCTYPE html>
<html>
<body>
<form action="currentPhpFileName.php" method="post">
<input type="text" name="fieldName1" value="testValue1" />
<input type="text" name="fieldName2" value="testValue2" />
<button type="submit" name="test" value="Test">Save</button>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
print_r($_POST['fieldName1']);
print_r($_POST['fieldName2']);
foreach($_POST as $value)
{
print_r($value);
//database connection
//logic to insert records into database
}
}
?>
Upvotes: 1