当从jQuery的发送未定义的索引错误变量到PHP [英] Undefined index error when sending variable from jquery to php

查看:171
本文介绍了当从jQuery的发送未定义的索引错误变量到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题的改编:<一href="http://stackoverflow.com/questions/29881307/passing-jquery-variable-to-php-within-a-modal">Passing jQuery的变量中的一个模式的PHP

我把它设置,它应该是工作,但我得到了不确定的指标差。

内的while循环:

 打印'&LT;李&GT;';
        打印'&LT;一类=BTN BTN默认数据切换=莫代尔数据-ID ='$行['身份证']'。数据目标=BS-例如模态-LG。&GT;&LT; IMG SRC ='。$行[形象]。/&gt的帮助;&LT; / A&GT;&LT; BR&GT;';
        打印'&LT;一类=BTN BTN默认数据切换=莫代尔数据-ID ='$行['身份证']'。数据目标=BS-例如模态-LG。&GT;&其中; H4&GT;'[名']'&所述$行。'; / H4&GT;&所述; / a取代;';
打印'&LT; /李&GT;';
 

我所有的jQuery的,包括AJAX code的:

  $(文件)。就绪(函数(){
            $(机构)的CSS(显示,无);
            $(机构)淡入(1000)。
            $(a.transition)。点击(函数(事件){
                。事件preventDefault();
                linkLocation = this.href;
                $(机构)淡出(1000,redirectPage)。
            });
            功能redirectPage(){
                了window.location = linkLocation;
            }
        });
$(文件)。在(点击,.btn功能(E){
            即preventDefault();
            警报(ID);
            VAR ID = $(本)的.d​​ata('身份证');
            警报(ID);
            $(h4.modal标题)文本(ID​​)。

            //$($(this).attr('href')).modal('show');
            .post的$('food.php',{ID:ID},功能(数据){
                警报(数据);
                警报(ID)//&LT;  - 此警报也工作
            });
        });
 

PHP的:

 &LT; PHP
        的$ id = $ _ POST [ID];
        打印'&LT; H4&GT;'。$ ID'&LT; / H4&GT;。

    ?&GT;
 

我一直在的$ id = $ _ POST得到一个指数未定义错误[ID];

我似乎无法找出原因,我已经适应了我的code做了这么多不同的方式,但他们没有工作。

第一个警报显示出来,但第二次没有。该文中得到放在&LT之内; DIV&GT; ,但它不会发送到变量。所有这一切都在一个文件中。 我有一种感觉我的ajax code是失去了一些东西。

一个无功转储显示:

 阵列(大小= 0)
   空
 

解决方案

您已经假设数据公布。 尝试,如果使用isset

 如果(使用isset($ _ POST ['身份证']))
{
     的$ id = $ _ POST [ID];
  回声$编号;
    }
 

Adapted from this question: Passing Jquery variable to php within a modal

I have it set up, and it should be working, but I am getting the undefined index error.

Inside a while loop:

print '<li>';
        print '<a class="btn btn-default" data-toggle="modal" data-id='.$row['id'].' data-target=".bs-example-modal-lg"><img src="'.$row["image"].'" /></a><br>';
        print '<a class="btn btn-default" data-toggle="modal" data-id='.$row['id'].' data-target=".bs-example-modal-lg"><h4>'.$row['name'].'</h4></a>';
print '</li>';

All of my jquery, including the ajax code:

 $(document).ready(function() {
            $("body").css("display", "none");
            $("body").fadeIn(1000);
            $("a.transition").click(function(event){
                event.preventDefault();
                linkLocation = this.href;
                $("body").fadeOut(1000, redirectPage);      
            });
            function redirectPage() {
                window.location = linkLocation;
            }
        }); 
$(document).on("click", ".btn", function (e) {
            e.preventDefault();
            alert (id);
            var id = $(this).data('id');
            alert (id);
            $("h4.modal-title").text(id);

            //$($(this).attr('href')).modal('show');    
            $.post('food.php',{id:id},function(data){
                alert(data);
                alert (id) // <--THIS ALERT WORKS TOO
            });
        });

The php:

<?php
        $id = $_POST["id"];
        print '<h4>'.$id.'</h4>'

    ?>

I keep getting an index undefined error on $id = $_POST["id"];.

I cant seem to figure out why, I have adapted my code to so many different ways of doing it, but none of them work.

The first alert shows up, but the second does not. The text does get placed within the <div>, however it does not get sent to the variable. All of this is in one file. I have a feeling my ajax code is missing something.

A Var dump shows:

array (size=0)
   empty

解决方案

you are already assuming data is posted. try if isset

if(isset($_POST['id']))
{
     $id = $_POST["id"];
  echo $id;
    }

这篇关于当从jQuery的发送未定义的索引错误变量到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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