1

I want to show a confirmation message at form submit with a confirmation message. Confirmation message itself needs to do some php calls in order to do some validations and then show a message with ok and cancel buttons. Upon pressing ok button, it should continue with submit flow and remain in the form otherwise.

I have taken following approach but it doesn't work. Even though it goes into else block it doesn't show the message. Seems like the second echo line is the problem. Can you give me a clue on how to accomplish this task?

PHP code:

<?php 
    if (!empty($_POST)){
        // read $_POST['item'] values and do the validation and construct validation message.
        // Because validation message is a dynamic one which is based on user inputs. 
        // Some server calls are also needs to make within this validation phase.
        
        if(empty($validation_msg)) {
            saveData();
        }
        else {
            echo '<script type="text/javascript"> if (confirm("' . $validation_msg . '")) { ';
            echo saveData(); 
            echo ' } </script>'; 
        }
    }
    
    function saveData(){
        // Save function goes here.
    }
?>

HTML code:

<div class="form-group">
    <button type="submit" class="btn btn-primary" id="submit_button" disabled><?= _('Save') ?></button>
</div>
dds
  • 23
  • 5
  • you should do it clientside, – Lawrence Cherone Jul 17 '22 at 18:16
  • Thank you @LawrenceCherone. I'm bit new to php and messed for quite sometime because of those two server calls (validation and actual submit). After several tryouts, as you suggested I have managed to do my task by calling validation method from client side through a ajax call. For that I had to move validation code to seperate php (validate.php) and then passed variables from this parent php (index.php) into validation.php and echo out the message which I wanted. But still I would like to know whether it is possible to do this kind of calling mechanism from a single php file. – dds Jul 17 '22 at 22:25

0 Answers0