Forums

jquery ajax eventi url

fasenderos 10 Mar, 2015
Hi,
I create an event called 'ajax' that make some stuff..
I want to call this event with a jquery-ajax (JS action) in the on load event
What is the url?
I try with full url + tvout=ajax like this:
http://www.mysite.com/index.php?option=com_chronoforms5&chronoform=form-name&tvout=ajax
But i get an 'Unexpected Token <'
Using simple url: 'test.php' it works
Thanks
GreyHead 10 Mar, 2015
Hi fasenderos,

Where have you put the URL?

What code is the error linked to exactly?

Bob
fasenderos 10 Mar, 2015
in a 'Load Javascript' action on load:

jQuery(document).ready(function ($) {
//#select_item is a Dropdown
$('#select_item').on('change', function() {
$.ajax({
                        type: 'post',
                        url: 'http://www.mysite.com/index.php?option=com_chronoforms5&chronoform=form-name&tvout=ajax',
                        data: {id: $(this).val()},
                        dataType: 'json',
                        success: function (data) {
                                console.log(data);
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
  console.log(textStatus, errorThrown);
}
                });
        });
});
fasenderos 10 Mar, 2015
The error is linked to console.log(data) in the success function
GreyHead 12 Mar, 2015
Hi fasenderos,

Does it work if you comment out the console.log() line?

Bob
fasenderos 13 Mar, 2015
Hi bob,
No it doesn't.

That's my Ajax event setup:
- DB Read with Model ID = Items and this where conditions:

<?php
 return array( 'id' => $_POST['id'] );
?>

- Custom Code

<?php
echo json_encode($form->data['Items']);
?>

That's the error from the console log:
parsererror SyntaxError: Unexpected token < {stack: (...), message: "Unexpected token <"}


To solve that problem I create an external php file to process the ajax request with this code:

define( '_JEXEC', 1 );
$path = '/reverted';
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] . $path);
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$app = JFactory::getApplication('site');
$app->initialise();	
$id = $_POST['id'];
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('title', 'description', 'price')));
$query->from($db->quoteName('#__mytable'));
$query->where($db->quoteName('id')." = ".$db->quote($id)); 	
$db->setQuery($query);
$result = $db->loadObject();	
echo json_encode($result);


But I would prefer it to be all managed by Chronoforms without external php file.
thanks
GreyHead 14 Mar, 2015
Hi fasenderos,

I believe that AJAX uses GET, not POST so your DB Read won't return anything. Try using this instead:
<?php
 return array( 'id' => $form->data['id'] );
?>

Bob
fasenderos 14 Mar, 2015

I believe that AJAX uses GET, not POST so your DB Read won't return anything


I don't think so..cause in my custom php file I use this $id = $_POST['id'] and this is the jquery-ajax code:

$.ajax({
    type: 'post',
    url: 'http://www.mysite.com/index.php?option=com_chronoforms5&chronoform=form-name&tvout=ajax',
    data: {id: $(this).val()},
    dataType: 'json',
    success: function (data) {
        console.log(data);
    }
});

however I have tried with your code but same error😟

Since I got the result with an external php file, now has become a matter of principle.
I want to simply understand why it does not work, so I did some tests.
I tried by removing the DB Read and use a Custom Code with the same code of the external php file:

	$id = $_POST['id'];
	$db = JFactory::getDbo();
	$query = $db->getQuery(true);
	$query->select($db->quoteName(array('title', 'description', 'price', 'private_business', 'payment_method', 'shipping_method', 'item_condition')));
	$query->from($db->quoteName('#__chronoengine_chronoforms_items'));
	$query->where($db->quoteName('id')." = ".$db->quote($id)); 
	$db->setQuery($query);
	$result = $db->loadObject();	
	echo json_encode($result);

No way.

The only question that comes to me is: is this the right url to call the ajax event?? http://www.mysite.com/index.php?option=com_chronoforms5&chronoform=form-name&tvout=ajax
GreyHead 14 Mar, 2015
Hi fasendero,

You may need the event name in the URL as well &event=ajax (assuming that ajax is the event name). If that is omitted then the load event will be used.

Bob
fasenderos 14 Mar, 2015
Hi bob,
I tried with this URL http://www.mysite.com/index.php?option=com_chronoforms5&chronoform=form-name&event=ajax
Same error ..i think the only way is to point the ajax request to external php file
Thank you bob for your time, much appreciated
This topic is locked and no more replies can be posted.