weixin_33675507 2019-12-07 11:18 采纳率: 0%
浏览 127

通过Ajax将JS数组传递给PHP

I have an array in javascript and I need to convert it into PHP array. So I have this code and I have tried other codes too but no one has worked. Here is the code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <div id="res" style="border: 1px solid black; width: 400px; height: 200px;"></div>
    <br>
    <input type="file" name="img[]" id="img" multiple>
    <input type="submit" name="submit" id="submit">
    <script>
        var array = new Array();
        $('#img').change(function() {
            var img = $('#img').val().split('\\').pop();
            array.push(img);
            document.getElementById('res').innerHTML = array;
        });
        $('#submit').click(function() {
            var send_data = JSON.stringify(array);
            $.ajax({
                type: "POST",
                enctype: "multipart/form-data",
                url: "test.php",
                data: {send_data:send_data},
                datatype: "JSON",
                success: function(data) {
                }
            });
        });
    </script>
</body>
</html>

And my test.php

<?php 
$data = $_POST['send_data'];
$decoded = json_decode($data, true);
var_dump($decoded);
?>

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33704591 2019-12-07 11:31
    关注

    HTML && Javascript sample code:

    function validateForm() {
    let formData = {
            'name': $('input[name=name]').val(),
            'email': $('input[name=email]').val(),
            'message': $('textarea[name=message]').val()
        };
        //Prevent page from refresh after sending message (ajax)
        $.ajax({
            url: "./assets/php/contact_me.php",
            type: "POST",
            data: formData,
            success: function (data, textStatus, jqXHR) {
                if (data.code)
                    $('#status').text(data.message);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $('#status').text("Error");
            }
        });
    }
    <a class="btn" onclick="validateForm()">Send Message</a>
    
    <!-- Status text -->
    <div id="status" class="text-danger pb-2"></div>

    PHP Code:

    <?php
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    
    header('Content-Type: application/json');
    if (strlen($name) < 3){
        print json_encode(array('message' => 'Name is too short', 'code' => 0));
        exit();
    }
    if ($email === ''){
        print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
        exit();
    } else {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
            exit();
        }
    }
    if (strlen($message) < 5){
        print json_encode(array('message' => 'Message is too short', 'code' => 0));
        exit();
    }
    print json_encode(array('message' => 'successfull!', 'code' => 1));
    exit();
    
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度