search

Home  >  Q&A  >  body text

Rendering data in a table using jQuery in PHP

<p>I have a data and I use PHP to access them. So I created a simple button and when I click it my program needs to create a table containing the data. <strong>Below you can see the jQuery code that reads the data and creates a table. </strong>But the problem is, I can't access every element in DATA. I want to clarify that since I've also added the code, I'm making a selection in the data. <em>It's called "household.php"</em></p> <pre class="brush:php;toolbar:false;"><html lang="en"> <head> <script src="https://code.jquery.com/jquery-3.6.2.js" integrity="sha256-pkn2CUZmheSeyssYw3vMp1 xyub4m e QK4sQskvuo4=" crossorigin="anonymous"></script> <link rel="stylesheet" href="style.css"> <script> function suchen() { jQuery(document).ready(function($) { $.ajax({ type: "get", url: "household.php", dataType: "json", success: function(response) { var data = JSON.parse(response) var html_table = ""; data.forEach(function(item) { var row = $("<tr>"); row.append($("<td>").text(item.contact_id_a)); row.append($("<td>").text(item.contact_id_b)); // add more cells for additional columns html_table = row[0].outerHTML; }); $("#tabelle").html(html_table); } }); }); } </script> </head> <body> <form id="form" onsubmit="suchen()" method="get"> <label>Enter your age: </label> <br /> <input type="number" name="min" min="0"> <br /> <input type="number" name="max" min="0"> <br /> <input type="submit"> </form> <div id="tabelle"></div> </body> </html></pre> <p><strong>This is the code for the family.php file. It works without problems. But I can't connect between my main php files. </strong></p> <pre class="brush:php;toolbar:false;"><?php require_once '/var/www/html/wordpress/wp-content/plugins/civicrm/civicrm/civicrm.config.php'; require_once 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); $relationships = \Civi\Api4\Relationship::get() ->addSelect('contact_id_a', 'contact_id_b', 'contact_id_a.display_name', 'contact_id_b.household_name', 'relationship_type_id') ->addClause('OR', ['relationship_type_id', '=', 7], ['relationship_type_id', '=', 8]) ->setLimit(25) ->execute(); foreach ($relationships as $relationship) { // do something } var_dump(json_encode($relationships)); ?></pre> <p>I cannot access the data using the php file. I also can't connect to my main php file by searching for php. </p>
P粉894008490P粉894008490447 days ago534

reply all(1)I'll reply

  • P粉038856725

    P粉0388567252023-09-05 00:44:06

    I think you have used echo json_encode($relationships) in your household.php file. You don't need to parse the response (which you used) because you already wrote the dataType to json. It will convert automatically. Hope this answer helps you. According to the jQuery Manual, "Any malformed JSON will be rejected and raise a parsing error. As of jQuery 1.9, empty responses will also be rejected." Therefore, you can just use dataType: 'json' if you are sure that the server will return correctly formatted JSON. If it just returns "a string that looks like JSON", you should use dataType: "text json" to force jQuery to convert. [Note] If the above answer doesn't help you, I recommend you to use this function to parse the response. var data = jQuery.parseJSON(response); or var data = $.parseJSON(response)

    reply
    0
  • Cancelreply