2

Is this possible? I want to output what only inside the isset variable on the same page.. I've had my research by up to this point, I can't find the solution for this kind of problem.

How can I get the data that is inside the isset, it displays data but it gets the whole page as output.

<?php session_start(); ?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="jquery.10.2.js"></script>
<script type="text/javascript">

$(function(){
    $('.a').click(function(){
        var id = $(this).attr('data-id');
        alert(id);
        $.ajax({
            type: 'post',
            url: 'index.php',
            data:{
                connect: 'try',
                idx : id
            },
            success:function(data){
                $('span').html(data);   
            }
        });
    });
});

</script>
 </head>

<body>

<a href="#" data-id="1" class="a">Click 1</a>
<a href="#" data-id="2" class="a">Click 2</a>
<a href="#" data-id="3" class="a">Click 3</a>
<a href="#" data-id="4" class="a">Click 4</a>


<span></span>
<?php

$con = mysqli_connect("localhost", "root", "", "finance");
if(isset($_POST['connect'])){

    $x = array();
    $data = mysqli_query($con, "SELECT lname FROM tbl_student WHERE id = '$_POST[idx]'");
    while ($row = mysqli_fetch_array($data)) {
        $x[] = $row;

    }
    echo 'this is a test echo';
}
?>
</body>
</html>
1
  • Though this is possible, it's difficult for me to imagine that - apart from needing to solve this for a quiz question - that there isn't a better way to accomplish what you seek. Commented May 25, 2014 at 2:32

1 Answer 1

1

I don't know why you think you have to use the same page; there is probably not a good reason for that. But I think this might be what you have in mind:

<?php session_start(); ?>
<?php

$con = mysqli_connect("localhost", "root", "", "finance");
if(isset($_POST['connect'])){

    $x = array();
    $data = mysqli_query($con, "SELECT lname FROM tbl_student WHERE id = '$_POST[idx]'");
    while ($row = mysqli_fetch_array($data)) {
        $x[] = $row;

    }
    echo 'this is a test echo';
} else {
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="jquery.10.2.js"></script>
<script type="text/javascript">

$(function(){
    $('.a').click(function(){
        var id = $(this).attr('data-id');
        alert(id);
        $.ajax({
            type: 'post',
            url: 'index.php',
            data:{
                connect: 'try',
                idx : id
            },
            success:function(data){
                $('span').html(data);   
            }
        });
    });
});

</script>
 </head>

<body>

<a href="#" data-id="1" class="a">Click 1</a>
<a href="#" data-id="2" class="a">Click 2</a>
<a href="#" data-id="3" class="a">Click 3</a>
<a href="#" data-id="4" class="a">Click 4</a>


<span></span>
</body>
</html>
<?php
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.