I am working on a shopping cart in PHP and I seem to be getting this error \"Notice: Undefined index:\" in all sorts of places. The error refers to the similar bit of coding
Try this:
$month = ( isset($_POST['month']) ) ? $_POST['month'] : '';
$op = ( isset($_POST['op']) ) ? $_POST['op'] : '';
I think there could be no form elements by name 'month' or 'op'. Can you verify if the HTML source (of the page which results in error when submitted) indeed has html elements by he above names
Are you putting the form processor in the same script as the form? If so, it is attempting to process before the post values are set (everything is executing).
Wrap all the processing code in a conditional that checks if the form has even been sent.
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST)){
//process form!
}else{
//show form, don't process yet! You can break out of php here and render your form
}
Scripts execute from the top down when programming procedurally. You need to make sure the program knows to ignore the processing logic if the form has not been sent. Likewise, after processing, you should redirect to a success page with something like
header('Location:http://www.yourdomainhere.com/formsuccess.php');
I would not get into the habit of supressing notices or errors.
Please don't take offense if I suggest that if you are having these problems and you are attempting to build a shopping cart, that you instead utilize a mature ecommerce solution like Magento or OsCommerce. A shopping cart is an interface that requires a high degree of security and if you are struggling with these kind of POST issues I can guarantee you will be fraught with headaches later. There are many great stable releases, some as simple as mere object models, that are available for download.
Make sure the tags correctly closed. And the closing tag will not include inside a loop. (if it contains in a looping structure).
Assure you have used method="post" in the form you are sending data from.
undefined index means the array key is not set , do a var_dump($_POST);die();
before the line that throws the error and see that you're trying to get an array key that does not exist.