1

I want to know if I can get the return of a JQUERY function, in PHP ?

I have this following jquery function, returning an array and I want to get this array in my PHP, in order to send it to process the data

function getItemMetaList() {
        var itemMetaArray = [];
        $('.frm_pro_form input, .frm_pro_form select, .frm_pro_form textarea').each(function(cpt){
            if($(this).attr('type') != 'hidden' && ($(this).attr("type") != "submit")) {
                console.log(cpt);
                console.log($(this).attr("name"));
                itemMetaArray.push($(this).attr("name"));
            }
        });
        return itemMetaArray;
    }

Thanks in advance

Nathan30
  • 689
  • 2
  • 8
  • 29
  • you can use ajax http://api.jquery.com/jquery.ajax/ – Frankich Dec 20 '16 at 09:57
  • Either use AJAX or put value in a hidden input and submit the form – RiggsFolly Dec 20 '16 at 09:57
  • The problem with AJAX is that normally you use it with PHP functions right ? I don't see how I can send JQUERY data to my PHP file with AJAX. For the input method I can't do that, I have to get the JQUERY array with a button click in my php page – Nathan30 Dec 20 '16 at 10:04
  • you can send your javascript* array with ajax called in jquery like in documentation without php function – Frankich Dec 20 '16 at 10:17
  • Yeah I finally find it. I just have a problem. I send an array, but I didn't get an array in PHP, just the content of the array. Any solutions for this ? – Nathan30 Dec 20 '16 at 10:21
  • what do you mean by the "just the content" $_POST isn't an array ? – Frankich Dec 20 '16 at 10:23
  • I mean : My array looks like this : Array [ "item_meta[71]", "item_meta[65]", "item_meta[70]", "item_meta[66]", "file67" ] How can I get the same result in my PHP page ? While I'm using (var_dump($_POST)) I have this result : array (0) { } – Nathan30 Dec 20 '16 at 10:26

2 Answers2

1
function getItemMetaList() {
    var itemMetaArray = [];
    $('.frm_pro_form input, .frm_pro_form select, .frm_pro_form textarea').each(function(cpt){
        if($(this).attr('type') != 'hidden' && ($(this).attr("type") != "submit")) {
            itemMetaArray.push($(this).attr("name"));
        }
    });
    $.ajax({
       method: "POST",
       url: "some.php",
       //data: { array: itemMetaArray}
       data: JSON.stringify({ array: itemMetaArray})
    }).done(function( msg ) {
       alert( "Data Saved: " + msg );
    });
}

you can send data by this way in a php file like documented here: ajax jquery

Frankich
  • 842
  • 9
  • 19
  • This is what I've done, thanks for the answer. But now the only problem is to get the array in PHP. For now the only things I've done is to display the content of the array in a

    with this " success: function(){ $('.answer').html(itemMetaArray); }" but I can't get the array data in a PHP variable.. My webpage isn't reload

    – Nathan30 Dec 20 '16 at 10:32
  • i edited data in my code, it's an other way to send data. if it does not work like you want look here : http://stackoverflow.com/questions/8890524/pass-array-to-ajax-request-in-ajax – Frankich Dec 20 '16 at 10:33
  • I already read this topic, but without a good solution. In my network tab I can see the POST request, with the content of the itemMetaArray. But the var_dump($_POST) still return an empty array... – Nathan30 Dec 20 '16 at 10:40
  • it's a quite stange , i never get this kind of problem, can you post what you have in the network tab ? the content of itemMetaArray – Frankich Dec 20 '16 at 10:45
  • Here is the picture : http://imgur.com/a/30Klz Here is my .php : timgu getItemMetaList()"; ?>

    – Nathan30 Dec 20 '16 at 10:46
  • remove all except in the index.php . if it return nothing or just an empty array , i can't help you more sorry – Frankich Dec 20 '16 at 10:52
  • just one other thing : your itemMetaArray get a wrong format in the picture – Frankich Dec 20 '16 at 10:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131038/discussion-between-nathan30-and-macbooc). – Nathan30 Dec 20 '16 at 11:02
1

The Ajax request returns data for JavaScript in the page loaded in your browser. The browser does not display the data automatically. The answer is captured by .done() in MacBook's example. You can put JavaScript in the done () method to display the data returned. If you wish the page to reload in your browser, Ajax is not the good way. In JavaScript, use form.submit instead. Then you can have your PHP code read the submitted data and generate a new html page and have this displayed by the browser.

PaulH
  • 2,918
  • 2
  • 15
  • 31
  • Yes you're right, I saw in the "network" tab of Developper console that the "response" sub panel shows the array returned, while I see an empty array in the page – Nathan30 Dec 20 '16 at 12:55