I explained the same problem 2 days ago, but I probably made it too opaque, so here is a demo in clear code:
The program consists of 4 files: groups.php
, editgroup.php
, editgroup.js
and save.php
.
It was supposed to work like this:
- The user starts
groups.php
, select a group (there aren’t any actual editing in the demo) and press the edit-button. - The
editgroup.php
starts, the user edits the group and press Save. - The
editgroup.php
then startssave.php
using an XMLHttpRequest-request.save.php
does the update of the database and are supposed to return togroups.php
.
But it doesn’t.
I’ve cut everything that is not basic out, so the problem must be in the remaining code.
Two files, a help-file and the mainmenu, are not used and so not included.
Oops. I just noticed I’ve forgotten Systemfejl-function. It is a function that logs the errormessages. I will put it in tomorrow.
I’ve been told to use use fetch instead of XMLHttpRequest, so I will look into it tomorrow.
I’m using PHP Version 7.4.29 and Javascript Version 1.15 and VS Code version 1.77 with some extensions, like PHP Debug v1.32.1.
The four files can be found at the bottom.
Hope someone can see anything. Because I sure can’t.
Poul
groups.php
<?php
function Controls() {
$S = '
<form id="Targeter" action="/Help/Grupper.htm" target="_blank" method="post">
</form>
<form style="position:relative;left:10px;top:15px;" method="post">
<button name="gId" formaction="/editgroup.php" method="post" id="Edit" type="submit" class="btn btn-primary" >Edit</button>
<button name="gId" formaction="/mainmenu.php" method="post" id="Return" type="submit" class="btn btn-primary" >Return</button>
<button form="Targeter" class="btn btn-primary" >Help</button>
</form>';
return $S;
} // Controls()
function ShowControls($Controls) {
// Displaying the Controls..
$S = '
<div id="Tools" style="width:100%;height:7vh;margin:1vh 1vh 1vh 0;border-radius:10px;
background-color:darkgray;">';
$S = $S.$Controls.PHP_EOL.'
</div id="Tools">';
echo($S);
} // ShowControls()
try {
ShowControls(Controls());
}
catch(Exception $e) {
SystemFejl($e->getMessage());
}
finally {
}
?>
</body>
</html>
editgroup.php
<?php
$Controls = "
<form style='position:relative;left:10px;top:15px;' method='post'>
<button id='Save' onclick='SaveNow(this)' class='btn btn-primary' name='gId'
value='8'>Save
</button>
<button onclick='ExitNow(this)' id='Cancel' class='btn btn-primary'>Cancel</button>
</form>";
function ShowControls($Controls) {
// Her den kode der viser Controls..
$S = '
<div id="Tools" style="width:100%;height:7vh;margin:1vh 1vh 1vh 0;border-radius:10px;
background-color:darkgray;">';
$S = $S.$Controls.PHP_EOL.'
</div id="Tools">';
echo($S);
} // ShowControls()
ShowControls($Controls);
?>
<script src="editgroup.js?newversion" type="text/javascript"></script>
</body>
</html>
function ExitNow(o) {
if (window.confirm("Are you sure?"))
window.location.assign('groups.php');
} // ExitNow()
function SaveNow(o) {
const xhttp = new XMLHttpRequest();
xhttp.open("POST", "save.php");
xhttp.send();
} // SaveNow()
save.php
<?php
echo('<meta http-equiv="Refresh" content="0; url='groups.php'" />');
?>