1

I am returning a json_encode object to an ajax call. First off, is there a better way to do this? Is this json_encode needed?

To the root of my question. When I try to get to make the key a variable, it throws a "not defined error". This line: var displayTriggers = trigger_rows;

Does anyone see what I'm doing wrong?

PHP:

try {
    $con = getConfig('pdo');
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql_triggers = "
        SELECT *
        FROM triggers
    ";
    $triggers_stmt = $con->prepare($sql_triggers);
    $triggers_stmt->execute();
    $triggers_rows = $triggers_stmt->fetchAll(PDO::FETCH_ASSOC);
    $triggers_arr = array();
    foreach ($triggers_rows as $triggers_row) {
        $trigger_id = $triggers_row['id'];
        $trigger_title = $triggers_row['trigger_name'];
        $trigger_setting = $triggers_row['setting'];
        $trigger_user = $triggers_row['user_id'];
        $trigger_placement = $triggers_row['placement'];
        $trigger_date = $triggers_row['date_changed'];
        $trigger_active = ( $trigger_setting == '1' ) ? ' active' : '';
        $html = '';
        $html .= '<div class="triggerRow" data-placement="'.$trigger_placement.'">';
        $html .= '<div class="triggerRowLeft">';
        $html .= '<div class="triggerTitle">' . $trigger_title . '</div>';
        $html .= '<div class="triggerText">' . $trigger_date . '</div>';
        $html .= '<div class="triggerText">' . $trigger_user . '</div>';
        $html .= '</div>';
        $html .= '<div class="triggerRowRight">';
        $html .= '<div class="triggerButton' . $trigger_active . '"></div>';
        $html .= '</div>';
        $html .= '</div>';
        $data = array('html' => $html);
        $triggers_arr[] = $data;
    }
    echo json_encode(['trigger_rows' => $triggers_arr]);
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

JS:

$wrapper = $('#triggerCont');
$.ajax({
    url: 'php/triggerSelect.php',
    dataType: 'json',
    success: function (data) {
      //console.log(data);
        if (data == null) {
            alert("Unable to retrieve triggers!");
            alert(data);
        } else {
            var displayTriggers = trigger_rows;
            $wrapper.empty();
            $(displayTriggers).each(function() {
                $wrapper.append(this.html);
                //console.log(this.html);
            });
        }
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " | " + errorThrown);
        alert('There are currently no project images for this selection');
    }
});
8
  • 2
    You get the data returned to the javascript in a variable called data from success: function (data) so try using data. rather than trigger_rows Commented Dec 5, 2019 at 15:04
  • 1
    Simple DEBUG idea. All browsers come with a debugger (F12). Load that and set a breakpoint on if (data == null) { and then check out what the object data actually contains Commented Dec 5, 2019 at 15:06
  • @RiggsFolly Doesn't this act as a breaking point though? alert(data); within the data == null statement? Commented Dec 5, 2019 at 15:13
  • It shows you data, stops the code executing while it does so. but as you are not in the debugger you cannot decide to step through the code line by line for example, also a good way to spot those little woopsies we all make Commented Dec 5, 2019 at 15:14
  • @Paul btw alert(data) when data === null is pointless, as it will alert null ;-) if you want to debug your db-php just return encoded json too like:'error'=> true, 'message' => "..." and check in js for if(data.error) Commented Dec 5, 2019 at 15:20

1 Answer 1

2
var displayTriggers = data.trigger_rows;

should do the trick

Sign up to request clarification or add additional context in comments.

2 Comments

Gotcha! I didn't even think about this. I used to do parse the data and use the obj, so just getting used to this method.
gotcha! Next time just check the console-output of the commented out console.log(data) , as you set dataType option to json it will automatically convert the string to json (json is already a js-object "stringified json" is just text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.