0

I have an array but can't complete the final "success" step. I want to populate an html page with array data retrieved from a MySQL query triggered by POST data sent via JQuery Ajax. Console.log shows the array content being returned, but I don't know how to get it for the final step (populate HTML page).

Normally, I use PHP to generate an array and include the HTML page I want to display it on. I like the speed of Ajax, and that the data can be passed without a submit button, but I am stuck. I hope there's enough info below to identify my error(s).

Thanks for any assistance.

Here's my Ajax:

// Get value of drop-down selection
$( "#agent_preview" ).change(function(){
    var broker_agent_id = $( this ).val();
    console.log(broker_agent_id);

    $.ajax({
        url: '../ajax/display-agent-profile.php',
        type: 'POST',            
        data: { broker_agent_id: broker_agent_id },
        cache: false,
        success: function(data){
            $("#preview").html(data);   
        }
    });           
});

Here's the PHP page (display-agent-profile.html.php) to process the Query:

<?php

if(isset($_POST['broker_agent_id'])){

    // Receive value of variable from ajax script @script.js and store in variable
    $broker_agent_id = htmlspecialchars($_POST['broker_agent_id']);

    // Connect to database
    include '../brokers/includes/dbconnect.php';


    // Get broker_id of agent using broker_agents.id
    try {
        $sql = "SELECT broker_id FROM broker_agents
                WHERE id = :id";
        $s = $db->prepare($sql);
        $s->bindValue(':id', $broker_agent_id);
        $s->execute();

        // Store query results in array
        $result = $s->fetch(PDO::FETCH_ASSOC);

        // Store broker id in variable
        $broker_id = $result['broker_id'];               
    } 
    catch (PDOException $e) {
        $errMsg = "Error fetching broker id from database " . $e->getMessage();
        include 'includes/error.html.php';
        exit();
    }

    // Retrieve data for agent based on broker_agents.broker_id and brokers.id (brokers.id = broker_agents.broker_id)
    try {
        $sql = "SELECT  brokers.id, broker_agents.profile_photo, broker_agents.first_name, broker_agents.last_name, brokers.company_logo, brokers.company_name, brokers.address1, brokers.address2, 
                        brokers.telephone, broker_agents.cell, broker_agents.agent_email, brokers.website, brokers.company_bio, broker_agents.about_me, brokers.services, broker_agents.affiliations, 
                        broker_agents.states_served, broker_agents.counties_served
                FROM    brokers
                INNER JOIN broker_agents
                ON      brokers.id = broker_agents.broker_id
                WHERE   broker_agents.id = :id
                AND     broker_agents.broker_id = :broker_id";
        $s = $db->prepare($sql);
        $s->bindValue(':id', $broker_agent_id);
        $s->bindValue(':broker_id', $broker_id);
        $s->execute();

        // Store agent results in associative array
        $agent = $s->fetch(PDO::FETCH_ASSOC); 
    } 
    catch (PDOException $e) {
        $errMsg = "Error fetching agent data from database " . $e->getMessage();
        include 'includes/error.html.php';
        exit();
    }

    // Disconnect 
    $db = NULL;       

    include 'profile-preview.html.php';
    exit();
}
?>
JimB814
  • 510
  • 8
  • 24
  • Well where do you want to append the data? What's the html? – TheDrot Jun 18 '16 at 21:05
  • Thanks for you reply. To an HTML page named "profile-preview.html.php" – JimB814 Jun 18 '16 at 21:25
  • Well that's not what I meant. :) Without HTML code I don't really know in which div or table or w/e you wan't the data in. – TheDrot Jun 18 '16 at 21:37
  • @mplungjan I challenge the use of "duplicate" and "exact duplicate." "The jungle isn't hard to navigate" said the man who lived 20 years in the jungle. I understand the purpose behind controlling duplication, but rushing to "duplication judgment" implies a lack of research on the part of the questioner. After 19 years answering here, and 30 years in IT perhaps you have forgotten the value of encouraging questions, rather than discouraging them. Everyone has areas of expertise. Experts should facilitate the learning of others who want to learn, which is demonstrated by asking questions. – JimB814 Jun 18 '16 at 21:39
  • TheDrot. Console.log shows the data. Best case for me is to have the array like I do in PHP. Then, I can do whatever I want. To move forward, I will place it into anywhere. I am open to suggestions. – JimB814 Jun 18 '16 at 21:42
  • Sorry to hear you felt discouraged- the success function of the duplicate has in my opinion all you need to proceed once you replace their variables with your own in this code: `success: function(response) { var json_obj = $.parseJSON(response); var output = ""; for (var i=0; i < json_obj.somearray.length; i++) { output += "
  • "+json_obj.somearray[i].somevar+"
  • "; } output += ""; $('#somecontsiner').html(output);` – mplungjan Jun 19 '16 at 04:33
  • Thanks for follow-up. I didn't need json. In the Ajax, I removed dataType and in success, I replaced ??? with my div ID ("preview"). In the PHP, I removed the echo line. I included the HTML file to display the data. Where I wanted the data to display, I added the div. It works. I am trying to understand Ajax in order to integrate more of it into my work. It's speed really improves the user experience. It's new (json even newer) to me, so I must struggle for clarity. Thanks for your assistance and expertise. I will seek to understand your script. – JimB814 Jun 19 '16 at 11:30