Link to home
Start Free TrialLog in
Avatar of Simon Leung
Simon Leung

asked on

radio button in php echo

I have set the $type variable at the beginning of PHP code but it doesn't restore correct in my php. Any idea ?

  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="Student" "($type=='Student')?'checked':''">


echo
<<<HTML
<form method="POST" action="{$_SERVER['PHP_SELF']}">
<div class="container">
<br/>
<b>Please fill in the following details:</b>
<br/><br/>
Ticket Type: <br/>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="Regular" ($type=='Regular')?'checked':''>
  <label class="form-check-label" for="inlineRadio1">Regular</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="Student" "($type=='Student')?'checked':''">
  <label class="form-check-label" for="inlineRadio2">Student</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="Retire" "($type=='Retire')?'checked':''">
  <label class="form-check-label" for="inlineRadio3">Retire</label>
</div>
<br/><br/>
<input class="form-control col-lg-6" type="int " id="id" name="id" placeholder="Student ID" value="$id" required="" ><br/>

<input class="form-control col-lg-6" type="text" placeholder="First Name" name="fname" value="$fname" required="" ><br/>

<input class="form-control col-lg-6" type="text" name="lname" placeholder="Last Name" value="$lname" required="" ><br/>

<br/><br/>
<input type="submit" name="submit" value="submit" class="btn btn-primary">
<input type="reset" name="reset" value="Clear" class="btn btn-primary">
<div/>
</form>
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Where is $type being set?
Also you do not seem to have the same quotes in every radio

value="Student" "($type=='Student')?'checked':''">
value="Regular" ($type=='Regular')?'checked':''>
value="Retire" "($type=='Retire')?'checked':''">

Open in new window


I would remove the echo and do

value="Student"<?= $type=='Student' ? ' checked' :'' ?>>
value="Regular"<?= $type=='Regular' ? ' checked' :'' ?>>
value="Retire"<?= $type=='Retire'  ? ' checked' :'' ?>>

Open in new window


Avatar of Simon Leung
Simon Leung

ASKER

Still doesn't work.

The HTML code after runnin the php is :

<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="Regular" (Regular=='Regular')?'checked':''>
  <label class="form-check-label" for="inlineRadio1">Regular</label>
</div>
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My suggestion works http://sandbox.onlinephpfunctions.com/code/62ff6bc1e3e9e73b7fc0e7bab0c4d4bf14e6c74d assuming your PHP handles the <?= ?> syntax

<?php
$type="Student";
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<div class="container">
<br/>
<b>Please fill in the following details:</b>
<br/><br/>
Ticket Type: <br/>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="Regular"<?=$type=='Regular'?' checked':'' ?>>
  <label class="form-check-label" for="inlineRadio1">Regular</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="Student"<?= $type=='Student'?' checked':''?>>
  <label class="form-check-label" for="inlineRadio2">Student</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="Retire"<?= $type=='Retire'?' checked':'' ?>>
  <label class="form-check-label" for="inlineRadio3">Retire</label>

Open in new window