如何从PHP中的JavaScript读取发布网址请求 [英] How to read post url request from javascript in PHP

查看:49
本文介绍了如何从PHP中的JavaScript读取发布网址请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用html输入的表单.
然后,我添加了这个javascript(jquery)笔画,以读取和收集表单中的所有值或数据.

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();

当我

console.log(formData);

输出将显示:

calc-ownership = ooo& calc-activity = restaurant& calc-tax = usn& calc-tax-2 =费用& calc-bank =合作伙伴& calc-who-pays =客户& operations_count = 0& calc-命名= 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 =

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=

然后我在jquery中找到了称为post的函数 $.post(path, formData, success, "json"); 要求看起来像: do.php?bank = partners

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

如您所见,它向我的do.php发出了发布请求.
现在,我如何读取该查询并使用该数据?

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

我在jquery中发现了与$.post类似的东西.是$.ajax 完整代码:

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

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

效果很好.
但是我想和$.post

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

此刻我正在查看自己的网址.它看起来是:https://stackoverflow.com?ask=32321

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

我需要类似的内容才能使用php从javascript这个网址读取我的查询

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

推荐答案

好的,我会举例说明,

如果要获取要使用的url参数值,

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

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

以上以获得更多帮助和理解.现在您想将表单数据发送到php进行处理,因为我得到了您的答案.

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

这是示例表格.

<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> 

要发布上述表格,我将使用javascript函数,

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>

现在,您需要仔细检查表单元素名称.在php文件中.我们来看一下.

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.

这篇关于如何从PHP中的JavaScript读取发布网址请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆