0

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

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

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"); 
        },

and here is my 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/>"; 
    }

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?

user1154295
  • 85
  • 1
  • 2
  • 8
  • 7
    `var_dump($_POST);` --- always use `var_dump()` to see what actually is in variable. *Knowledge* - that is what differs programmers from fortune-tellers – zerkms Apr 24 '12 at 07:36
  • thank you @zerkms the values are NULL :-( i am so confused as to why – user1154295 Apr 24 '12 at 11:53

3 Answers3

2

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'])){

Do something like the following

if (isset($_POST['your_name'])) {
   $your_name = $_POST['your_name'];
   // do whatever here
}
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
  • thank you @Hasan Khan my post variables do not exist and when i do a var_dump it all comes up NULL. I am at my wits end with this. Do you have an idea why the $_POST index does not exist? – user1154295 Apr 24 '12 at 11:50
  • It doesn't exist because it may not be submitted? Check the form that posts these variables. Make sure the name is same. – Muhammad Hasan Khan Apr 24 '12 at 12:10
1

$_POST['your_name'] cannot be assigned to the variable $your_name when the "your_name" isn't posted cause it doesn't exist .. you have to check if it exists first and then assign it to the variable .. the code should be like the following :

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

not seen any issue in your js code but your php code have lil flaw

 <?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"]; 
}

Note: PHP Error handling should be properly done or you can disable the php warnings

ieatbytes
  • 516
  • 1
  • 6
  • 13