1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to return a individual rows using ajax

Discussion in 'jQuery' started by Jassim Rahma, Jan 11, 2014.

  1. #1
    Hi,
    I am using jquery ajax with PHP and I would like to return individual rows instead of html. here is my code:

    
    $('#btnShowDetails').click(function(){
        $.ajax({
            url : "get_company_details.php",
            data : 'id=ed23e767-539f-11e3-aef0-74de2b9a31a4',
            type : "post",
            success : function(data)
            {
                $('#lblCompanyName').text(company name from data result);
                $('#lblCompanyWebsite').text(company website from data result);
            }
        });
    });
    
    Code (markup):
    and here is my get_company_details.php:

    
    <?php
        include('../includes/php_header.php');
        include($_SESSION["absolute_path"] . '/includes/connect2db.php');
       
        if (!isset($_SESSION["member_loggedOn"])) { exit(header("Location: ../signin")); }
       
        $mysql_query = $mysql_connection->prepare("CALL sp_get_company_details(:param_company_guid)");
        $mysql_query->bindParam(':param_company_guid', $_GET["id"], PDO::PARAM_STR);
        $mysql_query->execute();
    
        $mysql_row_count = $mysql_query->rowCount();
    
        if ($mysql_row_count <= 0) { exit(header("Location: " . $_SESSION["domain_name"] . "home")); }
    
        while ($mysql_row = $mysql_query->fetch())
        {
            $company_id = $mysql_row["company_id"];
            $company_guid = $mysql_row["company_guid"];
            $company_name = $mysql_row["company_name"];
            $company_industry = $mysql_row["industry_name"];
            $company_country = $mysql_row["country_name"];
            $company_region = $mysql_row["region_name"];
            $company_telephone = $mysql_row["company_telephone"];
            $company_fax = $mysql_row["company_fax"];
            $company_website = $mysql_row["company_website"];
            $company_email = $mysql_row["company_email"];
            $about_company = $mysql_row["about_company"];
            $hide_email = $mysql_row["hide_email"];
        }
    ?>
    
    Code (markup):
     
    Jassim Rahma, Jan 11, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    So? Look into using JSONencoded data, and parse that on return in the ajar function
     
    PoPSiCLe, Jan 11, 2014 IP
  3. webshore88

    webshore88 Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Use below code
    $out_data = array();
    $count = 0;
    while ($mysql_row = $mysql_query->fetch())
    {
        $out_data[$count]['company_id'] = $mysql_row["company_id"];
        $out_data[$count]['company_guid'] = $mysql_row["company_guid"];
        $out_data[$count]['company_name'] = $mysql_row["company_name"];
        $out_data[$count]['company_industry'] = $mysql_row["industry_name"];
        $out_data[$count]['company_country'] = $mysql_row["country_name"];
        $out_data[$count]['company_region'] = $mysql_row["region_name"];
        $out_data[$count]['company_telephone'] = $mysql_row["company_telephone"];
        $out_data[$count]['company_fax'] = $mysql_row["company_fax"];
        $out_data[$count]['company_website'] = $mysql_row["company_website"];
        $out_data[$count]['company_email'] = $mysql_row["company_email"];
        $out_data[$count]['about_company'] = $mysql_row["about_company"];
        $out_data[$count]['hide_email'] = $mysql_row["hide_email"];
        $count++;
    }
    echo json_encode($out_data);
    PHP:
    and user below code to json decode
    $.ajaxSetup({
        type:"post",
        url: "Paste Page URL Here"
    });
    $.ajax({
        data: '',
        success:function(response){
            var out_data = $.parseJSON(response);
            alert("Company Name: "out_data['company_name']);
        }
    });
    HTML:
     
    webshore88, Feb 7, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    You know, there are times you folks really make me laugh. Seriously:
    http://www.php.net/manual/en/pdostatement.fetchall.php

    I'm assuming from the colon in the query that $mysql_connection is a PDO object? (if so, dreadful to have it in the global space like that, much less the not so versatile and possibly inaccurate variable name...)

    <?php
    	include('../includes/php_header.php');
    	include($_SESSION["absolute_path"] . '/includes/connect2db.php');
     
    	if (!isset($_SESSION["member_loggedOn"])) {
    		exit(header("Location: ../signin"));
    	}
     
    	$statement = $mysql_connection->prepare('
    		CALL sp_get_company_details(:companyGuid)
    	);
    	$statement->execute([
    		':companyGuid' => $_GET["id"]
    	]);
    	if ($statement->rowCount() <= 0) {
    		exit(header("Location: " . $_SESSION["domain_name"] . "home"));
    	}
    	echo json_encode($statement->fetchAll());
    ?>
    Code (markup):
    Though really that exit(header) garbage? WHY are so many people playing these stupid redirect games? Just TRYING to waste handshakes or something?!?

    Oh, and beware, PDOStatement::rowCount is NOT reliable for SELECT. It might be better to do:

    <?php
    	include('../includes/php_header.php');
    	include($_SESSION["absolute_path"] . '/includes/connect2db.php');
     
    	if (!isset($_SESSION["member_loggedOn"])) {
    		exit(header("Location: ../signin"));
    	}
     
     /* I'm assuming from the : this is PDO? */
    	$statement = $mysql_connection->prepare('
    		CALL sp_get_company_details(:companyGuid)
    	);
    	$statement->execute([
    		':companyGuid' => $_GET["id"]
    	]);
    	$rows = $statement->fetchAll();
    	if ($rows && (count($rows) > 0)) {
    		echo json_encode($rows);
    	} else exit(header("Location: " . $_SESSION["domain_name"] . "home"));
    ?>
    Code (markup):
    Since fetchAll returns an empty array when there were no results, and false on failure.
     
    Last edited: Feb 7, 2014
    deathshadow, Feb 7, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    WAIT, is that ID unique?!? If so what are you looping for and/or the fetchAll in my version?

    Could we see the query being called?

    Gah, this is why snippets suck.
     
    deathshadow, Feb 7, 2014 IP