我想获取oracle sql查询结果数据并在php中修改它们,最后发送它们以使用JQuery,AJAX在不同的表单字段中输出它们。
这是我的php代码:
<?php
$loanid = (isset($_POST['loanid'])) ? $_POST['loanid'] : "not";
$loan_name = (isset($_POST['loan_name'])) ? $_POST['loan_name'] : "not";
$description = (isset($_POST['description'])) ? $_POST['description'] : "not";
$amount = (isset($_POST['amount'])) ? $_POST['amount'] : "not";
$datestring = (isset($_POST['textdatetimepicker1'])) ? $_POST['textdatetimepicker1'] : "not";
$rate = (isset($_POST['rate'])) ? $_POST['rate'] : "not";
echo "-starts: ";
echo $loanid;
echo $loan_name;
echo $description;
echo $amount;
$date_arr = explode('/', $datestring);
$date = date("Y/m/d", strtotime($date_arr[2] . $date_arr[1] . $date_arr[0]));
echo $datestring;
echo $rate;
echo " ends. ";
$conn = oci_connect('himadri', 'himadri', 'cse.du.ac.bd/duais_test');
if (!$conn) {
trigger_error("Could not connect to database", E_USER_ERROR);
} else {
echo "<br>";
echo "Connection established.";
}
$stid = oci_parse($conn, " begin
:result := PKG_PAYROLL.save_loan_type(
'<loan_type>
<loan_id>$loanid</loan_id>
<loan_name>$loan_name</loan_name>
<description>$description</description>
<amount>$amount</amount>
<date>$date</date>
<rate>$rate</rate>
</loan_type>'
);
end;");
oci_bind_by_name($stid, ':result', $ru, 5000);
$output = oci_execute($stid);
echo "<br>";
echo " \n SQL Query Result: \n ";
echo $ru;
$string = $ru;
$xml = simplexml_load_string($string);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
echo json_encode($array);
?>
例如,如果上面的查询结果返回json输出,如下所示:
{"status":"success","loan_type":{"loan_id":"123","loan_name":"Bank Loan","description":"FRB Bank Loan","amount":"1250000","date":"2016-01-26","rate":"5.257"}}
我想改变他们的价值,例如:
这是我的HTML和AJAX JQuery代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Record Loan Type Information</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body >
<div class="container" >
<div class="panel-group">
<div class="panel panel-primary" >
<div class="panel-heading" >
<h3 class="panel-title" style="text-align: center;">Record Loan Type Information</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" >
<div class="form-group">
<label class="control-label col-sm-3" for="acode">Loan ID:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="textloanid" name="loanid" placeholder="Enter Loan ID">
<span id="errmsg1" class="errmsg"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" style="text-align:right;" >Loan Name:</label>
<div class="col-sm-5">
<input type="text" id="textloan_name" name="loan_name" class="form-control" placeholder="Enter Loan Name" >
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="dcode">Description:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="textdescription" name="description" placeholder="Enter Loan Description">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" style="text-align:right;">Amount:</label>
<div class="col-sm-5">
<input type="text" id="textamount" name="amount" class="form-control" placeholder="Enter Amount" >
<span id="errmsg2" class="errmsg"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" style="text-align:right;" >Sanction Date:</label>
<div class="col-sm-5">
<div class="input-group date">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
<input type="text" id='textdatetimepicker1' name="textdatetimepicker1" class="form-control" placeholder="DD/MM/YYYY">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" style="text-align:right;">Interest Rate:</label>
<div class="col-sm-5">
<input type="text" id="textrate" name="rate" class="form-control" placeholder="Enter Rate" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="btnSubmit" class="btn btn-primary">Save</button>
<button type="submit" name="editbtn" class="btn btn-success editbtn">Edit</button>
<button type="button" class="btn btn-danger">Delete</button>
<button type="button" class="btn btn-info">Exit</button>
</div>
</div>
</form>
<!-- The result Serialize Text Output inside this div -->
<div id="serialize">Serialize Text Output: </div>
<!-- The result of the search will be rendered inside this div -->
<div id="result">Text Output: </div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var values = $("form").serialize();
var i=0;
$("form").on("submit", function( event ) {
event.preventDefault();
var values = $( this ).serialize();
$('#serialize').append(values);
$.ajax({
url: "loan_type_info.php",
type: "post",
async:true,
data: values,
dataType: 'html',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(data) {
$('#result').append('Response Data:');
$('#result').append(data);
$('#result').append('<br>');
$('#result').prop('loan_id', data.loan_id);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
</script>
</body>
</html>
在上面的代码中,我想将json数据粘贴到适当的输入字段中,例如:
更新:
$('#result').append('Response Data:');
$('#result').append(data);
I got :Response Data:123 Bank Loan FRB Bank Loan 1250000 26/01/2016 12.25
$('#result').append('Response JSON parse:');
data=JSON.parse(data);
$('#result').append(data);
I got :Response JSON parse: (nothing)
我该怎么做。请帮我解释一下这段代码。谢谢
答案 0 :(得分:2)
评论的后续行动。从服务器解析data
后,您现在可以访问data
中的值并将它们放在任何您想要的位置。
success: function(data) {
$('#result').append('Response Data:');
$('#result').append(data);
$('#result').append('<br>');
data=JSON.parse(data);
$('#textloanid').val(data.loan_type.load_id);
$('#textloan_name').val(data.loan_type.loan_name);
$('#result').prop('loan_id', data.loan_type.loan_id);
}
使用jQuery更改<input>
标签而不使用ajax:
$('#textloanid').val("YOUR_DESIRED_OUTPUT");
答案 1 :(得分:1)
在查询之后获得数据后,创建一个新数组并使用循环复制原始数据中的每个关联元素,一旦看到要修改的元素,只需将条件编码到循环中。完成循环后,您将获得符合规格的新数组。也可能想要在复制之后取消原始设置以将mem使用保持在最低限度。