0

I wanted to echo a bunch of html code if a user is on a specific rank. but I get an error Parse error: syntax error, unexpected '?'

PHP Code

<?php if ($row['rank'] == 0) { echo '

//SOME CODE HERE
                                <label>Question 1: </label>
<input class="au-input au-input--full" style="width: 50%;" type="text" name="question1" placeholder="Question 1" value="' . $question1 . '"></input>
<input type="checkbox" name="q1multiple"' <?php if(isset($_POST['q1multiple'])) { echo "checked='checked'"; } ?> '>
<label>Allow more than one answer </label>
</div>

//SOME CODE HERE
end of echo';} ?>

The error is in the if(isset($_POST['q1multiple'])) { echo "checked='checked'" part.

What should I do with it?

Jaocampooo
  • 482
  • 3
  • 10

2 Answers2

1

You should learn basics of PHP, you are inside an echo, don't open <?php again, just use :

<input type="checkbox" 
     name="q1multiple"
     '. (isset($_POST['q1multiple'])) ? "checked='checked" : '') .'
>

It's only string concatenation.

Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • Thanks, I created the html code outside an echo, and decided to put it inside an echo, my bad. Thank you for this. – Jaocampooo Jun 08 '19 at 10:33
1

You can't do an PHP tag inside an PHP-tag. Try it with an short-if command like: (isset($_POST['q1multiple']) ? 'checked="checked"' : '')

Here an fix of your code:

<?php if ($row['rank'] == 0) { echo '

//SOME CODE HERE
                                <label>Question 1: </label>
<input class="au-input au-input--full" style="width: 50%;" type="text" name="question1" placeholder="Question 1" value="' . $question1 . '"></input>
<input type="checkbox" name="q1multiple"' . (isset($_POST['q1multiple']) ? 'checked="checked"' : '') . '>
<label>Allow more than one answer </label>
</div>

//SOME CODE HERE
end of echo';} ?>
Richard
  • 618
  • 1
  • 9
  • 15
  • This solves the problem but the explanation is not what the problem is – Andreas Jun 08 '19 at 10:16
  • This one worked! Thank you so much. Can someone explain to me whats the "?" and ": ' ')" means that fix? So I could learn as well. :) – Jaocampooo Jun 08 '19 at 10:31
  • @James Ocampo it's called a "ternary operator". You can find info about it in the PHP docs. It's basically a short way of writing a simple "if" statement – ADyson Jun 08 '19 at 11:06