0
$('select[name="third_category"]').change(function() {
        var first_cat = $('#main_category').val();
        var second_cat = $('#top_category').val();
        var third_cat = $(this).val();
        $.ajax({
            type:'POST',
            url:'ajax/fields_ajax.php',
            data:{'first_cat':first_cat, 'second_cat':second_cat, 'third_cat':third_cat},
            success:function(data){
                var field_box = document.getElementsByClassName('field_box');
                $(field_box).append(data);
            }
        });
    });

My goal is, when user select third option i collect all the option's values on the page. Then i'm going to use them for my mysql queries. That is my fields_ajax.php file.

<?php 
session_start();
ob_start();
include 'inc/session.php';
include 'inc/config.php';
include 'inc/mysql.class.php'; 
ini_set('display_errors', 0);

if( isset($_POST) ){ // secure script, works only if POST was made
    $first_cat   = $_POST[first_cat];
    $second_cat   = $_POST[second_cat];
    $third_cat   = $_POST[third_cat];

    $category_field_query = "SELECT * FROM categories WHERE id = $first_cat";
    $category_field_query_run = mysqli_query($connect, $category_field_query);
    $cat_field = mysqli_fetch_object($category_field_query_run);

    echo "<p>".$cat_field->fields."</p>";


 } ?>

I can carry all values without problem. But when i use them on query, there's no response. Mysqli connection working correctly, my query works on other php files. And when I try something else instead of trying mysql query, ajax file response it without problem.

Drierer Kas
  • 47
  • 1
  • 7
  • Separate your concerns. Diagnose your PHP script first, if that returns what you expect, then look at your AJAX request. – Devon Bessemer Jun 29 '18 at 12:56
  • `$(field_box).append(data);`, the jQuery function takes a css selector, a DOM node or html string as an argument, `field_box` is neither of these – Musa Jun 29 '18 at 12:58
  • @Devon PHP script works correctly on other php files. Also ajax request works correctly too. When i echo first_cat inside fields_ajax.php it returns correct value. – Drierer Kas Jun 29 '18 at 13:06
  • Just so you know, your query is not secure. Are you `ob_start()`ing for a reason? I don't like your jquery selector after success. Does that work? – mickmackusa Jun 29 '18 at 13:06
  • How many field_boxes are there? – mickmackusa Jun 29 '18 at 13:12
  • `mysqli_fetch_object($que_run);` Shouldn't this be `mysqli_fetch_object($category_field_query_run);`? – Dimitris Filippou Jun 29 '18 at 13:13
  • @mickmackusa Three. When user select first option, it triggers another ajax file which return related second select-box. And second one triggers last one. And last select-option changed, my query suppose to work. – Drierer Kas Jun 29 '18 at 13:16
  • Can we see the form / dom? – mickmackusa Jun 29 '18 at 13:18
  • @DimitrisFilippou Yepp, changed. Thanks for warning. But the problem is still going on :/ – Drierer Kas Jun 29 '18 at 13:19
  • Your SELECT uses `*` but you are only using a column called `fields`? Just so you know `isset($_POST)` will ALWAYS be `true`. You should quote your SUPERGLOBAL keys to avoid `constant` Warnings. – mickmackusa Jun 29 '18 at 13:22
  • @mickmackusa Noted. Thanks for advices. Form and main scripts. https://codepen.io/Zasetsu/pen/WyLZqV cat2_list.php which works after first select. https://codepen.io/Zasetsu/pen/eKbeYd And cat3_list.php which works after second select. https://codepen.io/Zasetsu/pen/MXZOWy – Drierer Kas Jun 29 '18 at 13:29
  • Return your imploded $_POST array to confirm reception. Then write some conditionals to check for syntax/logic errors in your query. – mickmackusa Jun 29 '18 at 13:29
  • So there is only one `field_box` then. Does the categories table contain a `fields` column? – mickmackusa Jun 29 '18 at 13:32
  • Definitely quote your keys in ALL of your scripts. https://3v4l.org/HcIkt – mickmackusa Jun 29 '18 at 13:38
  • @mickmackusa It does. As i said, when i run same query on different php file it works :/ – Drierer Kas Jun 29 '18 at 13:38
  • 1
    There arent many more places to look now. Echo the POSTed values. Echo mysqli_error. Echo your rendered query string. I would be writing: `$('.field_box').append(data);` – mickmackusa Jun 29 '18 at 13:39
  • I second `$('.field_box').append(data);` ... because if you are using jquery... use it to its advantages and ditch the longhand javascript selects ;) – IncredibleHat Jun 29 '18 at 13:42
  • @IncredibleHat do you know if the OP's selector can work? I've never seen it done like that (and never would have thought to try). – mickmackusa Jun 29 '18 at 13:46
  • @mickmackusa it works, sorta... jquery will take that sort of element group object to work on... but its really kinda janky. By wrapping the group in `$()` ... it puts jquery's methods on it, so you can do things like `.append`. Whereas doing `field_box.append` would NOT work, since it was not wrapped in jquery. However if made as `var field_box = $('.field_box);` ... then you COULD do `field_box.append`. LOL! – IncredibleHat Jun 29 '18 at 13:48
  • Well, then I guess the issue is in the POSTed variables because the connection and query works elsewhere. We will either need confirmation of the php page receiving the expected data or new details from the error log, or I'm just about ready to close as: Unclear / Typo, can't reproduce / Why isn't my code working – mickmackusa Jun 29 '18 at 13:54
  • @mickmackusa There's no error from mysqli_error. Also I echoed posted values. All of them are correct. And rendered query string is SELECT fields FROM categories WHERE id = 1 for $_POST["first_cat"] = 1. – Drierer Kas Jun 29 '18 at 14:07
  • Can you use `console.log(data);`? – mickmackusa Jun 29 '18 at 14:09
  • @mickmackusa It returns empty, not undefined. – Drierer Kas Jun 29 '18 at 14:15
  • Try another column that will be returned from `SELECT *`. Try `id` column – mickmackusa Jun 29 '18 at 14:16
  • What about isolating the object syntax with spaces or curly brackets? `"

    ". $cat_field->fields ."

    "` or `"

    {$cat_field->fields}

    "`?
    – mickmackusa Jun 29 '18 at 14:19
  • @mickmackusa Same, nothing. This time i tried to echo "

    ".$cat_field->fields."

    ". Ajax returns just p tags inside field_box correcty. And same for console log, only p tags. I cant get anything from database :/ I tried same method for id column.
    – Drierer Kas Jun 29 '18 at 14:20
  • Time to swap in another resultset function. http://php.net/manual/en/mysqli-result.fetch-assoc.php – mickmackusa Jun 29 '18 at 14:21
  • Or http://php.net/manual/en/mysqli-result.num-rows.php – mickmackusa Jun 29 '18 at 14:26
  • You have posted: `ini_set('display_errors', 0);` but we dont want to silence errors right now. – mickmackusa Jun 29 '18 at 14:29
  • @mickmackusa Deleted that line. And i tried other resultset functions. Still nothing. Its like it doesnt even try to fetch any data. – Drierer Kas Jun 29 '18 at 18:13
  • There must be an anomaly in your code that is not represented in your question. Please provide an untouched copy-paste of your best/latest script that includes echoing checkpoints and mysqli_ error checking conditions. – mickmackusa Jun 29 '18 at 22:41
  • Here's a general reference for checking for mysqli errors while using prepared statements with placeholders for security reasons: https://stackoverflow.com/a/43246696/2943403 – mickmackusa Jun 29 '18 at 22:53

0 Answers0