Search code examples
phpdialogboxconfirmation

how to set PHP confirmation dialog box before save and after form validation in single page


How to set the popup confirmation dialog box once the form validation is done in php? If user clicks the submit button, it should validate the user input then, finally it will ask the confirmation message like "Do you want to save?" with ok and cancel buttons, then only it should save the data into database. please refer below code.

 <?php
    error_reporting( ~E_NOTICE );
    $name_err='';
    $name='';
    $type_err='';
    $type='';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['submit'])) {

              $e = true;

        if (empty($_POST['name'])) {
            $name_err = "Please enter the Product Name";
            $e = false;
        } else {
            $name = $_POST['name'];
            // check if name only contains letters and whitespace

        }
     if (empty($_POST['type'])||$_POST['type']=="select") {
            $type_err = "Please select the Product Type";
            $e = false;
        } else {
            $type = $_POST['type'];

                   }
     $con = mysqli_connect("localhost", "root", "", "csr");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }


}
//final check whether it is true the input will process

    if($e)
    {

        mysqli_select_db($con, "csr");

        mysqli_query($con, "insert into products(employee_id, name, type, brand, model, url, conditions, about, listedon, image_path, reserved_price, Purchased_price, buy_now_price, sale_end_date)
                                   values('$eid','$name','$type','$brand','$model','None','$condition','$about','$listedon','$imgName','$reserved_price','$Purchased_price','$buy_now_price','$sale_end_date')") or  die('Could not connect: ' . mysql_error());

        header('Location:/sellproduct/index/');
        mysqli_close($con);

    }
}
?>
 <form action=" "  method="post" class="stdform stdform2" autocomplete="off" enctype="multipart/form-data">

                    <p>
                             <input type="hidden" name="employee_id" value="" id="employee_id" />
                    </p>
                <p>
                    <label for="name" class="required">Product Name</label>
                    <span class="field">
                        <input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>

                </p>
                <p>
                    <label for="type" class="required">Product Type</label>
                    <span class="field">
                    <select name="type" id="type" minlength="2" maxlength="60">
                            <option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : "");  ?> selected disabled >Select</option>
                            <option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
                            <option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
 <form action=" "  method="post" class="stdform stdform2" autocomplete="off" enctype="multipart/form-data">

                    <p>
                             <input type="hidden" name="employee_id" value="" id="employee_id" />
                    </p>
                <p>
                    <label for="name" class="required">Product Name</label>
                    <span class="field">
                        <input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>

                </p>
                <p>
                    <label for="type" class="required">Product Type</label>
                    <span class="field">
                    <select name="type" id="type" minlength="2" maxlength="60">
                            <option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : "");  ?> selected disabled >Select</option>
                            <option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
                            <option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
 <form action=" "  method="post" class="stdform stdform2" autocomplete="off" enctype="multipart/form-data">

                    <p>
                             <input type="hidden" name="employee_id" value="" id="employee_id" />
                    </p>
                <p>
                    <label for="name" class="required">Product Name</label>
                    <span class="field">
                        <input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>

                </p>
                <p>
                    <label for="type" class="required">Product Type</label>
                    <span class="field">
                    <select name="type" id="type" minlength="2" maxlength="60">
                            <option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : "");  ?> selected disabled >Select</option>
                            <option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
                            <option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
</select><br><span class="error"><?php echo $type_err;?></span></span>                </p>

<p class="stdformbutton">
                    <input type="submit" class="bluishBtn button_small" name="submit" value="SAVE" id="submit">

                    <button name="cancel" id="cancel" type="button" class="greyishBtn button_small">Cancel</button>                </p>

</form>

please help me.


Solution

  • You can't generate a dialog box directly by PHP, in your final if statement, you should echo a basic html with a javascript which redirects to another php page taking your input in argument to insert it :

    echo '
    
    <html>
    <script type="text/javascript">
    
    function input_confirm(){
    
        var r = confirm("Confirm this input : '.$_POST['name'].'-'.$_POST['type'].'");
        if(r){
            window.location.href = "insert.php?type='.$_POST['type'].'&name='.$_POST['name'].'";
        }
    }
    
    
    </script>
    
    <body onload="input_confirm()">
    </body>
    
    </html>
    
    ';