1

So I want to know how I should make a table, written in PHP 8, that updates as soon a new insert has been made in the database table (SQL Server 2019). I've created a simple table like this:

<table>
  <thead>
    <tr>
      <th>header1</th>
      <th>header2</th>
      <th>header3</th>
    </tr>
   </thead>
   <tbody>
     <tr>
       <td>text1.1</td>
       <td>text1.2</td>
       <td>text1.3</td>
     </tr>
  </tbody>
</table>

Then I've added a button that creates some input fields, and when these are submitted, the data is inserted into the database. But the only way I know to update the table is to reload the page so the table now has the new row. But I've seen websites that do this without reloading the site, and want to know how? My guess is to make some kind of listener, but how I dont know.

the submit button with some inputs could be something like this:

<form>
  <input type="text" id="html" name="fav_language" value="">
  <label for="html">HTML</label><br>
  <input type="text" id="css" name="fav_language" value="">
  <label for="css">CSS</label><br>
  <input type="text" id="javascript" name="fav_language" value="">
  <label for="javascript">JavaScript</label>
  <input type="submit" value="Submit">
</form>

All this should just be pure self written code, not JQuery or something like that.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • Yes you can do ajax without jquery. Did u attempt ? – Ken Lee May 18 '22 at 06:21
  • Can it be done without ajax? – Mads Sander Høgstrup May 18 '22 at 06:29
  • "All this should just be pure self written code, not JQuery or something like that." - please explain why, this is a very big limitation today when most of the hard work is done by frameworks. – Dale K May 18 '22 at 06:30
  • How does your database update take place anyway? You either post the complete form back, or send it back via ajax. – Dale K May 18 '22 at 06:31
  • @DaleK I have written Ajax before, my question is simply just if it's possible to do it without. simply just to see if it was possible to avoid importing libs. – Mads Sander Høgstrup May 18 '22 at 06:34
  • You can write ajax manually, go google it :) its too complex for an answer here. (And you didn't answer my second question). – Dale K May 18 '22 at 06:36
  • The question has nothing to do with SQL Server however, so I have removed the tag. – Dale K May 18 '22 at 06:37
  • @DaleK In the example code I have a `` inside the form, which results in a page refresh. This post would trigger an INSERT before the table has been created and when that is done, the table will be created with all the newest data. – Mads Sander Høgstrup May 18 '22 at 06:40
  • @MadsSanderHøgstrup many of your replies in these comments should be answered _in the body of the question_ with an [edit]. –  May 18 '22 at 18:50

1 Answers1

1

If you want your forms to submit without reloading, you should use AJAX. E.g. Almost all of the chat boxes in the websites use AJAX to submit information. I think this question will help you.

If you want to use AJAX with PHP you can simply make AJAX to call your PHP file for submitting information. Look at this page to learn more.

If you want to use AJAX without any library you can do that with XMLHttpRequest. more info

Atena Dadkhah
  • 623
  • 1
  • 5
  • 19