A
A
AlexNest2020-05-15 19:00:01
PHP
AlexNest, 2020-05-15 19:00:01

How to pass data from jquery to php?

It is necessary that when selecting an item in the select, dynamically receive its value and work with it using php.
As I understand it, all this is implemented through ajax.
I found an example that, in theory, sends data to 'post.php' and outputs it to the console in parallel. The question is how to get data from post.php?

<head>
    <title>titile</title>
    <meta charset="utf-8">
    <style>

    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        $(function(){

            $('#company-list').on('change', function(event) {
                console.log($(this).val());
                $.ajax({
                    url: 'post.php',
                    type: 'POST',
                    dataType: 'html',
                    data: {idCompany: $(this).val()},
                })
                .done(function() {
                    console.log("success");
                })
                .fail(function() {
                    console.log("error");
                });

            });
        });
    </script>
</head>
<body>

    <select id="company-list">
      <option value="1">Компания 1</option>
      <option value="2">Компания 2</option>
      <option value="3">Компания 3</option>
      <option value="4">Компания 4</option>
    </select>

</body>
</html>

The example says that post.php can be dispensed with . But nothing happens, what's wrong? <?php echo $_POST['idCompany']; ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VicTHOR, 2020-05-15
@AlexNest

The question is how to get data from post.php?
What will be displayed in this file through echoor another output statement, as well as outside the php brackets, will come as a response.
To print this response to the console, redo the done method
.done(function(data) {
                    console.log(data);
                })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question