1

I'm getting stuck here.

I'm trying to trigger ajax when a div is selected and then get a JSON response. All I've been able to return is the error code, and I'm not sure why. Here's the simplified version (still getting same response as the actual). Maybe there's a better way? Thanks for your help.

jquery:

<html lang="en">
<meta charset="utf-8">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<style>
  #selectable .ui-selecting { background: #FECA40; }
  #selectable .ui-selected { background: #F39814; color: white; }
  #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
</style>

<script>

  $(function(){


$("#selectable").selectable({
  selected : function(event, ui) {
    if($(ui.selected).hasClass('ui-widget-content')){
         var rptID = ($(ui.selected).val());
             $.ajax(
   {

      type: "POST",
      dataType: "json",
      url: "hello2.php",
      success: function( response ) 
      { 

        alert(1);
      },
      error: function( error )
      {
        console.log(error);


      }

   } );




    } 
    }
});

});
  </script>

  <body><ol id="selectable">
  <li class="ui-widget-content" value=1 >Item 1</li>
  <li class="ui-widget-content" value=2>Item 2</li>
  <li class="ui-widget-content" value=3>Item 3</li>
</ol></body>
</html>

php:

<?php
 $json = array('hello' => 'hello');
    header('Content-Type: application/json');
        echo json_encode($json); 
 ?>
17
  • Have you tried to remove the header line in the php file? Commented Aug 19, 2014 at 15:26
  • alert(response): maybe? Commented Aug 19, 2014 at 15:27
  • What error are you getting? Commented Aug 19, 2014 at 15:27
  • Are there error in browser console? F12 Commented Aug 19, 2014 at 15:27
  • Doesn't make a difference. Commented Aug 19, 2014 at 15:27

1 Answer 1

3

php?> is going to cause a syntax error/warning at the PHP level, and that syntax error output will probably get tagged onto the end of your JSON output, causing a JSON parse error on the client.

PHP code blocks are simply <?php ... ?>.

e.g.

[marc ~]$ cat z.php
<?php
echo json_encode('foo');
php?>
[marc ~]$ php z.php
"foo"PHP Notice:  Use of undefined constant php - assumed 'php' in /home/marc/z.php on line 3

edit --- and now I see OP's edited away the php?> stuff...

Sign up to request clarification or add additional context in comments.

1 Comment

I had made a comment to that effect (prior to your answer); OP responded afterwards saying it was a typo. I didn't even get a +1 for that and you got 2 lol - Now you need to edit your answer.

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.