1

Have a look at my two different cases.

fun.php (Same in all case)

function pre_invoice($value) //earlier it was public function ---- (typo in the question)
{
//Code to insert query in database
}

Case 1:

form.php

<?php 
require_once 'fun.php';

if(isset($_POST['submit'])){
pre_invoice(999);
}
?>
<table class="table table-striped table-hover table-bordered">
    <thead class="text-center">
  <tr>
    <th>Service</th>
    <th>Quantity</th>
  </tr> 
 </thead>
<tbody>
  <form method="POST">

    <tr>
    <td></td>
    <td>8</td>
  </tr>

    <tr>
    <td></td>
    <td>12</td>
  </tr>

    <tr>
    <td></td>
    <td>4</td>
  </tr>

    <tr>
    <td></td>
    <td>6</td>
  </tr>
        <input type="submit" name="submit" />
        </form>
</tbody>
  </table>

Now when I click submit button in form.php, the query is inserted in the database.


Case 2:

form.php

<table class="table table-striped table-hover table-bordered">
    <thead class="text-center">
  <tr>
    <th>Service</th>
    <th>Quantity</th>
  </tr> 
</thead>
<tbody>
  <form method="POST">

    <tr>
    <td></td>
    <td>8</td>
  </tr>

    <tr>
    <td></td>
    <td>12</td>
  </tr>

    <tr>
    <td></td>
    <td>4</td>
  </tr>

    <tr>
    <td></td>
    <td>6</td>
  </tr>
        <input type="submit" name="submit" />
        </form>
</tbody>
  </table>

index.php

<?php 
 require_once 'fun.php';

 if(isset($_POST['submit'])){
  pre_invoice(999);
 }
?>
<div id="form"></div>​
<script>
   $("#form").load('form.php');
</script>

Now when I click submit button in index.php, the query is not inserted in the database.


I have all libraries included in the index.php all HTML tags are proper, SQL query are proper.

keso
  • 21
  • 4
  • 1
    Why you need setInterval , that will recreate your form every 3sec – hs-dev2 MR Jul 01 '19 at 01:35
  • The other codes which were not necessary for the question are skipped and the skipped codes need to refresh every 3 seconds. @hs-dev2MR – keso Jul 01 '19 at 09:31
  • " the skipped codes need to refresh every 3 seconds"...in that case make only the necessary parts refresh, and not your form. Separate the functionality into different parts. – ADyson Jul 01 '19 at 11:57

1 Answers1

1

The fist thing you want to do in such cases is to check your server's logs for PHP errors. This depends on what web server you are using and its configuration.

Also you can check you browser's JavaScript console for any error on the developer tools (by default, you can open the console with Ctrl+Shift+J on Chromium based browsers).

After some changes, it works on my tests.


fun.php

<?php
function pre_invoice($value) {
    // Your code
}

I removed public, this is not a class' method, thus it can not have a public/private keyword (this was producing a PHP error)

I added the <?php to indicate a PHP code start, we don't have to close it as that file don't have any additional HTML code


form.php

<form method="POST">
    <input type="submit" name="submit" />
</form>

(this file is the same as the one you provided)


index.php

<?php 
    require_once 'fun.php';

    if(isset($_POST['submit'])){
        pre_invoice(999);
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>title</title>
    </head>
    <body>
        <div id="form"></div>
        <!-- Load jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <!-- Load form -->
        <script>
            $("#form").load('form.php');
            // We load the form only once
            // setInterval would load the form many times (in your code every 3 seconds)
        </script>
    </body>
</html>

(I am not sure if you only provided a part of your HTML file)

I added a basic HTML structure (, etc.)

I added jQuery library to support he $ (this was producing an error on the browser's console) (I loaded jQuery from the Google's CDN, you may want to host it locally)

I removed setInterval as I think you don't need it, you don't have to load the form every 3 seconds


PS: Your SQL insert code may also produce errors.


Update after full form.php

Check Form inside a table

Since your code is invalid, there is a different result of the case 1, where the browser loads by default your HTML code, and the case 2, where the HTML id dynamically loaded.

So, you should move the <form> tag outside the table or inside a <td>.

GramThanos
  • 3,332
  • 1
  • 18
  • 32
  • 1
    Ok but what changes, and why? – ADyson Jun 30 '19 at 17:21
  • I am unable to figure out the changes you did. @GramThanos – keso Jul 01 '19 at 09:32
  • My changes are obvious. I removed the `public` keyword from the function definition (there should be an error on your server logs). I also created a full html document for the *index.html* and added the jQuery library that you use (the `$` that you call indicates that you use that library). @ADyson why did I added a correct HTML structure? or why did I corrected the php errors? – GramThanos Jul 01 '19 at 10:23
  • they're obvious to you, and fairly clear to me as an experienced developer, but a) they may not be obvious to someone unfamiliar with the techniques (e.g. the OP, otherwise they might not have asked the question) and b) you still need to explain _why_ you changed it. How does what you've changed help to solve the problem exactly? Again it might be clear to you, but not to others. The point of this site is to help people, not just encourage them to blindly copy and paste code potentially without fully understanding the implications. – ADyson Jul 01 '19 at 10:28
  • P.S. your newly-added description of what's changed omits a fairly vital change to the JavaScript where you removed setInterval...you should explain why you did that and why it was likely to have been an issue before. – ADyson Jul 01 '19 at 10:29
  • My answer's main point was that the OP should first check error logs on his server. The code I provided was only for testing by anyone that wanted to check it. I do think that you are right that I should explain more about my changes, they are visible, but maybe frustrated to a new programmer (as you said). – GramThanos Jul 01 '19 at 10:37
  • Updated the question, now my form.php here is fully coded as it's in my project. – keso Jul 01 '19 at 13:37
  • @keso if you are not sure whether your `pre_invoice` function works or not, follow the https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display to active PHP errors so that you can bebug it. Also, always format your code to see missing tags, on your *forms.php* you are missing the `` tag and maybe a `` (not that this will fix any problem, it just helps to avoid future problems). – GramThanos Jul 01 '19 at 13:50
  • *if you are not sure whether your pre_invoice function works or not*, my **CASE 1 work as I need**, so no issue in the function. `` was skipped in the question (updated). The last line in the question says, *I have all libraries included in the index.php all HTML tags are proper, SQL query are proper.* – keso Jul 01 '19 at 13:59
  • @keso Your `
    ` tag should be moved outside the table or inside a ``. I updated my answer. Your case 1 is also invalid, but the browser fixes/ingores the invalid code part.
    – GramThanos Jul 01 '19 at 14:32