doubledee Posted May 2, 2011 Share Posted May 2, 2011 Why doesn't this code work... // Initialize variables. $form_value = ''; $form_value = $_POST['form_value']; I get this error... Notice: Undefined index: form_value Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/ Share on other sites More sharing options...
requinix Posted May 2, 2011 Share Posted May 2, 2011 a) There was no form submitted. b) It didn't use POST. c) There was no "form_value" field. Any or all of the above apply. Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209635 Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 The error indicates that either the post index doesn't exist or isn't set Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209637 Share on other sites More sharing options...
doubledee Posted May 2, 2011 Author Share Posted May 2, 2011 The error indicates that either the post index doesn't exist or isn't set What do you mean by "post index"?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209643 Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 If I had this $_post['name']; name would be considered the index of the post array. The index is whatever is inside of the bracket for post, get, request etc. Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209671 Share on other sites More sharing options...
doubledee Posted May 2, 2011 Author Share Posted May 2, 2011 If I had this $_post['name']; name would be considered the index of the post array. The index is whatever is inside of the bracket for post, get, request etc. So what is wrong then? My index is there... <?php // Initialize variables. $attendeeName = $form_value = ''; $form_value = $_POST['form_value']; echo '<br />CURRENT POST[form_value] = ' . $form_value; echo '<br />CURRENT SESSION[form_value] = ' . $_SESSION['form_value'] . '<br />'; <!-- HTML PAYMENT FORM --> <form id="payment" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- NEW --> <label>SET NEW FORM VALUE:</label> <input type="input" name="form_value" id="form_value" value="<?php echo $_SESSION['form_value'];?>" /> Debbie Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209688 Share on other sites More sharing options...
fugix Posted May 3, 2011 Share Posted May 3, 2011 That notice occurs when youvrefresh the page without submittiting the form. Doesn't affect anything. Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209695 Share on other sites More sharing options...
Pikachu2000 Posted May 3, 2011 Share Posted May 3, 2011 You aren't checking to see if $_POST['form_value'] exists before trying to access its value. Conventional syntax: if( isset($_POST['form_value']) ) { $form_value = $_POST['form_value']; } else { $form_value = ''; } Ternary syntax: $form_value = isset($_POST['form_value']) ? $_POST['form_value'] : ''; Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209701 Share on other sites More sharing options...
doubledee Posted May 3, 2011 Author Share Posted May 3, 2011 You aren't checking to see if $_POST['form_value'] exists before trying to access its value. Why do you have to do that? In most languages, $form_value would just equal null if $_POST['form_value'] - or any variable - did not exist, right?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209706 Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 No, in most languages $_POST["form_value"] would be whatever the folks who designed the language felt like making it. It could be a pie if they so wanted it. In PHP if you try to access an item in an array that does not exist then you get a warning. Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1209710 Share on other sites More sharing options...
ManiacDan Posted May 3, 2011 Share Posted May 3, 2011 Warnings are not, strictly speaking, errors. A warning tells you that your code is designed wrong. In this particular instance, PHP is letting you know that you're not checking to see if an array element exists before you try to use it. $form_value will indeed be null, because you've accessed an array element that doesn't exist. More correct code: $form_value = null; if ( isset( $_POST['form_value'] ) ) { $form_value = $_POST['form_value']; } //or more simply: $form_value = isset( $_POST['form_value'] ) ? $_POST['form_value'] : null; Also, this code is wrong: $attendeeName = $form_value = ''; Don't chain assignments like that, it's ambiguous and wastes memory. edit: unless of course you're doing this specifically to empty both variables, then it's fine. If you ever do something like $a = $b = "something"; that's a waste. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/235380-problem-assign-_post-value-to-variable/#findComment-1210051 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.