1

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>
2
  • 3
    you can use ajax to post data Commented Mar 24, 2015 at 5:02
  • Welcome to Stackoverflow. Share the code snippets you have tried yet. Commented Mar 24, 2015 at 5:10

1 Answer 1

1

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
  }
 }
 ?>

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for reply, i mean, is there way to solve my case? for the recent, my plan, i submit it to array using java script, then i want to passing it into PHP variable on the same page, then i can using this array for looping and post it into database.. but i don't know how to do, when we use on the same page, it's impossible because java script and php script is different side. is there any ways guys?
i have tried your tips, and yeah, it's usefull.. But when we want to post 3 different inputs, and before that, our 3 different inputs have in an array. How do we post in database? is PHP can do that? thanks for reply..
For that you need to use php sql database connection. w3schools.com/php/php_mysql_connect.asp Try it out and come with new question if you face any issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.