【发布时间】:2023-03-24 15:12:01
【问题描述】:
我有
1.) 文本区域
2.) 选择框
3.) 按钮
我在 textarea 中传递 JSON 输入,单击按钮 json 数据被传递到另一个 php 页面“sendingInterface.php”,json 数据从该页面通过函数传递到另一个名为“index.php”的页面,其中解码 json 和插入数据库发生。对此的响应来自另一个文本区域。
我的html代码:
<form id="data" action="" method="post" >
<h3>Request</h3>
<textarea name="jsondata" id="jsondata" value=""> </textarea>
<br>
<select name="listoptions" id="listoptions">
<option selected="selected" value="">Select method</option>
<option value="sendIncidentReport">sendIncidentReport</option>
<option value="sendDeathReport">sendDeathReport</option>
</select>
<br>
<input type="button" name="send_report" id="send_report" value="Send Report">
<br>
<h3>Response</h3>
<br>
<textarea name="response" id="response" rows="8" cols="8" value=""> </textarea>
<script>
var url="http://localhost/API/sendingInterface.php?fn="
$(document).ready(function() {
$("#send_report").click(function () {
$.ajax({
url:url + "sendIncidentReport", // calling url
type: 'GET',
data:{jsondata:$("#jsondata").val()
},
cache: false,
async: false,
success: function(data) {
$('#response').val(data);
},
error: function() {
alert('Error');
}
});
});
});
$(document).ready(function() {
$("#send_report").click(function () {
$.ajax({
url:url + "sendDeathReport", // calling url
type: 'GET',
data:{jsondata:$("#jsondata").val()
},
cache: false,
async: false,
success: function(data) {
$('#response').val(data);
},
error: function() {
alert('Error');
}
});
});
});
</script>
PHP代码:sendingInterface.php
<?php
include_once 'index.php';
$obj=new send();
/*sendIncidentReport*/
if($_GET['fn'] =="sendIncidentReport")
{
$json=file_get_contents("php://input");
$obj->sendIncidentReport($json);
//$jsondata=$_GET['jsondata'];
//$obj->sendIncidentReport($jsondata);
}
/*sendDeathReport*/
if($_GET['fn'] =="sendDeathReport")
{
$json=file_get_contents("php://input");
$obj->sendDeathReport($json);
//$jsondata=$_GET['jsondata'];
//$obj->sendDeathReport($jsondata);
}
?>
我注释的 PHP 代码运行正常。但我想使用 file_get_contents("php://input") 来获取 json 代码。
【问题讨论】: