Jump to content

Archived

This topic is now archived and is closed to further replies.

mr. big

Trouble making a function - noob question

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.)

Share this post


Link to post
Share on other sites
in the function put in right after the start of the function:

global $_POST;

that SHOULD do it.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.