0

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:

  1. The user starts groups.php, select a group (there aren't any actual editing in the demo) and press the edit-button.
  2. The editgroup.php starts, the user edits the group and press Save.
  3. The editgroup.php then starts save.php using an XMLHttpRequest-request. save.php does the update of the database and are supposed to return to groups.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\'" />');
?>
Poul Kristensen
  • 135
  • 1
  • 9
  • 3
    You'll need to add `type="button"` to your buttons to stop them submitting the form normally – Phil Apr 13 '23 at 00:47
  • 3
    Don't respond to asynchronous requests with redirects. You need to make the request, handle the response and then navigate, all using JS – Phil Apr 13 '23 at 00:49
  • Okay. Thats an eyeopener. You mean I should not redirect in save.php? I usually do it in onload but since I had no use for it here, I didn't. Are you telling me it will be bad in some way if I don't? Thanks for changing the filenames. – Poul Kristensen Apr 13 '23 at 00:52
  • I think the type='button' thing worked for the Cancel button, but made no difference for the Save button. I'll do the navigation in the onload. Thanks – Poul Kristensen Apr 13 '23 at 01:06
  • 1
    `You mean I should not redirect in save.php`...it's not "bad", it's just that it won't work in this case, because AJAX doesn't follow redirect headers (the whole point of ajax is to allow you to make a request to the server while staying on the same page, with no need for postbacks, refreshes, redirects etc!). So if you _do_ want to redirect after the ajax has completed, you'll need to do it from javascript- but then if you immediately redirect after an ajax request, I'd somewhat question why you bothered to use ajax in the first place – ADyson Apr 13 '23 at 06:15
  • Though I just noticed you're using a Meta refresh tag rather than an actual redirect header. Even so, your current ajax code never puts that echoed text back into the receiving page so it would have no effect – ADyson Apr 13 '23 at 06:18
  • Hi ADyson - I'm finally starting to understand how it works. And you seem to be perfectly correct in what you write ("the whole point of ajax is to ...") I made it work fairly easily first time I tried it, so I didn't even notice these pitfalls.As I understand it now, save.php does not appear and even when you try to force the issue, nothing will appear on the display. I only see one thing left and that is that my program will not return to groups.php if I do not place window.location.assign('groups.php'); in onload. But apart from that, everything seems okay. Thanks a lot – Poul Kristensen Apr 14 '23 at 06:45
  • Yes, save.php will never appear directly on the page if you request it via AJAX. You _could_ write JS code which takes the response from save.php and puts it onto your page though. – ADyson Apr 14 '23 at 07:46

0 Answers0