oraya Posted July 5, 2012 Share Posted July 5, 2012 I have a simple captcha just to lessen the spam made up of three rand() numbers. num1 and num2 are added together then num3 is subtracted from the add to give the total. Now this is my question. If someone gets the sum wrong and the form is re-displayed with the sticky fields, I can remove the contents and change the values of the inputs and re-sumbit with the correct sum captcha submitted, but if someone takes the check out of the checkbox or puts one in then it throws up the error Notice: Undefined index: <input type="checkbox" name="call" value="Yes" <?php if($_POST['call'] == 'Yes') echo "checked" ?> /> Do you wish to receive a call from us? Chances are 99% of the users wont change any or the inputs. But I'd like to cover myself for that 1% that may. Of those they will fall into two categories those that selected the wrong option on first submit and notice it during the catchpa re-display of the form, and those that have changed their minds on a certain option. Either way I want to make sure that I don't get the Undefined index error if they try to re-submit the form with the check taken out or one put in. So any help would be brilliant. I've tried so many things over the last couple of hours and can't find a solution. I like to try and figure it out myself as it helps me learn, but after a few hours now I'm still no further forward. So any help would be very gratefully received. If only two stop me from pulling my hair out, I don't think I'd look good bald lol.. Many thanks in advance for any help Oraya Quote Link to comment https://forums.phpfreaks.com/topic/265228-how-can-i-re-write-this-checkbox-sticky/ Share on other sites More sharing options...
jcbones Posted July 5, 2012 Share Posted July 5, 2012 <input type="checkbox" name="call" value="Yes" <?php echo (isset($_POST['call'])) ? 'checked="checked"' : NULL; ?> /> Do you wish to receive a call from us? checkboxes will only be set IF it is checked, so you can use isset. If it is set, then it should be checked. Quote Link to comment https://forums.phpfreaks.com/topic/265228-how-can-i-re-write-this-checkbox-sticky/#findComment-1359245 Share on other sites More sharing options...
oraya Posted July 5, 2012 Author Share Posted July 5, 2012 And how would that pan out if when the form was re-displayed and the user removed the check and re-submitted, would that mean I'd still get the error Jc? Quote Link to comment https://forums.phpfreaks.com/topic/265228-how-can-i-re-write-this-checkbox-sticky/#findComment-1359248 Share on other sites More sharing options...
oraya Posted July 5, 2012 Author Share Posted July 5, 2012 Ok ignore that... lol I've just re-read you comment. No it wont because it's not set so it wont throw and error. Forgive me, long hours = tiredness and also new to php. Thank you so much for you help that's brilliant. One more question if you wouldn't mind what does the question mark do? Oraya Quote Link to comment https://forums.phpfreaks.com/topic/265228-how-can-i-re-write-this-checkbox-sticky/#findComment-1359252 Share on other sites More sharing options...
jcbones Posted July 5, 2012 Share Posted July 5, 2012 ternary operator. We have all been there with the long hours. So, I understand completely. Quote Link to comment https://forums.phpfreaks.com/topic/265228-how-can-i-re-write-this-checkbox-sticky/#findComment-1359253 Share on other sites More sharing options...
oraya Posted July 5, 2012 Author Share Posted July 5, 2012 Wonderful, have booked marked the link for later reading. You have now turned the long day into a happy ending, so thank you for that. I do like to try and figure things out for myself, as I have found that if it takes me a while to find a solution it tends to stick more in my memory and so aids my learning. But no amount of googling sticky forms gave me the answer. You are gent, so thank you kindly. I can now close my laptop having accomplished my goals for today, create a contact form with several combo's doing different things through if statements etc, write a simple captcha to lessen future spam, (yuck almost as bad as the tinned variety lol) and the last goal was to find a solution to the question asked. I can now get some shut eye now. Thank you once again! Have a great Thursday Jc. Oraya Quote Link to comment https://forums.phpfreaks.com/topic/265228-how-can-i-re-write-this-checkbox-sticky/#findComment-1359254 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.