My objective with this code is when an user click in edit button, is sent this all=<?php echo $arr; ?> to the page edit.php .
This code simply does nothing (no ajax call in firebug).
<?php
$arr = array("one", "two", "three")
?>
<div id="content">
<input class="edit" type="submit" value="Edit" />
</div>
<script type="text/javascript">
$(document).ready(function () {
$(".edit").submit(function () {
$.ajax({
url: "edit.php",
type: "post",
dataType: "html",
data: "<?php echo json_encode( $arr ); ?>",
success: function (data) {
$('#ad').html(data);
}
});
});
});
</script>
Edit As @MilanJaric showed, you do need to operate on the
clickof the submit button. In addition, however, you should prevent the default action of the click as well. End EditYou need to echo the data json_encoded and wrapped in quotes:
IMO, however, it gets real ugly to echo PHP inside your JS especially if you have to do a lot of it, so when the need arises, I use an IIFE to inject PHP data into the code as a JS var. This allows me to isolate all PHP echoing within the parens of the invocation of the IIFE: