Javascript 以html格式读取文本框中输入的值,并通过在php页面中检索来使用它 example.php
我无法在php中检索用户输入的值,然后使用它操作url,从而从API服务器获取数据。当我运行HTML页面时,它只会提醒我错误。我做错了什么?是ajax调用吗?应该是post方法吗?我尝试使用AJAX插件中的ajaxForm,但没有改变。任何帮助都将不胜感激。谢谢。您需要使用以下数据在ajax请求中传递trainno值:Javascript 以html格式读取文本框中输入的值,并通过在php页面中检索来使用它 example.php,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我无法在php中检索用户输入的值,然后使用它操作url,从而从API服务器获取数据。当我运行HTML页面时,它只会提醒我错误。我做错了什么?是ajax调用吗?应该是post方法吗?我尝试使用AJAX插件中的ajaxForm,但没有改变。任何帮助都将不胜感激。谢谢。您需要使用以下数据在ajax请求中传递trainno值: <?php echo "Value entered is: " .$_GET['train ']; $ch = curl_init("http:
<?php
echo "Value entered is: " .$_GET['train '];
$ch = curl_init("http://api.erail.in/route/?key=3d970b43-550b-4568-9227-492697f47093&trainno=12138");
$fp = fopen("example_homepage.txt", "w");
//$ch is a cURL handle returned by curl_init
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//Execute $ch
$result = curl_exec($ch);
//Read the file contents
$json_data = file_get_contents('example_homepage.txt');
echo $json_data;
curl_close($ch);
fclose($fp);
?>
HTML
example.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<h2>Train Information from Indian Railways API</h2>
<div style="float:left;">Your train name is: <span style="font-weight: bold" id="holder-name"></span></div>
<br><br>
Please enter train number:<br><br>
<form id="myform" name="myform"></form>
<input type="text" name="train" id="trainno">
<br><br>
<input type="submit" name="save" id="save" value="Save">
</form>
<script>
$('#save').on('click', function (event) {
event.preventDefault();
$.ajax({
type: "GET",
url: "example.php",
data: {train: $('#trainno').val()},
success: function (data) {
//alert(data.result.name);
$('#holder-name').html(data.result.name);
// YOU CAN DO WHATEVER YOU WANT WITH THE DATA
},
error: function (x, e) {
alert("Error");
}
});
});
</script>
</body>
</html>
请发布example.php代码在html/js代码中一切看起来都很棒。提供example.php代码,以便我们可以帮助您提供example.php代码的代码片段。如果您提交表单,它将刷新页面。请尝试我的答案Hi Lea,你的解决方案有效,谢谢!但我只是想知道,我的example.php在我发布的问题中是否可见。我确实用一个新标题发布了它。另外,若我希望我的输入字段是一个表单,并且ajax在表单提交时被执行,那个该怎么办呢。在解决方案中删除表单标记的原因是什么?另外,需要在php文件中设置头。我确实在ajax调用中解析了返回的json,但在添加了该表单之后,一切都变得一团糟。有什么建议吗?我删除了表单标签,这样我就阻止了表单提交。相反,我添加了一个侦听器,当您单击按钮时将触发该侦听器。这将设置一个ajax调用,页面将不会刷新。当我使用json时,我总是设置标题,因为我很久以前就遇到了一个问题,我的代码将其视为纯文本Hello Lea,不能阻止默认设置吗?但我不知道该怎么做,我想你是对的。我将用PrevendFault和标签编辑我的答案
$.ajax({
method: 'GET',
url: 'example.php',
data: { train: $('#trainno').val() },
success: function(Result){
var myObj = $.parseJSON(Result);
console.log(myObj.result);
},
error: function(data){
alert('ERROR');
}
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<h2>Train Information from Indian Railways API</h2>
<div style="float:left;">Your train name is: <span style="font-weight: bold" id="holder-name"></span></div>
<br><br>
Please enter train number:<br><br>
<form id="myform" name="myform"></form>
<input type="text" name="train" id="trainno">
<br><br>
<input type="submit" name="save" id="save" value="Save">
</form>
<script>
$('#save').on('click', function (event) {
event.preventDefault();
$.ajax({
type: "GET",
url: "example.php",
data: {train: $('#trainno').val()},
success: function (data) {
//alert(data.result.name);
$('#holder-name').html(data.result.name);
// YOU CAN DO WHATEVER YOU WANT WITH THE DATA
},
error: function (x, e) {
alert("Error");
}
});
});
</script>
</body>
</html>
<?php
if(isset($_REQUEST['train'])){
$url = 'http://api.erail.in/route/?key=3d970b43-550b-4568-9227-492697f47093&trainno=' . $_REQUEST['train'];
$data = file_get_contents($url);
header('Content-Type: application/json');
echo $data;
} else {
echo json_encode(array("error" => "no data"));
}