M
M
Mikhail Smirnov2020-08-07 13:29:18
PHP
Mikhail Smirnov, 2020-08-07 13:29:18

Need help with PHP array manipulation?

Help with PHP
sending an array

$.ajax({
            url: 'scripts/order.php',
            data: orderData,
            type: 'POST',
            cache: false,
            dataType: 'json',
            error: _orderError,
            success: function(responce) {
                if (responce.code === 'success') {
                    _orderSuccess(responce);
                } else {
                    _orderError(responce);
                }
            },
            complete: _orderComplete
        });

the array looks like
surname=Ivanov&name=Ivan&[email protected]&phone=1001010&comment=&cart={"1-1":{"goodname":"Product","goodprice":"350","goodimg":"image/ tovar2.jpg","count":1}}

Before sending the letter, I want to use the getData function to extract the data into an associative array, so that it would be more convenient to work with it. But I can't win on this one
function getParam($param) {
    return (isset($_POST[$param]));
}
 
function getData() {
    return array(
        'surname' => getParam('surname'),
        'name' => getParam('name'),
        'email' => getParam('email'),
        'phone' => getParam('phone'),
        'coment' => getParam('coment'),
        'cart' => isset($_POST['cart']) ? ($_POST['cart']) : '[]'
    );
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2020-08-07
@Mehannik

For example

function getData() {
    return array(
        'surname' => filter_input(INPUT_POST, 'surname', FILTER_SANITIZE_STRING),
        'name' => filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING),
        'email' => filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL),
        'phone' => filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING),
        'comment' => filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING),
        'cart' => json_decode(filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING) ?: '[]'),
    );
}

It's even better to describe the form validation separately, and separately to get the parameters. But this does not fit into a couple of lines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question