Re: [jgen] Fetch data from DB with ajax

47 views
Skip to first unread message
Message has been deleted

Mark Dexter

unread,
Apr 17, 2017, 2:39:55 PM4/17/17
to Joomla! General Development
You can debug the PHP side of the code using any PHP debugger. I use Eclipse with XDebug.

You can debug the Javascript side of things using the debugger built into Chrome or Firefox. You can also see the network XHR traffic in these browsers.

So you can see a complete picture of what is happening at each step.

On Mon, Apr 17, 2017 at 11:22 AM, <1996wiz...@gmail.com> wrote:
Im want to fetch data based on POST values ($data) by ajax.
Code:

if(isset($_POST['checkboxList'])){
$data = $_POST['checkboxList']; // Possible results: 1; 12; 123 (checkbox value)
}
if(isset($_POST['thisisajax'])){
$ajax_test = $_POST['thisisajax'];
}

if($ajax_test){
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('*');
    $query->from($db->quoteName('#__products'));
    $query->where($db->quoteName('id') . ' = (', implode(",",$data));
    // var_dump($query->__toString()); Doesnt work... Why?
    $db->setQuery($query);
    $results = $db->loadObjectList();
    $response = $results;

    echo new JResponseJson($response);
    jexit();
}

<form method="post" action="" name="form1" id="mycustomform">
    <input type="checkbox" name="checkboxList[]" onclick="function1();" value="1"> Apple</input><br>
    <input type="checkbox" name="checkboxList[]" onclick="function1();" value="2"> Pineapple</input><br>
    <input type="checkbox" name="checkboxList[]" onclick="function1();" value="3"> Pen</input>
</form>
<div id="response"></div>

<script>
function function1() {
    var data = jQuery("#mycustomform").serialize() + '&thisisajax=1';
    jQuery.ajax({
        url: "<?php echo JUri::current(); ?>", 
        type: "POST",
        data: data, 
        success: function(response){ 
            response = jQuery.parseJSON(response);
            if(response.success === true){
               jQuery('#response').html(response.data);
            } else {
                alert('error');
            }
        }
    });
}
</script>


I've two questions:
1) How can I debug my code? I cant do var_dumps for queries ect. Is it because ajax? How can I work around this?
2) Why I get this error? "Uncaught SyntaxError: Unexpected token < in JSON at position 0"
Is something wrong with the query? How to fix these kind of bugs, when u cant do var_dumps?

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-dev-general@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Hannes Papenberg

unread,
Apr 17, 2017, 2:45:03 PM4/17/17
to joomla-de...@googlegroups.com
And this is a very good example why I wrote that you should not try to
invent your own clever way to do things. Your code does not work because
you are calling a module and not a component and thus have a lot of
different output in your return, even if you do jexit(). Which is a bad
idea. You also have an SQL injection in your code that allows to take
full control of your system. I don't understand why you are not using
com_ajax as we told you several times already. Please don't tell me that
you are supporting pre-3.6 Joomla versions, considering that none of
them are supported and all of them have serious security issues.

Hannes
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages