PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am very new to php and stuck at very basic problem.my goal user will input value through drop down list, and according to that selection nos of images will be display on another div.here is code
problem is when for the first time the page loaded its shows
Notice: Undefined index: submit in C:\wamp\www\test\practice.php on line
whats wrong?Why the "if" is not working?
You are getting the error message because, the first time you load the page, there are no POST values and therefore $_POST['submit'] does not exist. if($_POST['submit'] tests whether $_POST['submit'] has a positive or a negative value; it can't cope with a situation in which $_POST['submit'] does not exist. That is why it is normal to test whether a $_POST variable is set as well as its value
if (isset($_POST['something'] && $_POST['something'] == some value)
.
In your case, the simplest thing to do would be to use the function empty(). empty() returns true if a variable has a value of false or zero or null, and also if the variable does not exist. So:
Thank you for reply.Yes with "isset()" its working fine.Here comes the 2nd problem
when user input - other than the 1st value from the drop down list, it come back with the 1st value(which i believe is default) with the reloaded page.How to show (or select that option )which user have chooses on the reloaded page?
mean if user choose 20, on the reloaded page it shows 20 in the drop down box.
For each of the options, if the submitted value matches the option value, add a "selected" attribute to the option tag. The resulting HTML will look something like this:
I rarely write an option tag without the value attribute for my own code, but if there is no value attribute, the value of the option defaults to the option label (the part between the tags).