0

Please have a look at the following code

....The Javascript code in ctp file...

<script type="text/javascript">
$(document).ready(function(){


   $( ".btn-danger" ).click(function(){
        console.log("Red Button");
        var toclose = $(this).parent().parent();
        $.ajax({
          url: "../img/media.jpg",
        }).done(function() {
            console.log( "The act has been done");
            toclose.toggle();
          });
   }); 


   $( ".btn-success" ).click(function(){
      console.log("Red Button");
      idOfButton = $(this).attr('id');
      var toclose = $(this).parent().parent();
      $.ajax({
        url: "../img/media.jpg",
      }).done(function() {
          increment(idOfButton);
            alert(idOfButton);
      console.log( "The act has been done");
          toclose.toggle();
      });
 }); 

  $( ".xy" ).click(function(){

    $(this).find("#enside1").toggle();
    $(this).find("#ptside1").toggle();
    console.log(this);
  });

      function xxx(id)
  {
      alert(id);
  }

  function increment(uniqueWord)
  {
      $.ajax({
          url: "http://localhost/cake2/flip2/correct",
                  data: {word: uniqueWord}

        }).done(function() {
            console.log( "The act has been done");
            toclose.toggle();
          });

    }

});


</script>

Please consider this function

function increment(uniqueWord)
  {
      $.ajax({
          url: "http://localhost/cake2/flip2/correct",
                  data: {word: uniqueWord}

        }).done(function() {
            console.log( "The act has been done");
            toclose.toggle();
          });

    }

Here I am trying to call a method in a PHP file. That method contains a parameter.

public function correct($word)
        {
            $this->Flip2->correctAnswer(89,$word);

        }

However there is an issue in passing the parameter values to the PHP method. It simply do not work. If I remove the parameters from "all" the places and just call the url: "http://localhost/cake2/flip2/correct", this works fine. What I have done wrong when passing the values to the PHP method via the Ajax call?

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
PeakGen
  • 21,894
  • 86
  • 261
  • 463

2 Answers2

1

You can just pass the word in the URL:

function increment(uniqueWord)
  {
  $.ajax({
      url: "http://localhost/cake2/flip2/correct/" + uniqueWord,
      //...
Dave
  • 28,833
  • 23
  • 113
  • 183
0

As Another solution you can change ajax call to be post request and get data in your controller from $this->request->data

 $.ajax({
      url: "http://localhost/cake2/flip2/correct",
      data: {word: uniqueWord},
      type: 'post'
    }).done(function() {
        console.log( "The act has been done");
        toclose.toggle();
      });

Controller will be

public function correct()
    {
        $this->Flip2->correctAnswer(89,$this->request->data['word']);

    }
Arman Petrosyan
  • 867
  • 13
  • 26