[英]how to load a value from database in radio button?
I need to know if you can do this function ... I have a database that has a "boolean" field that accepts 0 or 1 values And I have an HTML page in which there is a radioButton with values Ok or No I must be able to load the boolean value in the radioButton so if in the database the boolean variable is 1 on the HTML page (after executing the SELECT) the radioButton will have OK value if in the database the boolean variable is 0 on the HTML page (after executing the SELECT) the radioButton will have NO value 我需要知道您是否可以执行此功能...我有一个数据库,该数据库的“布尔”字段接受0或1个值,并且我有一个HTML页面,其中有一个单选按钮,其值必须为Ok或No,我必须能够在radioButton中加载布尔值,因此,如果在数据库中HTML页面上的boolean变量为1(执行SELECT之后),则如果数据库中HTML页面上的boolean变量为0(之后,则radioButton将具有OK值)执行SELECT)单选按钮将没有值
this is the code of my attempts : 这是我尝试的代码:
<?php $servername = "localhost"; $username = "progettocantiere"; $password = ""; $dbname = "my_progettocantiere"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $idAffidataria = $_GET['idAffidataria']; $sql1 = "SELECT * FROM Affidataria WHERE idAffidataria = '{$idAffidataria}'"; $result = $conn->query($sql1); $details = $result->fetch_array(); $savedNomeCantiere = $details["nomeCantiere"]; $savedAddettoSicurezza = $details["addettoSicurezza"]; $savedMailAffidataria = $details["mailAffidataria"]; $savedContrattoDiAppalto = $details["contrattoDiAppalto"]; // <--- BOOLEAN <---- $result1 = $conn->query($sql1); echo($nomeCantiere); ?> <html> <body> <table> <tr> <td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">Cantiere</td> <td colspan="4" bgcolor="#CDECFD"><input type="text" class="form-control" style="width: 100%;" name="cantiereAffidataria" id="cantiereAffidataria" value="<?php echo $savedNomeCantiere; ?>"/> </td> </tr> <tr> <td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">ContrattoDiAppalto</td> <td bgcolor="#B35556"><form action=""> OK <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" /> <BR> NO <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" checked/> </form></td> </tr> </table>
in other words I have to be able to change the radioButton according to the data provided by the SELECT on the database 换句话说,我必须能够根据SELECT在数据库上提供的数据来更改radioButton
I tried to give it the value through <?php echo $savedContrattoDiAppalto; ?>"
我试图通过<?php echo $savedContrattoDiAppalto; ?>"
给它赋值<?php echo $savedContrattoDiAppalto; ?>"
<?php echo $savedContrattoDiAppalto; ?>"
but I doubt it's right .. I'm running out of ideas .. can someone help me? <?php echo $savedContrattoDiAppalto; ?>"
但我怀疑这是正确的..我的想法已经用完了..有人可以帮助我吗?
Make an variable $checked = 'checked'
使变量$checked = 'checked'
Then make an condition to check if it's 1 or 0 然后进行条件检查是1还是0
$checked = 'checked';
if($savedContrattoDiAppalto != 1){
$checked = ''; // Default is checked, if it isn't 1 then it's empty and will not check your radio.
}
And apply that variable to your input attributes. 并将该变量应用于您的输入属性。
<input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto"
value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" $checked/>
Full code should be : 完整代码应为:
<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$idAffidataria = $_GET['idAffidataria'];
$sql1 = "SELECT * FROM Affidataria WHERE idAffidataria = '{$idAffidataria}'";
$result = $conn->query($sql1);
$details = $result->fetch_array();
$savedNomeCantiere = $details["nomeCantiere"];
$savedAddettoSicurezza = $details["addettoSicurezza"];
$savedMailAffidataria = $details["mailAffidataria"];
$savedContrattoDiAppalto = $details["contrattoDiAppalto"];
$checked = 'checked';
if($savedContrattoDiAppalto != true){
$checked = ''; // if not true then not checked
}
$result1 = $conn->query($sql1);
echo($nomeCantiere);
?>
<html>
<body>
<table>
<tr>
<td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">Cantiere</td>
<td colspan="4" bgcolor="#CDECFD"><input type="text" class="form-control" style="width: 100%;" name="cantiereAffidataria" id="cantiereAffidataria" value="<?php echo $savedNomeCantiere; ?>"/> </td>
</tr>
<tr>
<td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">ContrattoDiAppalto</td>
<td bgcolor="#B35556"><form action="">
OK <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" $checked/> <BR>
NO <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" $checked/>
</form></td>
</tr>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.