dimanche 22 février 2015

Read the value entered in text box in html form and use it by retrieving it in a php page



<!DOCTYPE>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ift.tt/13qgtmt"></script>

</head>

<body>
<h2>Train Information from Indian Railways API</h2>

<form id="info">
Please enter train number:<br><br>
<input type="text" id="trainno" name="train">
<br><br>
<input type="submit" value="Submit">
</form>

<script>
$("#info").submit(function(){

$.ajax({
method: 'GET',
url: 'example.php',
success: function(Result){
var myObj = $.parseJSON(Result);
console.log(myObj.result);
},
error: function(data){
alert('ERROR');
}
});
});
</script>

</body>


</html>


example.php



<?php

echo "Value entered is: " .$_GET['train '];

$ch = curl_init("http://ift.tt/1D4RaE0");
$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);

?>


I am unable to retrieve the user entered value in php and then use it to manipulate the url there, which gets me the data from API server. When I run the HTML page, it just gives me an alert of ERROR. What am I doing wrong? Is it the ajax call? Should it be post method? I tried using ajaxForm from AJAX plugin but no change. Any help will be appreciated. Thank You.





Aucun commentaire:

Enregistrer un commentaire