Jump to content

Trouble making a function - noob question


mr. big

Recommended Posts

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" '; ?> >&nbsp;
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" '; ?>
                        >&nbsp;
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" '; ?>
                      >&nbsp;
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]
<?php
function 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;
?>
>&nbsp;
Occasionally or never.<br>

<input type="radio" name="muscle" value="1.05"
<?php  $ch = altRadio('muscle', 1.05); echo $ch;
?>
>&nbsp;
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.)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.