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?

6
  • 2
    How do you call correct function? Commented May 20, 2014 at 15:43
  • Quick fix: $this->Flip2->correctAnswer(89,$_GET['word']);. You can learn how to properly debug AJAX calls here Commented May 20, 2014 at 15:44
  • For a more full-featured AJAX usage explanation you can review this Commented May 20, 2014 at 15:48
  • @AmalMurali: This is how ` url: localhost/cake2/flip2/correct` Commented May 20, 2014 at 16:15
  • @RahilWazir: CakePHP. MVP Pattern. Commented May 20, 2014 at 16:23

2 Answers 2

1

You can just pass the word in the URL:

function increment(uniqueWord)
  {
  $.ajax({
      url: "http://localhost/cake2/flip2/correct/" + uniqueWord,
      //...
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that is what I did too.
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']);

    }

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.