samoht Posted September 12, 2007 Share Posted September 12, 2007 hello, I am getting a notice of an Unidentified index on a line that looks like: <?php $DateVar = "DateStart"; it complains about DateStart How can this be??? it is just a string variable?? Any ideas Quote Link to comment Share on other sites More sharing options...
SammyGunnz Posted September 12, 2007 Share Posted September 12, 2007 Can you paste the exact error? Thanks. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 12, 2007 Share Posted September 12, 2007 try putting ob_start (); before all of your codel Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 12, 2007 Share Posted September 12, 2007 undefined indexes are to do with arrays, not when assigning a variable a string. You sure you got the right line? Quote Link to comment Share on other sites More sharing options...
samoht Posted September 13, 2007 Author Share Posted September 13, 2007 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) Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 13, 2007 Share Posted September 13, 2007 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)); Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted September 13, 2007 Share Posted September 13, 2007 $DateVar = "DateStart"; Is it supposed to be $DateStart ? As it is now, $DateVar will become the word "DateStart". Quote Link to comment Share on other sites More sharing options...
samoht Posted September 13, 2007 Author Share Posted September 13, 2007 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) Quote Link to comment Share on other sites More sharing options...
samoht Posted September 13, 2007 Author Share Posted September 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
chronister Posted September 13, 2007 Share Posted September 13, 2007 $_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 Quote Link to comment Share on other sites More sharing options...
samoht Posted September 13, 2007 Author Share Posted September 13, 2007 using double quotes does not help ??? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 13, 2007 Share Posted September 13, 2007 why do you have PHP in HTML? your overdoing it remove the echo $datevar in the html and just do the date. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 $_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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 It looks like the problem is those variables are not being posted. Where is the HTML form which posts these? Did you do print_r($_POST) at the top of the page to see what IS posted? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 14, 2007 Share Posted September 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
samoht Posted September 14, 2007 Author Share Posted September 14, 2007 Thanks that did it! You where correct "this code runs before and after the form is submitted." so this is what I needed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.