skip to Main Content

This is my test.php code

<input type="button" value="click me" name="my_btn" onclick="mybtn();">

<?php
    echo("before is set");

    if(isset($_POST['hello'])=="dear"){
    echo("after is set true");
}
?>

<script src="vendor/jquery/jquery.min.js"></script>

<script>
function mybtn(){

        $.ajax({
            url:"http://localhost/mysites/php_two/test.php",
            method:"post",
            data:{hello:"dear"},
        });

    }
</script>

when button is clicked, page only shows
button icon + “before is set” text only.

But in Devtools network preview (in chrome browser), it shows
button icon +” before is setafter is set true”

please help me to fix this issue.

2

Answers


  1. I think this want minimum 2 PHP pages 1st for show data and 2nd for creating data

    show.php

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <input type="button" value="click me" name="my_btn" onclick="mybtn();">
    <div id="dis">before is set</div>
    <script>
       function mybtn(){
    
            $.ajax({
                url: "data.php",
                type: "POST",
                data:{hello:"dear"},
                success: function(result){
                    $("#dis").html(result);
                }
            });
    
        }
    </script>
    

    data.php

    <?php
        if(isset($_POST['hello']) && $_POST['hello']=="dear"){
            echo("after is set true");
         }
    ?>
    
    Login or Signup to reply.
  2. You can’t mix if(isset($_POST['hello'])=="dear") because isset() takes a variable and check if it is set and not NULL which returns a boolean instead use it separate like this:

    if(isset($_POST['hello']) && $_POST['hello']=="dear")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search