There is a form in which data is entered, then we click OK and the entered data goes to the javaskiptovy file.

The form on the index.php page

<form id="'.$row['loadName']. '" method="GET"> <table сlass="value"> <tr> <td> <input id="'.$row['loadName'].'-LoadName" name="loadname" type="hidden" value="'.$row['loadName'] ?>'."/> </td> <td> <input id="'.$row['loadName'].'-TestOk" name="testOK" size="10" type="text" value="'.$RowForValues['TestOk'].'"/> </td> </tr> <input class="buttonValue" name="ValueOK" onClick="requestForValue(readDataForValue,\''.$row['loadName'].'\');" type="button" value="OK"/> </form> 

Javascript data.js

 function requestForValue(callback, id) { var xhr = getXMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { callback(xhr.responseText, id); } }; id_Loadname = id + '-LoadName'; id_TestOk = id + '-TestOk'; var LoadName = document.getElementById(id_Loadname).value; var TestOk = encodeURIComponent(document.getElementById(id_TestOk).value); xhr.open("GET", "AJAX/value.php?LoadName=" + LoadName + "&TestOK=" + TestOk, true); xhr.send(null); } 

After that there is an update of this data in the table. But there is a condition where in one case it is necessary to update part of the table (it works), and in another case it is necessary to update the ALL page.

value.php, which is javascript

 $dbh = dbConnectForAJAX(); $LoadName = $_GET["LoadName"]; $testok = $_GET["TestOK"]; if (empty($LoadName)) { /*запрос на обновление данных*/ } else { /*запрос на добавление данных*/ echo '<a href="javascript:location.reload()">See new data click here</a>'; } 

Tell me how else you can update the index.php page without clicking on the link, and so that it is updated automatically after adding data to the database.

  • meta not suitable? - Rimon
  • @KlyaKsa; To format a code, select it with the mouse and click on the {} button of the editor. - Nicolas Chabanovsky

1 answer 1

If you want to do everything beautifully - look towards the MVC or MVVM frameworks. For example, Backbone.js, Knockout.js (or both at the same time, using Knockback.js), Spine.js, Batman.js and many more in taste and color, each with its own characteristics.

And if you want to quickly make it work and forget - just return a special line from the server, say, “ <reload> ”, and see it in xhr.responseText (or before calling a callback or in it where it is more convenient) reload the whole page ( location.reload() )