Jump to content

[SOLVED] unidentified index?


samoht

Recommended Posts

The notice = "Notice: Undefined index: $DateVar in C:\wamp\www\BCDist\html\admin\store\season\seasonsedit.php on line 259"

258 $DateVar	= "DateStart";
259 $Date	= $_POST['$DateVar'] ? date('D. M d, Y', strtotime($_POST['$DateVar'])) : date('D. M d, Y', strtotime($DateStart));

 

(with added line #'s)

 

 

You are using single quotes around your variables on line 259. PHP will not parse variables within single quotes, pHP will treat them as-is (normal text). Remove the single quotes around $DateVar:

259 $Date	= $_POST[$DateVar] ? date('D. M d, Y', strtotime($_POST[$DateVar])) : date('D. M d, Y', strtotime($DateStart));

Ok - when I remove the single quotes it complains about DateStart. It still says the problem is on line 259???

 

Line 260 = include('../../include/datepick.php'); ?>

which is a js calendar and some html

</script>
<input type="text" name="<?php echo $DateVar; ?>" id="<?php echo $DateVar; ?>" value="<?php echo $Date; ?>" size="21" <?php if($TabIndex){echo "tabindex=\"$TabIndex\"";}?>>

 

you can see that I use $DateVar as the name and the id and $Date as the value -

 

So is this all messed UP?

 

I want to name my id and name "DateStart" and I want to be able to POST that value for my save function.

$DateStart is the Date from my database (if there is one)

here is the whole code snippet,

 

<?php
257 $TabIndex	= 6; 
258 $DateVar	= 'ccDateStart';
259 $Date	= $_POST[$DateVar] ? date('D. M d, Y', strtotime($_POST[$DateVar])) : date('D. M d, Y', strtotime($DateStart));
260 include('../../include/datepick.php'); ?>

 

This code creates this notice,

 

Notice: Undefined index: ccDateStart in C:\wamp\www\BCDist\html\admin\store\season\seasonsedit.php on line 259

 

as you can see I added the "cc" to the variable to see which one it didn't like  - so it must be the $DateVar inside $_POST on line 259 that throws the notice.

 

What to do?

$_POST["$DateStart"];

 

Any time you deal with an array, you have to surround the index with either single or double quotes. I believe that is the case. I don't think it will parse correctly with just a non-quoted $var inside the array index.

 

 

Nate

 

No, you can certainly use $_POST[$var]. That is much better than using $_POST["$var"] and making PHP parse the string.

Jusy noticed you are using an in-line if/else statement using the ternary operators.

 

I guess this code runs before and after the form is submitted. The post variable will only be populated when the form is submitted. So if you are using this code:

$_POST[$DateVar] ? date('D. M d, Y', strtotime($_POST[$DateVar])) : date('D. M d, Y', strtotime($DateStart));

PHP is expecting that the index (ccDateStart (stored in $DateVar)) exists within the post variable. If it is doesn't PHP will report a notice that its undefined (doesn't exist). What you'll want to do is use isset to see whether the variable/index exists first before using it:

$Date	= (isset($_POST[$DateVar]) && !empty($_POST[$DateVar])) ? date('D. M d, Y', strtotime($_POST[$DateVar])) : date('D. M d, Y', strtotime($DateStart));

Try the above code for line 259. The Undefined index notice message should now disappear.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.