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

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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



PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo