Kordian Wikliński
Kordian Wikliński

Reputation: 55

run ajax under jquery on CakePHP

I'm tryin to run my question.php from ajax using jquery. This piece of code is perfectly working

<script>
$(':radio').click(function() {
  if($(':radio:checked').length === 1) {
    var val = $(this).val();
    alert(val);
  }
});
</script>

but when instead of

alert(val);

i'm putting ajax code, here same sample with change

<script>
$(':radio').click(function() {
  if($(':radio:checked').length === 1) {
    var val = $(this).val();
    $.ajax({
        type: "POST",
        url: "questions.php",
           //data: {"a":val}
        });
  }
});
</script>

ajax doesn't run questions.php

questions.php is located in webroot catalog

Edit: it looks like it return my all page, but no change in my table occur questions.php is responsible for insert data into mysql table

questions.php

<?php
    $selected_button = $_POST['a'];
    $conn = mysql_connect('localhost','root','');
      if(!$conn )
       {
         die('Could not connect: ' . mysql_error());
       }

   $sql = "INSERT INTO answers (member_id, question_id, answer) VALUES (1, 2,3)";

   mysql_select_db('ipad',$conn);
   $result = mysql_query($sql, $conn);
   if(!$result)
     {
        return false;
     }
   return true;

   mysql_close($conn);
 ?> 

PROBLEM SOLVED problem was with pointing a file

<script>
$(':radio').click(function () {
        if ($(':radio:checked').length === 1) {
            var val = $(this).val();
            $.ajax({
                type: "POST",
                url: "../../../questions.php",
                data: {"a": val}
            })
                .error(function (msg) {
                    alert(msg);
                })
                .done(function (msg) {
                    alert(msg);
                });
        }
    });
</script>

Upvotes: 0

Views: 100

Answers (2)

sas
sas

Reputation: 2597

try below code, url need to be correct. check cakephp routing what is the correct format like, controller/action.

  $(':radio').click(function () {
            if ($(':radio:checked').length === 1) {
                var val = $(this).val();
                $.ajax({
                    type: "POST",
                    url: "questions.php",
                    data: {"a": val}
                })
                    .error(function (msg) {
                        alert(msg);
                    })
                    .done(function (msg) {
                        alert(msg);
                    });
            }
        });

my test code

<script src="jqueryPATH"></script>
<script>
    $(document).ready(function(){
        $('#hello').on('click', function () {
            $.ajax({
                type: "POST",
                url: "questions.php",
                data: {"a": 1}
            })
                .error(function (msg) {
                    alert(msg);
                })
                .done(function (msg) {
                    alert(msg);
                });
        });
    });

</script>
<a href="#" id="hello">hello</a>

questions.php

<?php
$selected_button = $_POST['a'];
echo $selected_button;

Upvotes: 1

alabount
alabount

Reputation: 56

You need to put the code you want to run in a function in a controller and then use the url field to call that function. So for example, if you put your code in the test function of questions_controller.php it would be

url: '/questions/test'

Upvotes: 0

Related Questions