Jump to content

calling a confirmation dialog box inside an IF stmt in php


Cheslia

Recommended Posts

I've looked around various forums and found different answers, but nothing seems to fix exactly or work for me.  

 

Scenario:  User is modifying a database table's text column value that already exists in the table in a <textarea> tag.  Upon clicking submit, the validation code does a database query to see where the primary key might be associated with other records that would be affected by this change and returns a list of those affected.  The code then evaluates if there is anything returned, and if there are rows returned, creates a list to be presented to the user to let them know that their change will affect all these other records that are tied to this particular record.  At this point, I want to prompt the user to confirm their choice to update this record, knowing how it will affect the related records that use this record.  Hope that is clearer than mud....but here's the snippet of PHP code....

 

Note: the $_SESSION['feedback'] simply displays its output in a designated <div> tag.  

if(strlen(trim($ori_list))==0) {
   //no other ORIs use this custom step, so it is ok to make the update
   $_SESSION['feedback'] .= "No other ORIs share this custom text script - ok to proceed.<br>";
   $upd_ok = 'y';
} else {
   $_SESSION['feedback'] .= "If you make this change, it will affect these ORIs - " . $ori_list . "   Are you sure you want to proceed?<br>";
   //display a pop-up confirmation box and get the yes/no answer
                        
                        
   if ($confirm=='y') {
      $upd_ok = 'y';
      $_SESSION['feedback'] .= "Proceeding with the update to protocol_step_id ". $text_choice . " <br>";
    } else {
       $upd_ok = 'n';
       $_SESSION['feedback'] .= "Aborting the update to protocol_step_id ". $text_choice . " <br>";
     }
}

I've tried echo '<script....  type answers, I've tried closing my php with ?>, putting in an <a href> tag with an onclick= then reopening the <?php.   I've looked at jquery confirm, and ajax even, but nothing seems to just allow me to pop up a dialog box in this particular place of my logic and return a True/False or Yes/No value back to my code to proceed.  Any help would be greatly appreciated as you all seem to be so helpful thusfar with my other questions.  

 

Thanks.

Link to comment
Share on other sites

Use an interstitial page: the form submits to another page which does those checks, and if there are records the user sees another page which presents them with the confirmation option. The form on that page either submits the changes (confirm) or does whatever else (cancel).

Link to comment
Share on other sites

Sure, but it's a little more complicated.

 

1. Your AJAX to submit the data needs to be able to handle the cases where (a) the submission doesn't need confirmation and (b) the submission does need confirmation.

2. If it does then your PHP needs to return whatever data is necessary for the Javascript to prompt the user.

3. That Javascript then needs a dialog where it can present the data, with confirmation and cancel buttons.

4a. Confirming that dialog means re-submitting the data with some sort of flag set which says to bypass any required confirmation check. The submission would probably work the same way as the "doesn't need confirmation" process from #1, with the PHP simply returning success or failure.

4b. If you need to consider race conditions, where the underlying data forcing the confirmation step has changed while waiting for that confirmation, then you'll need versioning or a timestamp or some other sort of identifier which lets you track whether the state of the data at the time of the first submission is the same as it is during the second submission. If it's changed then that would mean another confirmation box (with some sort of message saying the data changed, thus the additional prompt).

Link to comment
Share on other sites

Another question....what about putting a <div> that will be hidden until this scenario gets triggered, then unhiding it and displaying radio buttons to select for the user to continue vs. a pop-up dialog box?  I've been playing with that a bit but having issues.  I already have some ajax/jquery going for hiding/showing/input into the textarea div based on a selection from a menu, but not sure how to incorporate it into the existing ajax....or to have a separate function inside the same <script> tag.  

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
          $('#csid').bind('change', function(event) {
  //          $("#csid").on("click", function () {
              console.log($("#csid").val());
              var cs_id = $("#csid").val();
              var schema_name = '<?php echo $schema ?>';
              console.log(schema_name);
              console.log(cs_id);
              if(cs_id=="NEW") {
                  var cleartxt = "";
                  $('#new').show();
                  $('#existing').show();
                  $('#newSection').val(cleartxt);
              } else {
                  $('#new').hide();
                  $('#existing').show();
                  $.ajax({
                      type: 'Post',
                      url: 'display_custext.php',
                      data: {
                          csid: cs_id,                  //passes the variable cs_id as the $_POST['csid'] to the designated page
                          schema: schema_name,          //passes the variable schema_name as the $_POST['schema'] to the designated page
                      },
                      success: function (data) {
                          var obj = $.parseJSON(data);
                          var result = "";
                          $.each(obj, function () {
                              result = result + this['PROTOCOL_STEP_CUSTOM_TEXT'];     //create what will be passed to the .html tag in the ID='newSection' tagged section of html
                          });
                          $('#newSection').val(result);        //returns the value to display in the <input> tag
                      }
                      });
              }
          });

        });
    </script>

Here's the part of the html form...

            <form class="h3" method="post" action='new_custext.php'>
                <span class="error"><?php echo "<br>" . $searchforErr;?></span>
                <div style="color:blue;font-size:13px" id="feedback"><?php echo $_SESSION['feedback']?></div>
                <p>
                    Select NEW or an existing Custom Text Script:<br><br>
                    <select size="7" name="text_choices[]" class="select" id="csid">
                        <?php $page->get_custext($schema); ?>
                    </select><br><br>
                </p>
                <div id="new" style="display:none">
                    Enter a name for your new custom text:
                    <input type="text" name="textname" ><br><br>
                </div>
                <div id="existing" style="display:none">
                    Enter your NEW custom script or modify the existing script here: <br><br>
                    <textarea name="custext" id="newSection" rows="6" cols="100" wrap="soft"></textarea><br><br>
                </div>
                <input class="ExtraLargeButton" type="submit" name="submit" value="Submit">  
                <input class="ExtraLargeButton" type=button name="home" value="Home" onClick="parent.location='index.php'">
                <!-- Not used for returning to the LogGUI application, will most likely include link back to V11 when called from V11
                <input class="button" type=button name="loggui" value="Log Index Home" onClick="parent.location='<?php echo BASE_LOGGUI_URL; ?>'">-->
            </form>
Link to comment
Share on other sites

I don't have any examples - it's all very dependent on what frameworks you're using, if any. All I see in what you've posted is jQuery, which is more like a set of utilities than an actual framework framework that you would build on. So I suggest finding and using other frameworks - Bootstrap, for instance, would guide you on the markup and help you apply styles to it. Of course there are others that can do more.

 

1. Your AJAX to submit the data needs to be able to handle the cases where (a) the submission doesn't need confirmation and (b) the submission does need confirmation.

That's something you have to do in your Javascript. When it does the AJAX it needs to test for those two types of responses (and, of course, the PHP side has to be modified to return that).

 

2. If it does then your PHP needs to return whatever data is necessary for the Javascript to prompt the user.

That means the PHP returns not just that it needs confirmation but all those "other records" you mentioned. Considering the previous comment, the responses might look like

{
	"status": "success"
}
{
	"status": "needs-confirmation",
	"records": [ {whatever...} ]
}
{
	"status": "error",
	whatever...
}

3. That Javascript then needs a dialog where it can present the data, with confirmation and cancel buttons.

You make the dialog in HTML and use Javascript to manipulate it. The markup will probably be something like

<div id="confirmation-dialog" style="display:none;">
	<h2>These records will be updated:</h2>
	<table>whatever to show the records</table>
	<div><button id="confirmation-accept" type="button">Accept</button> <button id="confirmation-reject" type="button">Cancel</button></div>
</div>
You clear the table and add data, then show the #confirmation-dialog. You also need code for the two buttons.

 

4a. Confirming that dialog means re-submitting the data with some sort of flag set which says to bypass any required confirmation check. The submission would probably work the same way as the "doesn't need confirmation" process from #1, with the PHP simply returning success or failure.

If you put the AJAX part into a function then you can call that function with the initial submit and again with the confirmation submit.

 

4b. If you need to consider race conditions, where the underlying data forcing the confirmation step has changed while waiting for that confirmation, then you'll need versioning or a timestamp or some other sort of identifier which lets you track whether the state of the data at the time of the first submission is the same as it is during the second submission. If it's changed then that would mean another confirmation box (with some sort of message saying the data changed, thus the additional prompt).

Uh... Don't mind that. Just keep it in mind for the future.
Link to comment
Share on other sites

I'm still of the mindset that AJAX (in a currently non-AJAX heavy site) should be an afterthought. Having that second page means your stuff works and you can focus on other important parts, but doesn't mean you can't add in AJAX later.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.