php in an input field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

php in an input field

Post by chopWood »

I'm trying to put a php variable in the value of a radio input field but can't get the syntax correct:

echo '<input type="radio" name="textcat" value=" 'textCats[0])' " checked = "checked"/> biography';


thanks for you assistance
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: php in an input field

Post by yacahuma »

Code: Select all

echo "<input type=\"radio\" name=\"textcat\" value=\"{$textCats[0]}\ " checked = \"checked\"/> biography";

or

echo '<input type="radio" name="textcat" value="' . $textCats[0]) . ' " checked = "checked"/> biography';
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: php in an input field

Post by chopWood »

I probably should have given the complete context which is as follows:

Code: Select all

echo '<form id="form2" name="form1" method="post" action="textEdit.php">';

if ($_POST['textcat']== $textCats[0]){
	echo '<input type="radio" name="textcat" value=" '.textCats[0].' " checked = "checked" /> biography';
	} else {
	echo '<input type="radio" name="textcat" value=" '.textCats[0].' " /> biography';
	}
// It still comes up with a parse error:
Parse error: syntax error, unexpected '[', expecting ',' or ';' in /Users/garylee/Desktop/

If I replace the textCats[0] with $textCats, however, there is no error. So how do I include a subscripted variable?
thanks
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: php in an input field

Post by chopWood »

Okay, thank you for the suggestion of adding the dots as in .textCats[0]. That was one of my mistakes. The other was proobably too obvious to recognize but it should have been .$textCats[0]. as that was the previously defined array... der all work and no play makes Gary a dull boy.

thanks again
Post Reply