如何在php中以字符串形式回显的形式填充radiobuttons?

时间:2016-12-19 08:48:49

标签: php jquery mysql ajax forms

我有一个PHP脚本,它通过ajax从jquery接收数据,然后执行查询以从MySql中检索数据。在检索数据之后,我想用这些数据填充表格中的一堆单选按钮。表单附加到变量$output,它将作为字符串发送回ajax。如何根据数据填充单选按钮?我尝试了以下操作,但它所做的只是覆盖了字符串,结果如下"已检查"。任何帮助表示赞赏。

<td class="answers"><input type="radio" name="quality_of_service" value="4" "%'.($result["quality_of_service"] === 3) ? 'checked':''.'%"></td>

我的PHP

<?php
$output = '';
if(isset($_POST['id'])){
$id = $_POST['id'][0];

/*  print_r($id);*/
}
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//begin the transaction
$stmt = $conn->prepare("SELECT quality_of_service, self_improvement, personal_behavior, organization_rules_commitment, team_work, appearance, work_with_high_responsibility, loyalty_to_organization, punctuality_on_work, office_maintaining, areas_of_improvement, points_of_weakness, points_of_strength FROM appraisals_table WHERE Apr_Id = :id");



$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();

// set the resulting array to associative
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if (!empty($result)) {

$output .= '
<form id="reviewForm" action="" method="POST">

<div id="myRate">
How would you rank the employee in the following areas ?<span class="error">*</span><br>
<table class="myRateTable">
<tr id="table-header">
<th></th>
<th>Excellent<br><br>(4)</th>
<th>Above Average<br><br>(3)</th>
<th>Average<br><br>(2)</th>
<th>Poor<br><br>(1)</th>
</tr>
<tr>
<td class="questions">Quality of Service</td> 
<td class="answers"><input type="radio" name="quality_of_service" value="4" "%'.($result["quality_of_service"] === 3) ? 'checked':''.'%"></td>
<td class="answers"><input type="radio" name="quality_of_service" value="3"></td>
<td class="answers"><input type="radio" name="quality_of_service" value="2"></td>
<td class="answers"><input type="radio" name="quality_of_service" value="1"></td>
</tr>
</table>
</div>
</form>
';


echo $output;
}


}

catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}

$conn = null;

?>

1 个答案:

答案 0 :(得分:0)

为什么你不能让它变得更简单,

if($result["quality_of_service"] === 3)
{
  <td class="answers">
      <input type="radio" name="quality_of_service" value="4" checked>
  </td>
}
else
{
    <td class="answers">
      <input type="radio" name="quality_of_service" value="4">
    </td>
}