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

7
  • you can use ajax api.jquery.com/jquery.ajax Commented Dec 20, 2016 at 9:57
  • Either use AJAX or put value in a hidden input and submit the form Commented Dec 20, 2016 at 9: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 Commented Dec 20, 2016 at 10:04
  • you can send your javascript* array with ajax called in jquery like in documentation without php function Commented Dec 20, 2016 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 ? Commented Dec 20, 2016 at 10:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

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 <p> with this " success: function(){ $('.answer').html(itemMetaArray); }" but I can't get the array data in a PHP variable.. My webpage isn't reload
i edited data in my code, it's an other way to send data. if it does not work like you want look here : stackoverflow.com/questions/8890524/…
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...
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
Here is the picture : imgur.com/a/30Klz Here is my .php : timgu <script src="code.jquery.com/jquery-3.1.0.js"></script> <script type="text/javascript" src="/wordpress/script.js"></script> <?php echo "<script>getItemMetaList()</script>"; ?> <p class="answer"><?php print_r($_POST);?></p>
|
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.