2

I am hoping you can help me, I am trying to retrieve data from a MySQL database and update a checkbox, radio button and text area. At the moment all I can seem to update is input fields.

On the homepage there's a table which initiates using PHP and using a while loop populates data from MySQL. One of the columns there is a form with hidden fields that contain the primary key for each row of data returned by the SQL, also a button.

When selecting the button the following Ajax call is made to load data into a form.

I could not figure out the following post How to check radio buttons and I don't want to select a button to change the radio buttons or check box.

$(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();
?>

The JSON Echo Data:

{"MilestoneDescription":"50% on shipment of Goods","MilestoneRiskDB":"Green","MilestoneStatus":"Complete","MilestoneBaselineDate":"2014-09-12","MilestoneExpectedDate":"2014-09-12","MilestoneNotes":"Shipped on 12th Sept","MilestoneID":"1599","MilestonePercentage":"102945.00","MilestoneValue":"51472.50","AcceptanceTrigger":"","MLSBillable":true}

Above you can see the MLSBillable is set to true.

In the HTML form there are many fields which are populated fine but only if the field is type input. I need to toggle the checkbox and radio buttons but cant work out how.

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

Finally any examples of how I can update a textarea would also help.

Thank you very much for your support.

Community
  • 1
  • 1
Tai
  • 21
  • 1
  • Your question is very broad, and doesn't contain much error information. Please be clear about what part goes wrong, and strip down the code as far as possible. If the problem is in parsing the JSON, leave out the entire server side part. After all, the JSON could even be static content, and should still be handled the same by JavaScript. Also, include any errors your get (see console window) and describe actual and desired behaviour. NB: `mysql_*` functions are outdated and deprecated. You should not use them. – GolezTrol Oct 11 '14 at 22:23
  • Thank you GolezTrol I am half way through the site to change to mysqli No Console Errors The server side is fine and JSON Data returned. When I Echo the JSON Data I can load all the relevant data (10 Fields) into the form. I am struggling with toggling check boxes on the HTML form which has an id of "MLSBillable". $Billable= $row['mls_Billable']; $mlsarr = array( 'MLSBillable' => $Billable == "1", ); echo json_encode( $mlsarr ); – Tai Oct 11 '14 at 22:53
  • I tried to parse the json data while I am in the success function of the Ajax call. Still no luck added the following $(":input[name='" + dataString + "']").attr("checked", jsonOBJ[dataString]); – Tai Oct 11 '14 at 23:53
  • also tried in success function of Ajax call $(":input[name='" + dataString + "']").proc("checked", jsonOBJ[dataString]); – Tai Oct 11 '14 at 23:54
  • I have managed to finally find the solution. – Tai Nov 20 '14 at 22:13
  • // Toggle customer Name Box on Json data return var CustomerNameData = data.CustomerName; $(function() { $('[name=CustomerID] option').filter(function() { return ($(this).text() == CustomerNameData); }).prop('selected', true); }) – Tai Nov 20 '14 at 22:13
  • The above parse json data and sets the select box based on the text data not value – Tai Nov 20 '14 at 22:14
  • You can then answer your own question. A nice answer with formatted code is more useful for future visitors, and might earn you some upvotes too. See: http://stackoverflow.com/help/self-answer – GolezTrol Nov 20 '14 at 22:21

0 Answers0