JSON Echo切换复选框和单选按钮状态

时间:2014-10-11 22:19:18

标签: php html mysql ajax json

我希望你能帮助我,我正在尝试从MySQL数据库中检索数据并更新复选框,单选按钮和文本区域。目前,我似乎只能更新输入字段。

在主页上有一个表,它启动使用PHP并使用while循环填充来自MySQL的数据。其中一列有一个隐藏字段的表单,其中包含SQL返回的每行数据的主键,也包含一个按钮。

选择按钮时,会进行以下Ajax调用以将数据加载到表单中。

我无法弄清楚以下帖子How to check radio buttons,我不想选择按钮来更改单选按钮或复选框。

$(function() {
$(".mlsload").click(function() {
var id = ($(this).val());
var mlsid = $("#mlsidbutton"+id).val();
var odrid = $("#odridbutton"+id).val();
var dataString = 'drop_3='+ mlsid + '&drop_2=' + odrid;

$.ajax({
type: "POST",
async: false,
url: "../EPRS/functions/reportsdatamls.php",
dataType: 'json',
data: dataString,
success: function( data ) {
       for(var dataString in data) {        
                   $("input#"+dataString).val( data[dataString] );
                       }

                }
});



<?php
$mlsid = $_POST['drop_3']; // Reads the value of drop 3 form the project reports section - this identifies the milestone to load

//mysql_connect("localhost", "root", "") or
  //die("Could not connect: " . mysql_error());
//mysql_select_db("miranda_prj_report");
include('../mysqlconn/db.php');
// loads data from my SQL table to enter into an array. The array is then used to populate field dataon http forms
$query  = "SELECT * FROM `miranda_prj_report`.`prj_mls_milestones` where mls_id = $mlsid";
$result = mysql_query($query,$sql_con);
$row = mysql_fetch_array($result, MYSQL_ASSOC);

// Inserts all values from the view into the following veriables
$mlsid = $row['mls_id'];
$odrid = $row['odr_id'];
$mlsterms = $row['mls_Milestone_Terms'];
$mlsvalue = $row['mls_Milestone_Value'];
$mlspercentage = $row['mls_Milestone_Percentage'];
$mlsbaselinedate = $row['mls_Baseline_Date'];
$mlsexpecteddate = $row['mls_Expected_Date'];
$mlsstatus = $row['mls_Milestone_Status'];
$mlsrisk = $row['mls_Milestone_Risk'];
$mlsnotes = $row['mls_Milestone_Notes'];
$AcceptanceTrigger = $row['mls_Acceptance_Trigger'];
$MLSBillable = $row['mls_Billable'];


// Creates an Array and update the Fields representing these values on the project reports page under the orders tab
$mlsarr = array(
'MilestoneDescription'=> $mlsterms,
'MilestoneRiskDB' => $mlsrisk,
'MilestoneStatus' => $mlsstatus,
'MilestoneBaselineDate' => $mlsbaselinedate,
'MilestoneExpectedDate' => $mlsexpecteddate,
'MilestoneNotes' => $mlsnotes,
'MilestoneID'=> $mlsid,
'MilestonePercentage'=> $mlspercentage,
'MilestoneValue'=> $mlsvalue,
'AcceptanceTrigger'=> $AcceptanceTrigger,
'MLSBillable' => $Billable == "1",
);

echo json_encode( $mlsarr );
mysql_close();
?>

JSON Echo数据:

  

{“MilestoneDescription”:“出货量的50%   商品“,”MilestoneRiskDB“:”绿色“,”MilestoneStatus“:”完整“,”MilestoneBaselineDate“:”2014-09-12“,”MilestoneExpectedDate“:”2014-09-12“,”MilestoneNotes“:”已发货第12   七重峰”, “MilestoneID”: “1599”, “MilestonePercentage”: “102945.00”, “MilestoneValue”: “51472.50”, “AcceptanceTrigger”: “”, “MLSBillable”:真}

上面你可以看到MLSBillable设置为true。

在HTML表单中,有许多字段可以填充,但仅当字段是类型输入时才会填充。我需要切换复选框和单选按钮,但无法解决问题。

<label for="MLSBillable">Billable Milestone </label><input type="checkbox" name="MLSBillable" ID="MLSBillable"/>

<label for="MilestoneRisk">Update Milestone Risk</label> 
<input type="radio" name="MilestoneRisk" value="Red" id="MilestoneRisk_0" /><label for="MilestoneRisk">Red</label>
<input type="radio" name="MilestoneRisk" value="Amber" id="MilestoneRisk_1" /><label for="MilestoneRisk">Amber</label>
<input type="radio" name="MilestoneRisk" value="Green" id="MilestoneRisk_2" /><label for="MilestoneRisk">Green</label>

最后,我如何更新textarea的任何示例也会有所帮助。

非常感谢您的支持。

0 个答案:

没有答案