mr. big Posted October 15, 2006 Share Posted October 15, 2006 I have a sticky form (I actually invented the sticky code for non-text buttons myself, not that it hasn't been done a thousand times before LOL). Here's the applicable form snippet:[code] Do you do regular weight training?<br> <input type="radio" name="muscle" value="1" <?php if ((!isset($_POST['muscle'])) or ($_POST['muscle'] == "1")) echo 'checked="checked" '; ?> > Occasionally or never.<br> <input type="radio" name="muscle" value="1.05" <?php if ((isset($_POST['muscle'])) and ($_POST['muscle'] == "1.05")) echo 'checked="checked" '; ?> > Yes, as part of my exercise routine.<br /> <input type="radio" name="muscle" value="1.2" <?php if ((isset($_POST['muscle'])) and ($_POST['muscle'] == "1.2")) echo 'checked="checked" '; ?> > Yes and I have gained significant muscle bulk.<br />[/code]The php tests the first entry and writes[i] checked="checked"[/i] into the form 1) by default, i.e. if no value is set, or 2) if it has previously been chosen. It tests subsequent buttons and inserts [i] checked="checked"[/i] only if the button has previously been chosen. So far so good.I have tried fifty ways from Sunday to make a formula that will work in the stickies so I can get rid of the awful clutter. Here's my last try:[code]<?phpfunction defRadio ($namex, $valuex) { if ((!isset($_POST['$namex'])) or ($_POST['$valuex'] == "$valuex")) { return 'checked="checked"'; } else { return 'foobar'; }}function altRadio ($namey, $valuey) { if ((isset($_POST['$namey'])) and ($_POST['$valuey'] == "$valuey")) { return 'checked="checked"'; } else { return 'foobar'; }}?> <div class="bmi_l"> <span class="b"> Do you do regular weight training?</span><br> <input type="radio" name="muscle" value="1" <?php $ch=defRadio('muscle', 1); echo $ch; ?> > Occasionally or never.<br> <input type="radio" name="muscle" value="1.05" <?php $ch = altRadio('muscle', 1.05); echo $ch; ?> > Yes, as part of my exercise routine.<br />[/code]The problem I'm having is that the form always fills in the first button, even if the second (third, fourth) button was previously checked. The word "foobar" does echo in the altRadio choices. I'm thinking the $_POST values aren't making it to the formula, but I've tried every way I know to get them in.Herlp!(The "foobar" value is just for debugging.) Quote Link to comment https://forums.phpfreaks.com/topic/24015-trouble-making-a-function-noob-question/ Share on other sites More sharing options...
Jocka Posted October 16, 2006 Share Posted October 16, 2006 in the function put in right after the start of the function:global $_POST;that SHOULD do it. Quote Link to comment https://forums.phpfreaks.com/topic/24015-trouble-making-a-function-noob-question/#findComment-109240 Share on other sites More sharing options...
mr. big Posted October 16, 2006 Author Share Posted October 16, 2006 Thanks for the response, but it doesn't work.I think this is on the right track, though -- the POST variable is not available to the function for a reason I don't understand. I'm still working away on this :'(This is really confusing, because $POST is a superglobal. I'm going to try $REQUEST. Quote Link to comment https://forums.phpfreaks.com/topic/24015-trouble-making-a-function-noob-question/#findComment-109481 Share on other sites More sharing options...
Daniel0 Posted October 16, 2006 Share Posted October 16, 2006 You put a variable inside single quotes, then it wont get parsed.Use this (made smaller and fixed):[code]function defRadio($namex, $valuex){ return $_POST[$namex] == $valuex ? 'checked="checked"' : null;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24015-trouble-making-a-function-noob-question/#findComment-109484 Share on other sites More sharing options...
mr. big Posted October 16, 2006 Author Share Posted October 16, 2006 Thanks so much for your help, Daniel0. I feel silly discovering that the main problem was such a basic syntax mistake -- it's the power of misdirection, as I was concentrating on learning function-building.I doubly appreciate the code, as I'm not comfortable with the format and it will give me something tangible to learn. Quote Link to comment https://forums.phpfreaks.com/topic/24015-trouble-making-a-function-noob-question/#findComment-109517 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.