Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Probably a stupid question, but I can't figure it out, I need another person's perspective. Im guessing its wrong quotations or something but the error is;

Parse error: syntax error, unexpected '?' in C:\xampp\htdocs\Joomla-Lifestyle\components\com_jumi\files\medication.php on line 244

Line 244 is;

echo '<input type="radio" name="med" value="med' . $count . '" '<?php if(isset($_POST['view'])) echo checked="checked"; ?>'>' . $MedEntriesName . '<br>';

This line of code is in a loop and I just want to keep the radio button checked when the "View" button is clicked.

Thanks!

share|improve this question
    
Your putting <?php inside of a current php-block/sequence. – bestprogrammerintheworld Apr 19 '14 at 11:35

4 Answers 4

up vote 2 down vote accepted

Use like this

<?php
    $checked = isset($_POST['view']) ? 'checked="checked"' : '';
    echo '<input type="radio" name="med" value="med'.$count.'" '.$checked.'>'.$MedEntriesName.'<br>';
?>
share|improve this answer
    
This worked perfectly thank you! – user3266484 Apr 19 '14 at 11:53
    
you are welcome :) – PravinS Apr 19 '14 at 11:53

What you try is not correct. You can't open a new php sequence in an php block.

echo '<input type="radio" name="med" value="med' . $count . '" '<?php if(isset($_POST['view'])) echo checked="checked"; ?>'>' . $MedEntriesName . '<br>';
                                                                ^ here

So you have to concat the string. Its easier when you make the if before and use the variable in your string.

share|improve this answer

You shouldn't use < ?php ?> inside a PHP code. Use ternary operators.

share|improve this answer

You missed a . (dot), quotes around the checked and you don't need to open php in php:

echo '<input type="radio" name="med" value="med' . $count . '" '<?php if(isset($_POST['view'])) echo 'checked="checked"'; ?>'>' . $MedEntriesName . '<br>';
                                   ---- the dot right here -----^
// Correct line:
echo '<input type="radio" name="med" value="med' . $count . '" '.(if(isset($_POST['view'])) echo 'checked="checked"').'>' . $MedEntriesName . '<br>';

And I suggest the short if/else (known as ternary):

echo '<input type="radio" name="med" value="med'.$count.'" '.(isset($_POST['view']) ? 'checked': '').'>'.$MedEntriesName.'<br>';

Small example for the ternary:

if( 1==1 ){ echo 'yes';} // standard if/else
else{ echo 'no'; }

echo 1==1 ? 'yes': 'no'; // this does the same

Also, to improve usability, you should wrap it in a label. If the label-text gets clicked, it will check the radiobutton. A lot of people expect this behaviour:

<label><input type="radio" /> Some text</label>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.