AJAX - Retour de deux objets JSON avec une fonction PHP
3 réponses
- votes
-
- 2013-07-26
Sije comprends correctement,vous devez lesenvelopper dans untableauet passer à Javascript.
function get_ldap_attr() { header("Content-type: application/json"); ... // suppose you want to return $second_var echo json_encode(array($sup,$second_var)); die(); }
Lafonction de succès de la requête JSON les recevra sousforme detableau. Vouspouvez y accéder avec unindexentier.
jQuery(function() { jQuery('#empLanId').on('blur', function() { var lan = jQuery('#empLanId').val(); var ajaxurl = '<?php echo admin_url("admin-ajax.php", null); ?>'; var data = { action: "get_ldap", lan: lan}; jQuery.ajax({ type: 'POST', url: ajaxurl, data: data, dataType: 'json', success: function(response) { response[0] // first $sup variable response[1] // second $second_var variable //jQuery('#empSupLanId').val(response.lanid); //jQuery('#empSupName').val(response.fullname); //jQuery('#empSupNumber').val(response.phone); } }); }); });
If I get it correctly, You need to wrap them in an array and pass to Javascript.
function get_ldap_attr() { header("Content-type: application/json"); ... // suppose you want to return $second_var echo json_encode(array($sup,$second_var)); die(); }
Success function of JSON request will receive them as array. You can access them with integer index.
jQuery(function() { jQuery('#empLanId').on('blur', function() { var lan = jQuery('#empLanId').val(); var ajaxurl = '<?php echo admin_url("admin-ajax.php", null); ?>'; var data = { action: "get_ldap", lan: lan}; jQuery.ajax({ type: 'POST', url: ajaxurl, data: data, dataType: 'json', success: function(response) { response[0] // first $sup variable response[1] // second $second_var variable //jQuery('#empSupLanId').val(response.lanid); //jQuery('#empSupName').val(response.fullname); //jQuery('#empSupNumber').val(response.phone); } }); }); });
-
- 2018-11-07
CodejQuery:
(function ($) { 'use strict'; $(function () { var value_1 = 10; var value_2 = 20; $('.btn').on('click', function(){ $.ajax({ url: 'your_ajax_url_here', type: 'POST', data: { action : 'action_name', val_1 : value_1, val_2 : value_2, }, beforeSend: function () { console.log('Sending....'); }, success: function (response) { var obj = JSON.parse(response); console.log(obj); }, error: function (errorThrown, status, error) { console.log( status ); } }); }) }); })(jQuery);
Code PHP:
function get_ldap_attr() { $obj_val_1 = $_POST['val_1']; $obj_val_2 = $_POST['val_2']; echo json_encode(array( 'obj_1' => $obj_val_1, 'obj_2' => $obj_val_2, )); }
C'esttout.
jQuery Code:
(function ($) { 'use strict'; $(function () { var value_1 = 10; var value_2 = 20; $('.btn').on('click', function(){ $.ajax({ url: 'your_ajax_url_here', type: 'POST', data: { action : 'action_name', val_1 : value_1, val_2 : value_2, }, beforeSend: function () { console.log('Sending....'); }, success: function (response) { var obj = JSON.parse(response); console.log(obj); }, error: function (errorThrown, status, error) { console.log( status ); } }); }) }); })(jQuery);
PHP Code:
function get_ldap_attr() { $obj_val_1 = $_POST['val_1']; $obj_val_2 = $_POST['val_2']; echo json_encode(array( 'obj_1' => $obj_val_1, 'obj_2' => $obj_val_2, )); }
That's all.
-
- 2018-07-26
<script type="text/javascript"> $(document).ready(function(){ //jquery script $("#emp_mat").change(function(){ var emp_mat = $(this).val(); $.ajax({ url:"emp_info.php", dataType:'json', data:{data:emp_mat} }).done(function(result) { $("#nom_prenom").val(result[0]); $("#adresse").val(result[1]); }) }); }); </script> <input type="text" id="nom_prenom" disabled="true" size="58"/> <input type="text" id="adresse" disabled="true" size="58"/>
<script type="text/javascript"> $(document).ready(function(){ //jquery script $("#emp_mat").change(function(){ var emp_mat = $(this).val(); $.ajax({ url:"emp_info.php", dataType:'json', data:{data:emp_mat} }).done(function(result) { $("#nom_prenom").val(result[0]); $("#adresse").val(result[1]); }) }); }); </script> <input type="text" id="nom_prenom" disabled="true" size="58"/> <input type="text" id="adresse" disabled="true" size="58"/>
-
Veuillez ** [modifier] votre réponse **,et ajouter uneexplication: **pourquoi ** celapourrait-il résoudre leproblème?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2018-07-26
- fuxia
Commentpourrais-je retourner deux objets JSON avec un appel AJAXet unefonction PHP? Toute aideesttrès appréciée!
Voici lafonction PHP:
Et lejQuery: