0

So i have these radio buttons in XHTML that I want to put into a PHP function to generate and I can't get it to work.

In XHTML it looks like this and is working;

<p><input type="radio" value="<? echo blabla; ?>" name="radioA" checked="checked" /></p>
<input type="hidden" value="<? echo $value; ?>" name="hiddenA[]" />

In PHP I need to set the "radioA" and "hiddenA" to variables respectively "radioB"/"hiddenB", "radioC"/"hiddenC" and so on for my code to work. This is what I have so far but it is not working. The first radio name is a string, but the second one is array. Thanks in advance.

function radio($Radio, $Array) {
    echo '<p><input type="radio" value="$value>" name="$Radio" />', $value, '</p>';
    echo '<input type="hidden" value="$value" name="$Array" />';
}

I guess what I'm trying to do is to return the name of the variable as a string. $_POST['hiddenA'] ===> hiddenA[]

3
  • You have to use double qouted(") strings for variables in them to be expanded. Commented Oct 29, 2012 at 0:22
  • Where is the $Value coming from? "." append strings not "," in PHP. If the variable $Array is an array and you echo it it will echo an array. Perhaps you meant to access a value from the array? As a general rule you should use a lower case letter to begin a variable name: $array rather then $Array... Commented Oct 29, 2012 at 0:25
  • The problems is that I need to set up the name of the button to the name of the array, not the value of the array. So if i type it manually in XHTML name="hiddenA[]" works, but if i use PHP to echo it, it needs to be $_POST['hiddenA']. So that's where I get confused Commented Oct 29, 2012 at 0:50

2 Answers 2

2

Basic php syntax:

$a = 'hello';
echo '$a'; // outputs the literal characters $ and a
echo "$a"; // outputs "hello"
Sign up to request clarification or add additional context in comments.

Comments

0

You have a few syntax issues here:

function radio($Radio, $Array) {
    echo '<p><input type="radio" value="$Value>" name="$Radio" />', $Value, '</p>';
    echo '<input type="hidden" value="$Value" name="$Array" />';
}
  1. You have to use "" not '' when embedding variables:

    echo "My name is {$name}.";

  2. In PHP the . character is used to append strings not ,

    echo "My name is " . $name . " and I am cool";

  3. If you try to echo an array you will get the word Array not the array itself. Instead you can echo a value in the array:

    echo "$array[0]";

  4. You are trying to use a variable that is not defined in your example: $Value. This would give you an VariableUndefined exception.

  5. It is generally bad practice to use capital letters at the start of variable names in PHP. So $Array should be $array.

With these in mind you code should look something like:

function radio($radio, $array) {
    $value1 = $array[0];
    $value2 = $array[1];

    echo "<p><input type=\"radio\" value=\"$value1>\" name=\"$radio\" />$value1</p>";
    echo "<input type=\"hidden\" value=\"$value2\" name=\"$value2\" />";
}

From the sounds of your example you need a for loop somewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.