本文介绍了如何使用PHP,AJAX和Amp的mysqli插入数据JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP

    $data = array(
        'success' => false,
        'errors' => array()
    );

    if(isset($_POST['inputs'])) {


            $inputs = $_POST['inputs'];
            foreach($inputs as $input) {

                    $name = $input['name'];
                    $value = $input['value'];

                    switch ($name) {
                            case 'email':
                                    if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
                                            $msg = "You must provide a valid e-mail address";
                                    }
                                    break;

                            default:
                                    $msg = "Sorry, we do not understand the data, please try again";
                                    break;
                    }

                    if(!empty($msg)) {
                            $data['errors'][] = array(
                                    'msg' => $msg,
                                    'field' => $name
                            );
                    }
            }

            if(empty($data['errors'])) {
                    $data['success'] = true;

                    $json = file_get_contents('php://input');
                    $obj = json_decode($json,true);

                    foreach ($obj as $item) {
                            $query = "INSERT INTO users (email)

                            VALUES

                            ('".$item['email']."')";

                            $objConnection->query($query) or die($objConnection->error);
                            $objConnection->close();
                    }

            }

    } else {
            $data['errors'][] = "Data missing";
    }
    header("Content-type: application/json; charset=UTF-8");
    echo json_encode((object)$data);

jQuery/AJAX

$(function() {
    $('.submit').click(function() {

            var formInputs = new Array();

            var formId = $(this).parent().attr('id');

            $('#' + formId + ' input').each(function() {

                    var obj = {
                        'value': $(this).val(),
                        'name': $(this).attr('name')
                    };

                    formInputs.push(obj);
            });

            $.ajax({
                    url: 'add.php',
                    type: 'POST',
                    dataType: 'json',
                    cache: false,
                    data: {
                            'inputs': formInputs
                    },
                    success: function(data) {
                            if(data.success) {
                                    $(".hej").html("worked");
                            } else {
                                    $.each(data.errors, function() {
                                            var list = "<p>"+this.msg+"</p>";
                                            $(".hej").html(list);
                                    });
                            }
                    }
            });

            return false;

    });
});

我一直在寻找答案,看看自己是否能解决,我确实找到了这个.除了给我一个错误说:为foreach()提供了无效的参数"外,它似乎正在工作.如果有人有更好的方法/解决方案来插入数据,那我将不知所措.

I have been searching for an answer, to see if I could figure it out myself, I did find this Insert data with JSON and mysql. It seems to be working, besides that it gives me an error that says: "Invalid argument supplied for foreach()". If anyone has a better method / solution to insert the data, I am open ears.

推荐答案

我知道了!

if(empty($data['errors'])) {

    $data['success'] = true;

    $inputs = $_POST['inputs'];
    $email = $inputs[0]['value']; // I select the first element in my array, then I select its value
    $password = $inputs[1]['value']; // I select the second element in my array, then I select its value

    $query = "INSERT INTO users (email, password)

    VALUES

    ('{$email}', '{$password}')";

}

这篇关于如何使用PHP,AJAX和Amp的mysqli插入数据JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:30