Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have form with inputs in html.
Then I added this javascript (jquery) stroke to read and collect all value or data from form.

var formData = $("#form").serialize();

When I

console.log(formData);

Output will show:

calc-ownership=ooo&calc-activity=restaurant&calc-tax=usn&calc-tax-2=charge&calc-bank=partners&calc-who-payments=client&operations_count=0&calc-nomenclature=slim&documents_count=0&calc-who-docs=client&staff_count=0&calc-more%5B%5D=patent&calc-more%5B%5D=alcohol&period=&price=&price_sber=&rate-name=&email-to=

Then I found function in jquery called post $.post(path, formData, success, "json"); Request look like: do.php?bank=partners

As you see it makes post request to my do.php.
Now how I can read this query and work with this data?

I found analog to $.post in jquery. It is $.ajax Full code:

$.ajax({ url: path, method: "POST", data: {formData: formData} });

It works well.
But I want to work with $.post

I am looking at my url at the moment. And it looks : https://stackoverflow.com?ask=32321

I need something similar to read my query from javascript this url with php

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

Ok I will explain with example,

If you want to get url parameter values to you have to use,

<script>
let my_variable='<?php echo $_GET['url_param_name'];?>';
</script>

above for more help and understanding. Now you want to send form data to php for processing as I got your answer.

This is sample form.

<form id="my_form" name"my_form" method="POST" onsubmit="return send();">
  First name:<br>
  <input type="text" name="first_name" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="last_name" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form> 

To post above form I will use javascript function,

<script>
function send() {

    $.ajax
    ({
        type: 'POST',
        url: './path/your_php_file_where_form_data_processed.php',
        data:$('#my_form').serialize(),
        success: function () {
           // do what you need to do on succeess
        },
        error: function (x, e) {
            // for error handling
            if (x.status == 0) {
                console.log('You are offline!! -  Please Check Your Network.');
            } else if (x.status == 404) {
                console.log('Requested URL not found.');
            } else if (x.status == 500) {
                console.log('Internal Server Error.');
            } else if (e == 'parsererror') {
                console.log('Error. - Parsing JSON Request failed.');
            } else if (e == 'timeout') {
                console.log('Request Time out.');
            } else {
                console.log('Unknown Error. - ' + x.responseText);
            }
        }
    });
    return false;
}
</<script>

Now you need to check carefully your form element names. In php file. Let's look it.

<?php
//include_once './../../classes/Database.php'; import if you have database configurations
//session_start(); make sure to use sessions if your site using sessions

if(isset($_POST))
{
    var_dump($_POST); //this will echo your form inputed data.
    //if you want use one by one posted data
    echo $_POST['first_name'];
    echo $_POST['last_name'];
}
else
{
    echo 'Data not comes here';
}
?>

Thought this might help your task.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...