AJAX不设置PHP $ _POST varibale,因此它返回为'未定义' [英] AJAX isn't setting a PHP $_POST varibale so it's returning as 'Undefined'

查看:101
本文介绍了AJAX不设置PHP $ _POST varibale,因此它返回为'未定义'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AJAX新手,并不擅长PHP。我试图简单地使用JQuery $。AJAX 函数向我的PHP页面发送一个字符串Hello。到目前为止,我已经成功地让AJAX将信息发送到页面并记录在控制台中,但数据并没有存储到POST变量中。



请保留记住,我不是懒惰的来到这个论坛并寻求帮助,但我没有其他选择,因为我已经搜索了大约2天,现在如何解决这个问题,并没有发现任何工作。 / p>

这里是我的HTML( order.html ) - 这不是我所有的HTML,但它只是你需要的) :

 < html> 
< body>
< form method =POST>
< button id =order-btntype =submitformaction =PHP / sendMail.php>订单< / button>
< / form>
<! - JavaScript / JQuery链接 - >
< script src =https://code.jquery.com/jquery-3.2.1.min.js>< / script>
< script type =text / javascriptsrc =JS / order.js>< / script>
< / body>
< / html>

这是我的JavaScript( order.js - 再次,我只提供必要的代码)

  $(#order-btn)。click(function() {
var txt =Hello!;
$ .ajax({
url:PHP / sendMail.php,
type:POST,
data:{data:txt},
dataType:html,
asyc:true,
成功:函数(数据){
console.log(data);
$,
error:function(xhr,ajaxOptions,thrownError){
alert(ERROR:+ xhr.responseText + - + thrownError);
}
} );
});

这里是我的PHP(sendMail.PHP - 我只提供必要的代码)

 <?php 
if(isset($ _ POST ['data'])){
$ data = $ _POST ['数据'];
echo $ data;
} else {
echo无法获取数据。;
}

为了澄清,在我的实际代码中,网址是我的完整网址网站页面。



如果您希望看到该网站更好地了解我如何以及为什么需要使用此功能,请在评论中告诉我们。

p>

UPDATE&解决方案:

从我收到的帮助中,我明白了AJAX只会更新当前页面上的信息(例如,如果您有AJAX功能在index.html,那么你只能在该页面上运行AJAX,并且不能跨页面传输信息)

为了解决我的问题,我停止将用户发送到sendMail。 php页面,而改为在$ .ajax成功方法中我当前所在页面(order.html)的HTML内容。



以下是更新后的JavaScript代码:

  $(#order-btn)。click(function(){
var txt =Hello! ;
$ .ajax({
url:order.html,
type:'POST',
data:{data:txt},
dataType: 'html',
success:function(data){
if(parseInt(data)!= 0){
$(body)。html(data);
}
},
error:function(xhr,ajaxOptions,thrownError){
alert(ERROR:+ xhr.responseText + - + thrownError);
}
});
});

我要感谢所有帮助过我的人:)

解决方案

  $。ajax({//创建一个ajax请求到load_page.php 
类型:POST,
url:load-page.php,
data:{page:url},//将页码作为参数
dataType:html,// expect html to be返回
成功:函数(msg){

if(parseInt(msg)!= 0)//如果没有错误
{'b pageContent' ).html(msg); //将返回的html加载到
pageContet
$('#loading').css('visibility','hidden'); //并隐藏旋转的
gif
}
});

像上面的例子一样,load-page.php被调用,所以返回的数据将在pageContent中显示是一些div等的id,这个div不在load-page.php上,这个div在发送这个ajax请求的页面上。

可能是

将使sence
的参考链接:参考链接
a>!


I'm new to AJAX and not so good at PHP. I'm trying to simply send a string saying "Hello" to my PHP page using the JQuery $.AJAX function. So far I have successfully got AJAX to send the information to the page and log it in the console but the data doesn't get stored into the POST variable.

Please keep in mind I'm not being lazy by coming to this forum and asking for help but I have no other choice because I've been searching for about 2 days now on how to fix this problem and haven't found anything that's worked.

Here's my HTML (order.html) - This isn't all my HTML but it's all you will need):

<html>
    <body>
        <form method="POST">
            <button id="order-btn" type="submit" formaction="PHP/sendMail.php">Order</button>
        </form>
        <!-- JavaScript/JQuery links -->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script type="text/javascript" src="JS/order.js"></script>
    </body>
</html>

Here's my JavaScript (order.js - Once again, I'm only providing necessary code)

$("#order-btn").click(function() {
    var txt = "Hello!";
    $.ajax({
        url: "PHP/sendMail.php",
        type: "POST",
        data: {data: txt},
        dataType: "html",
        asyc: true,
        success: function(data){
            console.log(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("ERROR:" + xhr.responseText+" - "+thrownError);
        }
    });
});

Here's my PHP(sendMail.PHP - I'm only providing necessary code)

<?php
if(isset($_POST['data'])) {  
    $data = $_POST['data'];
    echo $data;
} else {
    echo "Failed to grab data.";    
}

Just to clarify, in my actual code the URL is the full URL of my website page.

Let me know in the comments if you would like to see the site to get a better understanding of how and why I need this feature to work.

UPDATE & SOLUTION:

From the help I received I now understand that AJAX will only update information on the current page (So, for example, if you have an AJAX function on index.html then you can only run AJAX on that page and can't transfer information across pages)

To solve my problem I stopped sending users to the sendMail.php page and instead changed the HTML content of the page I was currently on (order.html) in the $.ajax success method.

Here's the updated JavaScript code:

$("#order-btn").click(function() {
                var txt = "Hello!";
                $.ajax({
            url: "order.html",
            type: 'POST',
                data: {data: txt},
            dataType: 'html',
            success: function(data){
                        if(parseInt(data)!=0) {
                            $("body").html(data);
                        }
            },
            error: function (xhr, ajaxOptions, thrownError) {
            alert("ERROR:" + xhr.responseText+" - "+thrownError);
            }
        });
    });

I'd like to thank everyone that helped :)

解决方案

$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "load-page.php",
data: {page:url}, //with the page number as a parameter
dataType: "html", //expect html to be returned
success: function(msg){

if(parseInt(msg)!=0) //if no errors
{
    $(‘#pageContent’).html(msg); //load the returned html into 
    pageContet
    $(‘#loading’).css(‘visibility’,’hidden’);//and hide the rotating    
    gif
}
});

Like in above example load-page.php is called so data returned is going to dispaly in pageContent which is the id of some div etc and this div is not on load-page.php this div is on the page from where this ajax request is sent.

may be it will make sence reference link: Reference Link!

这篇关于AJAX不设置PHP $ _POST varibale,因此它返回为'未定义'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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