0

-I have an array that contains more arrays in my php file. I am sending a integer variable (Possibly something else for sometime later that isn't the problem)

-I want php file to return array according to the incoming variable (in this example just 1 and 2.)

-I tried somethings from other posts but they were too complicated and I couldn't understand any of them. And I want to be able to change it whenever I want.

This is what I have done so far..

index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script type="text/javascript">     
var sayac = 1;
$(document).ready(function() {

    $(document).delegate("#klikme", "click", function() {
        $.post('sayac.php',{"komut": sayac }, function(data){

            var res = $.parseJSON(data);
            $('#output').html(res.result[0]);
            $('#output2').html(res.result[1]);
        });
        sayac = sayac + 1;
        if(sayac > 2)
            sayac = 1;
    });
});
</script>
<div id = "klikme">
    KLİK ME!
</div>

<div id = "output">
<?php include 'sayac.php'?>

</div>

<div id = "output2">

</div>

sayac.php

<?php 

    $returnThis = array( array("One" => "Sample Stuff Bla Bla this is one!","Two" => "Sample Text One"), 
                        array("One" => "Sample Stuff Bla Bla this is one!", "Two"=> "Sample Text Two"));

    if(isset($_POST["komut"]))
    {
        switch($_POST["komut"])
        {
            case 1:
                echo json_encode($returnThis[0]) ;
                break;
            case 2:
                echo json_encode($returnThis[1]); 
                break;
            default:
                echo "Something is wrong";
                break;
        }
    }

?>

I want to return array and in my index I want to fill each of my output div areas with returned array's indexes but these doesn't work

6
  • Echo the jsons inside your switch Commented Jun 28, 2015 at 14:58
  • You didn't explain what problem you are facing? Commented Jun 28, 2015 at 15:00
  • That was emberessing... Fixed it. Didn't work. I still can't get any response. Commented Jun 28, 2015 at 15:01
  • Yes sorry. I edited my question. @anant-kumar-singh Commented Jun 28, 2015 at 15:05
  • The problem is in the PHP or the JS? You should focus your question on the problematic issue Commented Jun 28, 2015 at 15:08

2 Answers 2

1

Just change your stuff like below:-

<script type="text/javascript">     

$(document).ready(function() {
var sayac = 1;
    $(document).delegate("#klikme", "click", function() {
        $.post('sayac.php',{"komut": sayac }, function(data){

            var res = $.parseJSON(data);
            console.log(res); // since response is an object so you cannot access in the way that you did.
            $('#output').html(res.One); // get first value
            $('#output2').html(res.Two); // get second value
        });
        sayac = sayac + 1;
        if(sayac > 2)
            sayac = 1;
    });
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Edit the javascript code like this:

$(document).ready(function() {

$('#klikme').click(function() {
    $.ajax({
        type: 'POST',
        url: 'sayac.php',
        data: 'komut='+sayac,
        dataType: 'json',
        cache: false,
        success: function(result) {
        $('#output').html(result.One);
        $('#output2').html(result.Two);
        }
    });
});

});

1 Comment

your code is correct but , what is the issue using $.post. this is latest and fast than $.ajax.

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.