0

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

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    This code here: `$ll = $_SESSION['Confirm']; echo "";`looks like it's being executed before your AJAX code. It will execute on the server as soon as the previous lines of PHP code have run (the ones which output the AJAX code to the browser). It seems you aren't giving the user any time to respond to the confirm dialog before collecting the Session value. Don't forget that _all_ the PHP code will execute on the server before any of the JavaScript code is delivered to the browser. – ADyson Jul 20 '20 at 10:51
  • Sounds like you should perhaps go and have a good, thorough read of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) first of all. – CBroe Jul 20 '20 at 11:17

1 Answers1

0

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
    {
        ...
    }