1

I'm loading values from an XML file and then updating the XML with a form on a php page. My problem is this - I have a query running a count on nodes, which works perfectly fine, but I also want to update nodes with the count and get accurate values back. When I submit my form, it submits the values as they are when I load the file, but in order to update the count properly (because I need the updated node count) I need to have it submit twice. I've tried to have it submit the form on a body onload function but from what I can ascertain when you hit the submit button it doesn't do a true full page refresh. It reloads the page but I've been unable to have it actually run any scripts or onload functions. The alternative that I thought of but can't figure out how to implement is having the count go down if I change the option value to closed.

<!DOCTYPE html>
<?php
$xml->load('test.xml');
$xpath = new DOMXpath($xml);
$liftsopen = $xpath->query("//node/Lifts-Open")->item(0);
$lift1status = $xpath->query("//areas/area[@name='Lift1']/lifts/lift[@name='Lift1']/@status")->item(0);
$lift2status = $xpath->query("//areas/area[@name='Lift2']/lifts/lift[@name='Lift2']/@status")->item(0);

$liftcount = 0;
foreach ( $xpath->query('//lifts/lift[@status="OPEN"]') as $count1 )   {
 $liftcount++;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $lift1->nodeValue = $_POST['lift1statusform'];
    $lift2->nodeValue = $_POST['lift2statusform'];
    $liftsopen->nodeValue = $liftcount;
    $xml->save('test.xml');
}
?>

<body>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <h4>Lift1</h4>
        <select name="lift1statusform" >
          <option selected value="<?= htmlspecialchars($lift1status->nodeValue)  ?>"><?= htmlspecialchars($lift1status->nodeValue)  ?></option>
          <option value="OPEN">OPEN</option>
          <option value="CLOSED">CLOSED</option>
        </select>
        <h4>Gondola</h4>
        <select name="lift2statusform" >
          <option selected value="<?= htmlspecialchars($lift2status->nodeValue)  ?>"><?= htmlspecialchars($lift2status->nodeValue)  ?></option>
          <option value="OPEN">OPEN</option>
          <option value="CLOSED">CLOSED</option>
        </select>        
<input name="submit" type="button" value="Save"/>
</form>
</body>
</html>

used the following Ajax function to submit the form twice:

function Test() {
    var form = $('form');
    $.ajax({
      type: 'POST',
      url: form.attr('action'),
      data: form.serialize(),
      success: function(data) {
        $.ajax({
          type: 'POST',
          url: form.attr('action'),
          data: form.serialize(),
          success: function(data) {
          location.reload(true);
          }
        });
        }
      });
    }
eleytheria
  • 123
  • 9
  • Your question makes no sense, and here is why: The user of your page only enters values in the form once, not twice, so you only need one submission to do all the processing you could possibly want. – KIKO Software Jun 28 '19 at 16:49
  • Hey KIKO software. That's actually incorrect. The trail count is loaded by the status value of the lift when the page is loaded. So when I hit submit, it will update the status value, but the count value is still the same. In order to save the new count value, I need to submit the page again. – eleytheria Jun 28 '19 at 17:01

1 Answers1

1

Would AJAX be a valid solution to your problem?

If you need to perform multiple "form submits", you should check out the capabilities of AJAX. Ajax allows you to send exchange data with a back-end PHP file without leaving (or even refreshing) the current page. Perhaps it is your solution.

As with most js actions, it can be event-driven (that is, triggered one-or-more-times by some event), or repeated inside a loop, etc.

AJAX is most implemented using the jQuery library:

$(document).ready(function(){
    $.ajax({
        type: 'post',
         url: 'my_backend_file.php',
        data: {varname:value}
    }).done(function(recd){
        console.log('data recd from PHP side, if needed: ' + recd);
    });
});

although it is equally well handled in vanilla js also.

References and Other Examples:

https://stackoverflow.com/a/54332971/1447509

Should I have one form or two separate forms

https://www.html5rocks.com/en/tutorials/file/xhr2

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Hmm, the problem is that everything is on a single php page. The only separate file is my XML. – eleytheria Jun 28 '19 at 17:04
  • Are you prevented from creating a second PHP page? If not, then you will be happy to find the additional benefits of AJAX. Suggestion: take 20 mins and re-create one of the simpler ajax examples, just to get the feel of how it works. *I'll bet the possibilities (i.e. how to work this into your project) will begin to percolate and crystallize.* Note that the 2nd PHP page would be the ajax receiver/processor page that will act as the form `action="pagename.php"` page. With forms, you can cleverly make the form submit to the same page, but with AJAX it is best to use a second page. – cssyphus Jun 28 '19 at 17:12