how to html form post - file uploading and reading json response from php server

后端 未结 5 1569
名媛妹妹
名媛妹妹 2020-12-18 06:42

i am trying file uploading to php my server.file and data uploading through multi part /form-data ,the file and data received on php server but in my php server return json

相关标签:
5条回答
  • 2020-12-18 06:56

    You should probably use an AJAX call. Here's a solution using jQuery:

    <script type="text/javascript">
    $(document).ready(function(){
        $("#browseButton").click(function(){
            var url = "";
            var formdata = $("#myForm").serialize();
            $.ajax({
                url: url,
                type: 'POST',
                data:  formdata,
                dataType: 'json',
                cache: false,
                contentType: false,
                processData: false,
                success: function(response){
                    if(response.status == "success"){
                        // Success
    
                    } else {
                        // Failure
    
                    }
                },
                error: function(response){
                    // Error
    
                }          
            });
        });
    });
    </script>
    

    In order to redirect the user, you can use: window.location.href = " ... your_url ...";

    Here's an explanation on how to use jQuery AJAX and multi-part data:

    Sending multipart/formdata with jQuery.ajax

    0 讨论(0)
  • 2020-12-18 07:02

    I think it should work for you. Using AJAX, as I do

         //Your php code
            $arrToJSON = array(
            "dataPHPtoJs"=>"yourData",
            "asYouWant"=>"<div class=\".class1\">soemting</div>"    
            );  
            return json_encode(array($arrToJSON));
    
    
    
    
        //Your javaScript code
        $(document).on("event", "#idElement", function(){
            //Data you want to send to php evaluate
             var dt={ 
                      ObjEvn:"btn_Login",
                      dataJsToPHP: $("#txt_EmailLogin").val()
                    };
    
            //Ajax      
             var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
                                    url: "yourServer.php",
                                    type: "POST",
                                    data: dt,
                                    dataType: "json"
                                });
    
            //Ajax Done catch JSON from PHP 
                request.done(function(dataset){
                    for (var index in dataset){ 
                         dataPHPtoJsJS=dataset[index].dataPHPtoJs;
                         asManyasYouWantJS=dataset[index].asYouWant;
                     }
    
                     //JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response
                     if(dataPHPtoJsJS){
                        $( "#idYourHtmlElement" ).removeClass( "class1" )
                        $( "#idYourHtmlElement" ).addClass( "class2" )
                     }
    
    
             }); 
    
            //Ajax Fail 
                request.fail(function(jqXHR, textStatus) {
                    alert("Request failed: " + textStatus);
                }); 
        }
    
    0 讨论(0)
  • 2020-12-18 07:15

    Your JSON response would be a kind of associative array in php. Encode your array data into JSON using "json_encode" and return values as you want .

       $arr = array('status' => $status, 'status2' => $status2, );
       echo json_encode($arr);
    

    NOTE: If you are using ajax to call php file then do not use any php echo/print in that file and not even HTML. ECHO only "json_encode();" Nothing else.

    0 讨论(0)
  • 2020-12-18 07:20

    try json_decode.

        $data = ({"code":0, "message":"success"});
        $array = json_decode($data, true);
    

    by passing 2nd parameter to true you will get response in array instead of object.

    the array will be then populated as follow:

        array (size=2)
        'code' => int 0
        'message' => string 'success' (length=7)
    
    0 讨论(0)
  • 2020-12-18 07:21

    To sum it up:

    1. Upload your data to server using AJAX with native JS (>=IE10) or jQuery
    2. Catch (xhr.responseText in native JS) and parse the response
    3. Redirect with window.location.href="success.php"
    0 讨论(0)
提交回复
热议问题