Search code examples
javascriptphpajaxpostconfirm

How to get $_SESSION value updated when jscript confirm and call PHP by AJAX


After lots of trying, I need help. After a click event, i perform checking on data exist in my database and use jscript to pop a confirmation box.

How to get it works as I click cancel, its not showing any value I set on confirm.php?

enter image description here

Confirm with OK and Cancel

jscript code:

if($row_count2 > 0)
{
    if ($row2["Status"] == "REJECTED")
    {
    <script type="text/javascript">
        if (!confirm('Lot No has been REJECTED before.. Double confirm to add the Lot No!!')) {
        var option = "NO";
        $.ajax({
            type: "POST", 
            url: "confirm.php",
            data: "value="+option,
            success: function (data) {
                alert(data);
            }         
        });
    }
    </script>
    }
}

$ll = $_SESSION['Confirm'];
echo "<script type='text/javascript'>alert('$ll');</script>";

if ($row_count2 < 1 || ($_SESSION['Confirm'] = "YES"))
{
    //my insert query is here...
}

And this is inside confirm.php:

<?php    
    session_start();
    
    $xx = $_REQUEST["value"];
    $_SESSION['Confirm'] = $xx;
    echo "<script type='text/javascript'>alert('$xx');</script>";
?>

I'm expecting to get $_SESSION['Confirm'] value as NO

By default $_SESSION['Confirm'] value is YES

alert(data) shows that $xx is NO but when I put alert for $_SESSION['Confirm'] value on if condition before my insert query $_SESSION['Confirm'] value still remain YES


Solution

  • Instead of using JScript alert which is really troublesome.. I end up using modal popup alert.. enter image description here

    <div class="modal-content">
        <div class="modal-header bg-secondary">
            <button type="button" class="close" data-dismiss="modal"></button>
            <h4 class="modal-title text-white ">Confirmation</h4>
        </div>
        <div class="modal-body">                    
            <div class="box-body">
                <form class="form-signin">
                    <label for="txtLotNo" class="mr-sm-2 ml-sm-4">Lot No : '.$lotNo.' has been REJECTED before.. Double confirm to add the Lot No!!</label>                   
                    <br>
                    <div class="text-center">
                        <button type="button" id="getAdd" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#myAdd" data-id="YES'.$lotNo.$newLotNo.'" >YES</button>
                        <button type="button" id="getAdd" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#myAdd" data-id="NO" >NO</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    

    Then I call a php file using JScript which can handle the data-id perfectly..

        <script>
            $(document).on('click','#getAdd',function(e){
                e.preventDefault();
                var per_id=$(this).data('id');
                //alert(per_id);
                $('#add-data').html('');
                $.ajax({
                    url:'addLot_func.php',
                    type:'POST',
                    data:'id='+per_id,
                    dataType:'html'
                }).done(function(data){
                    $('#add-data').html('');
                    $('#add-data').html(data); 
                }).fail(function(){
                    $('#add-data').html('<p>Error</p>');
                });
            });
        </script>   
    

    In addLot_func.php:

        if(isset($_REQUEST['id'])){
            $id=intval($_REQUEST['id']);
    
            $reject = $_REQUEST['id'];
        }
        else $reject = "";
    
        if(substr($reject,0,3) == "YES")
        {
            ...
        }
        else
        {
            ...
        }