1

I wrote a simple php script that basically echos the values put into a form on the page, later, I will have this write to a DB but I was working on troubleshooting it before I did that since I keep getting this warning.

Does anyone know why I am getting it? If I just fill in the fields and submit the form, the script works fine and the warning disappears.

PHP Function:

function quickEntry()
{
    $subTitle = $_POST['subTitle'];
    $subDetails = $_POST['details']; //This is line 13 in my code
    echo "$subTitle";
    echo "<br>$subDetails";
}

HTML / PHP Code:

<form method="post" action="">
    <hr>
    <h1>Quick Entry:<p></h1>
    Subject Title:<br> <input type="text" name="subTitle"><br><br>
    Subject Details: <p><textarea name="details" placeholder="Enter Details here..."></textarea><p><br>
    <input type="submit" name="QuickEntrySubmit" value="Submit Quick Entry" /><br>
</form>

<?php
if (isset($_POST['QuickEntrySubmit']))
{
    quickEntry();
}
?>

I know that I could disable warnings and I wouldn't see this, but I really just want to know why php is throwing the warning so I can fix the syntax appropriately and keep my code clean going forward. Full warning is:

Notice: Undefined index: details in C:\xampp\htdocs\test1.php on line 13

Thanks!

2
  • Your code is fine. There shouldn’t be any warning like that. I’ve tested it on my system and it works as expected. Commented Jul 21, 2014 at 3:10
  • if a index is not set in array which you try to access it will give you warning. your are accessing $_POST['details'] if it is not set you will get warning you can use isset with a ternary operator like this $subDetails = isset($_POST['details']) ? $_POST['details'] : ""; Commented Jul 21, 2014 at 3:17

2 Answers 2

1

The reason why you are getting that error is because you are not checking whether the 'subTitle' and 'details' inputs have values in them. Your code should work well like this:

function quickEntry(){
  $subTitle = isset($_POST['subTitle'])? $_POST['subTitle']: null;
  $subDetails = isset($_POST['details'])? $_POST['details']: null ; //This is line 13 in my code
  if(!is_null($subTitle) && !is_null($subDetails)){
    echo "$subTitle";
    echo "<br>$subDetails";
  } else{
  //blah blah blah
  }
Sign up to request clarification or add additional context in comments.

1 Comment

if(!is_null($var)) is a longwinded way of saying if($var)
1

You get the warnings because the $_POST variables with the indexes that you're checking for ($_POST['subTitle'] & $_POST['details']) aren't set, so the variables are empty as you reference something that isn't there.

You should do a check to ensure they are set first before trying to assign them to a variable:

$subTitle = (isset($_POST['subTitle']) && !empty($_POST['subTitle'])) ? $_POST['subTitle'] : null;
$subDetails = (isset($_POST['details']) && !empty($_POST['details'])) ? $_POST['details'] :  null;

The above code will check to ensure the post index isset() and isn't empty() before assigning it to the variables.


NOTE

The variables are set when you submit the form because you are actually posting the data to the script.

You see the input attribute name? When you submit the form, they are the relevant $_POST indexes. Just remember to ensure that the values aren't empty if you intend to print them to the markup.

Comments

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.