Javascript POST not working - Sending Javascript array to PHP

前端 未结 4 664
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 08:05

I know there a fair few entries on SO and the web on this however I just can\'t get to work - any help would be appreciated.

So i have an array in Javascript which I

相关标签:
4条回答
  • 2020-11-27 08:30

    You have a misunderstanding about how ajax works. Although jquery makes it easy, it is still not automatic. You should just find a tutorial about ajax with jquery, but if you want to just send an array to php and see the output on screen, something like this would work:

    index.php

    <html>
    <head>
    <title>Test</title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        //attach to the button a click event
        $('#btn').click(function(){
                //get the value from the textbox
            var txt=$('#txt').val();
                //if txt is blank, alert an error
            if(txt == ''){
                alert("Enter some text");
            } else {
                        //send txt to the server
                        //notice the function at the end. this gets called after the data has been sent
                $.post('catcher.php', {'text':txt}, function(data){
                                //now data is an object, so put the message in the div
                    $('#response').text(data.message);
                }, 'json');
            }
        });
    });
    </script>
    </head>
    <body>
    <input type="text" id="txt">
    <input type="button" id="btn">
    <pre id="response" style="overflow:auto;width:800px;height:600px;margin:0 auto;border:1px solid black;">&nbsp;</pre>
    </body>
    </html>
    

    catcher.php:

    <?php
    //if something was posted
    if(!empty($_POST)){
        //start an output var
        $output = array();
    
        //do any processing here.
        $output['message'] = "Success!";
    
        //send the output back to the client
        echo json_encode($output);
    }
    

    It is better to use 2 files, one for the user to load that initiates the ajax call and one page to handle the ajax call. Sending an array works the same, just replace getting the textbox value with sending an array.

    0 讨论(0)
  • 2020-11-27 08:32

    This is what happens when you open your page (index.php)

    1. A GET request is issued to index.php and the content is returned. There are no values in the $_POST array so your print_r() line does nothing.
    2. Javascript is executed that sends a POST request to index.php via AJAX. Note that this is an entirely new request, separate to the original GET. The $_POST array will be populated on this request however the response is discarded.

    Hopefully this will illustrate what you can do.

    ajax.php

    <?php echo json_encode($_POST);
    

    index.php

    <script>
    var toSearchArray = ['some', 'array', 'with', 'values'];
    $.post('ajax.php', {
        variable: toSearchArray
    }, function(data) {
        alert(data); // here you will see the result of the ajax.php script
    });
    </script>
    
    0 讨论(0)
  • 2020-11-27 08:33

    Instead of declaring variable toSearchArray as array. consider it an javascript object. var toSearchArray = {}.

    0 讨论(0)
  • 2020-11-27 08:42

    Well I don't think thats the right way to do it when it comes to arrays, see you need to use JSON encode in javascript then JSON decode in php Refer to this question Pass Javascript Array -> PHP

    0 讨论(0)
提交回复
热议问题