0

Here is my ajax code:

function send()
{
    var xmlhttp;
     if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       }
     else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }

    xmlhttp.onreadystatechange=function()
       {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
            alert(xmlhttp.responseText);
         }
       }

    xmlhttp.open("POST","test.php",true);
    xmlhttp.send('subject=' + document.getElementById("subject").value);
}

here is my php code:

<?php
$subject = $_POST['subject'];
echo $subject;
?>

It says error on line 2 in the php code, undefined index. I don't know what else to do..any help would be apriciated, thanks.

1
  • an obvious comment, but does your form element definitely have the id subject? I would also try it with GET instead, worth a try! Commented Jan 19, 2012 at 16:16

2 Answers 2

1

This means that there is no 'subject' value in the $_POST array. This means that when the request to the page was made, there was not a post variable called subject with a value. One simple way to check for this is to use:

if(isset($_POST['subject'])){
    $subject = $_POST['subject'];
}else{
    $subject = "default";
}

This was makes sure that subject has a value and will not cause any page errors. Make sure you are definitely sending a POST variable called subject (You can do this in Chrome Developer tools or Firebug in the Network requests panel).

Sign up to request clarification or add additional context in comments.

9 Comments

Got it, but whats the problem? why it isn't getting the value of subject?
As I said, if this is an AJAX application, then check your network panel in dev tools or firebug and look at the request data, in the header it should show the request variables and their type. Check that there is a POST variable being sent, and that it has the value you are expecting it to.
Firebug in post: Source subject=dfgasdasd Firebug in response: <b>Notice</b>: Undefined index: subject in <b>C:\wamp\www\msgs\test.php</b> on line <b>2</b><br /> ???
Try it with GET instead, see if you have the same problem.
Try it with $_POST again and use: print_r ( $_POST ); You should be able to see the variables that are in the post array.
|
0

Check your form input named subject. You might have misspelled it.

2 Comments

Nope..I get the value of the subject, tested it.
If 'subject' was in your $_POST, you wouldn't have the notice about the undefined index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.