esjay
esjay

Reputation: 165

CakePHP Friendsofcake search ajax results

sorry if I am missing something simple here. I am using CakePHP 3 and the Friendsofcake Search plug-in and trying to load my results with AJAX. I am not sure what to set for the URL - my understanding is the FormHelper url and the AJAX url must match for SecurityComponent. However even with that disabled I cannot get the form to submit. Any help is appreciated. The plugin is working fine otherwise and I can submit other forms using AJAX just fine - I suspect I am missing something here (or it's not possible - I am a beginning programmer)

<?php
 $formUrl='//'.$_SERVER['HTTP_HOST'].Router::url(['controller'=>'Treasures','action'=>'frontIndex']);
echo $this->Form->create('Treasure',['id'=>'myForm','url'=>$formUrl]);
echo $this->Form->input('q');
... (Form submit, end, etc.)
?>

 <script>
$( "#myForm" ).submit(function( event ) {
   event.preventDefault();
    $.ajax({
    async:true,
    data:$(this).serialize(),
    dataType:"html",
    success:function (data, textStatus) {
       $(".ajax-result").html(data);
    },
    type:"POST",
    url:"<?=$formUrl?>"
   });
 </script>

<div class="ajax-result"></div>

Can someone tell me what I should be setting for the $formUrl? Currently the Controller action I am using this on successfully filters data with the search plugin and I have specialized the view to return AJAX results when requested - but there is obviously something else going on I am missing.

Upvotes: 0

Views: 89

Answers (1)

esjay
esjay

Reputation: 165

This works if I leave the URL blank and use GET instead of POST - should've thought of that sooner.

echo $this->Form->create('Treasure',['id'=>'myForm']);
...
?>

 <script>
$( "#myForm" ).submit(function( event ) {
   event.preventDefault();
   $.ajax({
     async:true,
     data:$(this).serialize(),
     dataType:"html",
     success:function (data, textStatus) {
       $(".ajax-result").html(data);
     },
     type:"GET"
    });
 </script>

Works as expected. Hope this helps someone!

Upvotes: 0

Related Questions