We help IT Professionals succeed at work.

radio button in php echo

Simon Leung
Simon Leung asked
on
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>
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Where is $type being set?
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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':''">

I would remove the echo and do

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

Author

Commented:

Author

Commented:
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>
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
A few of things
1. checked is an attribute it does not have to appear in quotes
2. $_POST['inlineRadioOptions'] is not guaranteed to exist
3. You can't use expressions inside a HEREDOC. Evaluate outside and use the evaluated vars inside

Firstly do this (use this for all your post variables.
if (isset ($_POST['submit']) && isset ($_POST['fname'])) {
  ...
  $type= isset($_POST['inlineRadioOptions']) ? $_POST['inlineRadioOptions'] : '';

Open in new window



Then do this
$regularChecked = $type == 'Regular' ? 'checked' : '';
$studentChecked = $type == 'Student' ? 'checked' : '';
$retireChecked = $type == 'Retire' ? '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" {$regularChecked}>
  <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" {$studentrChecked}>
  <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" {$retireChecked}>
  <label class="form-check-label" for="inlineRadio3">Retire</label>
</div>

Open in new window

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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>