I want to have the checkbox seleted if the productNeedsAudit from the Json value is True. But I new to php and I am also stuck implementing the logic with an echo statement. I am trying the following:
echo "<tr><td><div class='control-group controls'>
<label for='productNeedsAudit'>
<input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True'" . $soJson->{'productNeedsAudit'} == 'True' ? 'checked' : '' . ">
Needs Audit
</label>
</div></td></tr>";
In the results, the check box isn't selected. Instead, I am getting the following in the web browser.
> Needs Audit
Here's the HTML
<table>
<tr>
<td>
<select style='width: 100%;' id='productType' name='productType'>
<option value='TEST: TEST</option>
</select>
</td>
</tr>
<tr>
<td>
<div class='control-group controls'>
<label for='productDescription'>Description: </label>
<input type='text' name='productDescription' value='*.1stdibs.com, *.1stdibs.us.com' />
</div>
</td>
</tr>
<tr>
<td>
<div class='control-group controls'>
<label for='productPriceBookDescription'>Price Book Description: </label>
<input type='text' id='priceBookDesc' name='productPriceBookDescription' style='width:100%' value='' />
</div>
</td>
</tr>
/>
Needs Audit
</label>
</div></td></tr>
<tr>
<td>
<div class='control-group controls'>
<label for='productServiceOrderNumber'>Service Order: </label>
<select id='productServiceOrderNumber' name='productServiceOrderNumber'>
<option value='992769:53:14' selected='selected'>992769:53:14</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
<input id='subcustomers' name='subcustomers'>
</input>
<input id='subproducts' name='subproducts'>
</input>
</td>
</tr>
<tr>
<td>
<input id='tags' name='tags'>
</input>
</td>
</tr>
</td></tr>
</table>
Group the ternary operator.
echo "<tr><td><div class='control-group controls'>
<label for='productNeedsAudit'>
<input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True'" . ($soJson->{'productNeedsAudit'} == 'True' ? ' checked' : '') . ">
Needs Audit
</label>
</div></td></tr>";
Longer write up on ternary operator. http://davidwalsh.name/php-shorthand-if-else-ternary-operators
Functional example:
<?php
$a = "False";
echo "<tr><td><div class='control-group controls'>
<label for='productNeedsAudit'>
<input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True'" . ($a == 'True' ? ' checked' : '') . ">
Needs Audit
</label>
</div></td></tr>";
Output:
<tr><td><div class='control-group controls'>
<label for='productNeedsAudit'>
<input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True'>
Needs Audit
</label>
</div></td></tr>
As true:
<tr><td><div class='control-group controls'>
<label for='productNeedsAudit'>
<input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True'checked>
Needs Audit
</label>
</div></td></tr>
Note I added a space in the checked
attribute. I think some browsers also prefer that to be checked="checked"
.
When your expression isn't grouped the PHP is checking does " <tr><td><div class='control-group controls'><label for='productNeedsAudit'><input name='productNeedsAudit' type='checkbox'id='productNeedsAudit' value='True'
+ what ever $soJson->{'productNeedsAudit'}
is" equal True
. Since it doesn't it goes to the second part of the ternary operator and outputs:
'' . ">
Needs Audit
</label>
</div></td></tr>"