Show label data in radio instead of text

Asked

Viewed 49 times

0

I have this appointment and I show it this way:

<?php 
if(isset($_POST['data']))
{
$servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

$inicio = mysqli_real_escape_string($conn, $_POST['data']);

$sql = "SELECT `regOratorio`.`DataEntrada`,
`regOratorio`.`Utente`,
`regOratorio`.`Estado`,
`regOratorio`.`Observacao`,
`regOratorio`.`Colaborador`
FROM `centrodb`.`regOratorio` WHERE DataEntrada = '$inicio'";

 $result=mysqli_query($conn, $sql);

if (!$result) {
  echo 'There are no results for your search';
  } else {
 echo '<form name="customer_details" action="" method="POST" onsubmit="return alguma_funcao()">';    
 }
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{

echo "<fieldset>";
echo "<table cellspacing='10'>";
 echo "<tr>";
echo "<td>";   

echo "<label>Utente</label>"; 
echo "<input id='' type='text' value='".$row['Utente']."'";  

echo "<label>Estado</label>"; 
echo "<input id='' type='text' value='".$row['Estado']."'";  

echo "<label>Observacao</label>"; 
echo "<input type='text' value='".$row['Observacao']."'";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</fieldset>";

echo "<fieldset>";
echo "<table cellspacing='10'>";
echo "<tr>";
echo "<td>";
echo "<input type='text' value='".$row['Colaborador']."'";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</fieldset>";

 }
echo "</form>";
mysqli_close($conn);
} 
else
{
?>

<form action="" method="post">
  <label>Enter Student Number:</label>
  <input name="data" type="date" placeholder="Type Here">
  <br>
  <br>
  <input type="submit" value="Enter">
</form>

<?php
}
?>

The label of State is as type='text', but wanted him to show the query data as type='radio', where I have two states (Present and Absent), I want you to learn both and select what was recorded in the table and I want to be able to edit that field (state) and that of Observing. It is possible?

  • 2

    Bruno you motre only 1 state or all? wouldn’t it be better in a buy select?

  • I only have two states, the present and absent, I want to show both and that appears selected what is recorded in the table, and I want to give the possibility to edit this field (state) and the observation, is possible?

1 answer

0


Well come on,

just change this code of yours:

echo "<label>Estado</label>"; 
echo "<input id='' type='text' value='".$row['Estado']."'";  

therefore:

echo "<label>Estado</label>"; 
echo "<input type='radio' value='presente' " . ( ($row['Estado']=='presente') ? 'checked' : '' ) ."> Presente";
echo "<input type='radio' value='ausente' " . ( ($row['Estado']=='ausente') ? 'checked' : '' ) ."> Ausente";

Php allows you to write an if like this:

($row['Estado'] == ausente)?
 'checked' : ''>Ausente"

which is saying if your state ($Row['State']) is equal to absent, then mark this radio as checked if you don’t put anything.

I think it’s worth showing you what a select combo would look like.

A visual example of what it would look like with select and radio

Estados com select: <br>
<select name="estados">
    <option value="sp">São Paulo</option>
    <option value="rj">Rio de Janeiro</option>
    <option value="mg">Minas Gerais </option>
</select>

<br>
<br>

Estados radio: <br>
<input type="radio" name="sp" value="sp" checked>São Paulo<br>
<input type="radio" name="rj" value="rj">Rio de Janeiro<br>
<input type="radio" name="mg" value="mg"> Minas Gerais 

  • young man, I just checked here and there’s a syntax error, I’m changing now and try again

  • If putting as you said gives error 500 on the page, then I made a small change, which was echo "<input type='radio' value='Presente' ('".$row['Estado']."' == Presente)? 'checked' : ''> Presente"; echo "<input type='radio' value='ausente' ('".$row['Estado']."' == ausente)? 'checked' : ''> Ausente";, but none is filled, remain empty

  • 1

    @Brunopinto I changed the code, now puts the code I put, and tells me how he is absent and present saved in his bank. Thus: Present or so Present or Present

  • Your amendment no longer makes a mistake, but radios are empty, does not fetch the information from the database table.

  • the information is already coming from the bank in this variable $Row['Status'], but it will only be checked if it is equal to the present or absent, ( if in the table of your bank is saved in upper case for example Present or Present. then it will not mark checked, so I asked how it is registered in the bank, has how to say:?

  • Bruno the if compares one thing to another. that => 'Present' is different from that 'present' that tb is different from that => 'PRESENT'

  • Solved, thanks, the problem was just that. Now as I edit this field case State need to change?

  • I’m on chat, but I leave here what I want: Let’s imagine that whoever made the registration made a mistake and put the client present but after all he was not present, I want to be able to change the registration

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.