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>
PHP

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
Julian Hansen

Where is $type being set?
Michel Plungjan

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


Simon Leung

ASKER
Your help has saved me hundreds of hours of internet surfing.
fblack61
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
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michel Plungjan

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