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):
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:
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.
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.