Jump to content


Photo

Trouble making a function - noob question


  • Please log in to reply
4 replies to this topic

#1 mr. big

mr. big
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 15 October 2006 - 07:39 PM

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:
								
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 />

The php tests the first entry and writes checked="checked" 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 checked="checked" 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:

<?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 />

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.)
"Every monkey thinks its children are beautiful."

#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 16 October 2006 - 12:30 AM

in the function put in right after the start of the function:

global $_POST;

that SHOULD do it.

#3 mr. big

mr. big
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 16 October 2006 - 12:43 PM

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.
"Every monkey thinks its children are beautiful."

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 12:49 PM

You put a variable inside single quotes, then it wont get parsed.

Use this (made smaller and fixed):
function defRadio($namex, $valuex)
{
	return $_POST[$namex] == $valuex ? 'checked="checked"' : null;
}


#5 mr. big

mr. big
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 16 October 2006 - 01:58 PM

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.
"Every monkey thinks its children are beautiful."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users