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);
?>
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...