【问题标题】:Undefined index in PHP postPHP帖子中未定义的索引
【发布时间】:2012-05-04 20:06:32
【问题描述】:

我采用一个表单并在 jquery 中进行变量检查,然后将其传递给 ajax 中的一个 php 文件,但我收到了这个通知

注意:未定义索引:第 3 行 C:\xampp\htdocs\process.php 中的 your_name 这里不对劲 注意:未定义索引:第 7 行 C:\xampp\htdocs\process.php 中的 your_email

这里是我的 jquery 代码

        $(".button").click(function(){
    $('.error').hide(); 
    var your_email=$("input#your_email").val(); 
    if(your_email ==""){
        $("label#youremail_error").show();  
        $("input#your_email").focus(); 
        return false; 
    }
    var your_name=$("input#your_name").val(); 
    if(your_name==""){
        $("label#yourname_error").show();
        $("input#your_name").focus(); 
        return false; 
    }
    var friends_email=$("input#friends_email").val(); 
    if(friends_email==""){
        $("label#friendsemail_error").show(); 
        $("input#friends_email").focus(); 
        return false; 
        }
    var friends_name=$("input#friends_name").val(); 
    if(friends_email==""){
        $("label#friendsname_error").show(); 
        $("input#friends_name").focus(); 
        return false;
        }
        var dataString = 'your_email=' + your_email + '&friends_email=' + friends_email + '&your_name=' + your_name + '&friends_name=' + friends_name; 
        //alert(dataString); 
        $.ajax({
        type: "POST", 
        url:"process.php",
        data: dataString, 
        success: function(ret) {
            alert(ret); 
            //alert("thank you for signing up"); 
        },

这是我的 PHP

   <?php
include 'inc/class.phpmailer.php';
if(isset($_POST['your_name'])){
    $your_name=$_POST['your_name'];
    }
else{
    echo "something is wrong with your name having:";
    var_dump($_POST['your_name']);
    echo "<br/>"; 
    }
if(isset($_POST['your_email'])){
    $your_email=$_POST['your_email']; 
}
else{
    echo "something is wrong with your email having:";
    var_dump($_POST['your_email']); 
    echo "<br/>"; 
    }
if(isset($_POST['friends_name'])){
    $friends_name=$_POST['friends_name']; 
    }
else{
    echo "something is wrong with friends name having:"; 
    var_dump($_POST['friends_name']);
    echo "<br/>"; 
    }

我不知道为什么我会收到此通知 显然我的 $_POST 值没有设置。
我对此束手无策。你知道为什么/什么时候没有设置 $_POST 吗?

【问题讨论】:

  • var_dump($_POST); --- 总是使用var_dump() 来查看变量中的实际内容。 知识 - 这就是程序员与算命者的区别
  • 谢谢@zerkms 这些值是NULL :-(我很困惑为什么

标签: php forms jquery phpmailer undefined-index


【解决方案1】:

您首先获取 your_name 的值,然后检查它是否存在

$your_name=$_POST["your_name"];  <--- this line should be inside an if isset
if(!isset($_POST['your_name'])){

执行以下操作

if (isset($_POST['your_name'])) {
   $your_name = $_POST['your_name'];
   // do whatever here
}

【讨论】:

  • 谢谢@Hasan Khan,我的帖子变量不存在,当我执行 var_dump 时,它全部为 NULL。我对此束手无策。你知道为什么 $_POST 索引不存在吗?
  • 不存在是因为可能没有提交吗?检查发布这些变量的表单。确保名称相同。
【解决方案2】:

在您的 js 代码中没有发现任何问题,但您的 php 代码存在 lil 缺陷

 <?php
include 'inc/class.phpmailer.php';

//it can be like that or
if(!isset($_POST['your_name'])){
    echo "something is wrong here"; 
}
else{
    $your_name=$_POST["your_name"]; 
}

注意:PHP 错误处理应该正确完成,或者您可以禁用 php 警告

【讨论】:

    【解决方案3】:

    $_POST['your_name'] 当“your_name”未发布时无法分配给变量 $your_name,因为它不存在..您必须先检查它是否存在,然后将其分配给变量.. 代码应如下所示:

    if (isset($_POST['your_name'])) {
       $your_name = $_POST['your_name'];
    }
    else { 
       echo "something is wrong here";
    }
    

    【讨论】:

    • 谢谢@Ahmed Muhammad :-) 你明白它为什么不存在吗?
    • 当您提交表单时.. 如果“your_name”未提交,则不会将其添加到 $_POST 数组中
    猜你喜欢
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2014-12-19
    相关资源
    最近更新 更多