PHP帖子中未定义的索引 [英] Undefined index in PHP post

查看:87
本文介绍了PHP帖子中未定义的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取一种形式,并在jquery中进行变量检查,然后将其传递给ajax中的php文件,但我得到此通知

I take a form and do variable checking in jquery then pass it to a php file in ajax but I am getting this notice

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

Notice: Undefined index: your_name in C:\xampp\htdocs\process.php on line 3 something is wrong here Notice: Undefined index: your_email in C:\xampp\htdocs\process.php on line 7

这是我的jquery代码

Here is my jquery code here

        $(".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吗?

I am not sure why I am getting this notice Apparently my $_POST values are not set.
I am at my wits end with this one. Do you know why/when a $_POST is not set?

推荐答案

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

You are getting value of your_name first and then checking if it exists

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

这篇关于PHP帖子中未定义的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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