how to get the form value into outside the form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

how to get the form value into outside the form?

Post by sathya »

here is my php code. i tried to make the train name/train number auto suggest.for that i have to get the input from trainnumber field.

i have to pass that train number to the api url and get the response.based on the response i will include in jquery.

kindly help me how to pass the formvalue into outisde the form within the page..


for example suppose if i type palani in the train number field it should the pass value as http://api.railwayapi.com/suggest_train ... /---->here my text will go whatever i typed.

else any smarter method there kindly suggest me.

Code: Select all

<!DOCTYPE html>
<html lang="en">
  <head>
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">

<title>Live Train Running Status|Indian Railways Live Train Status|spot your train</title>
  <script src="path/to/jquery.easy-autocomplete.min.js"></script> 
  <script>
var options = {
	url: $url,

	getValue: "train",

	list: {
		match: {
			enabled: true
		}
	}
};
$("#trainnumber").easyAutocomplete(options);
<?php
  $json = '';
  if(isset($_GET['trainnumber'])) { 
    $url = 'http://api.railwayapi.com/suggest_train/trains/'.$_GET['trainnumber'].'/apikey/*/';
?>
</script>
  <head>
<body>
<center><h2><b>Live Train Running Status</b></h2></center>
<ul>
  <li><a class="active" href="#home">Home</a></li>
  <li><a href="#news">Live Train Status</a></li>
  <ul style="float:right;list-style-type:none;">
    <li><a href="#about">Blog</a></li>
    <li><a href="#login">Contact</a></li>
  </ul>
</ul>

<h3>live train running status provide the information about current train running status,so that you can easily spot your train.you also get the details about train departure ,arrivals and late time in every station here.</h3>
<center>
<div id="page-wrap"><form action="livetrainstatus.php" method="GET">
<tr>
<td>    Train Number</td></br>
<td><input type="text" name ="trainnumber" size="20"> </td> 
</tr></br></br>
<tr>
<td> Select Date</td></br>
<td><select name="doj">
  <option value="<?php echo date('Ymd', strtotime("today")); ?>">Today</option>
<option value="<?php echo date('Ymd', strtotime("tomorrow")); ?>">Tomorrow</option>
<option value="<?php echo date('Ymd', strtotime("tomorrow")); ?>">Yesterday</option>
</select></td>
</tr></br></br>
<tr>
<td><input type="submit" value ="submit" size="20">
</td></br>
</form></br></br>
</div>
</center>
  </body>
<footer>
<?php include_once("analyticstracking.php") ?>
<script type="text/javascript" src="usersontxt.php?uvon=showon"></script></footer>
</html>
Last edited by requinix on Wed Feb 03, 2016 11:34 pm, edited 1 time in total.
Reason: removing api key by request
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to get the form value into outside the form?

Post by Christopher »

What does not work? You do not have a closing } in you if().
(#10850)
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

okay thanks for your reply.

whats my problem is,

1. i have train number text box field inside my page form
2.i want to pass the train number form field value into the api url like www.api.raliayapi.com/train number--->i am not getting idea how to pass the train number field into the api url.
3.when i pass the value in the api url like www.api.raliayapi.com/22651 it will produce the json response.
4.i want to display the json response in my train number text box field like auto suggest.

This is what i tried to convey you.

I have searched lot and no place provided the logic to get the autosuggest queries using json response directly.

if some one provided the logic it will helpful for me lot.i am not tried any code for this but if i get the logic idea i will learn and i can code.

thanks
sathya
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

got one good website for this tutorial.i recommend to people this link.

http://easyautocomplete.com/guide here the remote method will be the good example .
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to get the form value into outside the form?

Post by Christopher »

You need to use an Ajax call for auto complete. The PHP script that is called can use curl or file_get_contents() to call raliayapi.com and fetch the JSON, then send it as the response back to the Ajax call.
(#10850)
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

Thanks
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

I tried the below http://easyautocomplete.com/guide website and changes my code based on that.but still i cant get the expected output (auto complete suggest display train name and train number )

My code:

Code: Select all

<!DOCTYPE html>
<html lang="en">
  <head>
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">

<title>Live Train Running Status|Indian Railways Live Train Status|spot your train</title>

<link href="../dist/easy-autocomplete.min.css" rel="stylesheet" type="text/css">
		<script src="../lib/jquery-1.11.2.min.js"></script>
		<script src="../dist/jquery.easy-autocomplete.min.js" type="text/javascript" ></script>
  <head>
<body>
<center><h2><b>Live Train Running Status</b></h2></center>
<ul>
  <li><a class="active" href="#home">Home</a></li>
  <li><a href="#news">Live Train Status</a></li>
  <ul style="float:right;list-style-type:none;">
    <li><a href="#about">Blog</a></li>
    <li><a href="#login">Contact</a></li>
  </ul>
</ul>
<center>
<div id="page-wrap"><form action="livetrainstatus.php" method="GET">
<tr>
<td>    Train Number</td></br>
<td><input type="text" name ="trainnumber" size="20" id="trainnumber"/> </td> // here the id=trainnumber used to get the user input
<script>
		var options = {
			url: function(phrase) {
				
				return "http://api.railwayapi.com/suggest_train/trains/" + phrase + "/apikey/*/"; //i tried to pass the query typed by used into the remote url like this example one ..http://api.railwayapi.com/suggest_train/trains/pal/apikey/*/
			},
			getValue: "train",  // when we pass the phrase into the url i will get the json response and all the train name and train no will come under the train like this .
/*{
    "total": 8,
    "response_code": 200,
    "train": [
        "PALLAVAN EXP (12605)",
        "PALLAVAN EXP (12606)",
        "PALNADU EXP (12747)",
        "PALNADU EXP (12748)",
        "PALAMOU EXPRESS (13347)",
        "PALAMOU EXP (13348)",
        "PALANI EXPRESS (22651)",
        "PALANI MAS EXP (22652)"
    ]
}
*/
		};
		$("#trainnumber").easyAutocomplete(options);
		</script>
		
</tr></br></br>
<tr>
<td> Select Date</td></br>
<td><select name="doj">
  <option value="<?php echo date('Ymd', strtotime("today")); ?>">Today</option>
<option value="<?php echo date('Ymd', strtotime("tomorrow")); ?>">Tomorrow</option>
<option value="<?php echo date('Ymd', strtotime("tomorrow")); ?>">Yesterday</option>
</select></td>
</tr></br></br>
<tr>
<td><input type="submit" value ="submit" size="20">
</td></br>
</form></br></br>
</div>
</center>
  </body>
</html>
I have uploaded the below files in my hosting with the correct order.
<link href="../dist/easy-autocomplete.min.css" rel="stylesheet" type="text/css">
<script src="../lib/jquery-1.11.2.min.js"></script>
<script src="../dist/jquery.easy-autocomplete.min.js" type="text/javascript" ></script>
but still i cant get the expected output.

kindly help me to get the issue and what i did wrong here.
my url is livetrainrunningstatus.co.in

Thanks for reading this.
Last edited by requinix on Wed Feb 03, 2016 11:38 pm, edited 1 time in total.
Reason: removing api key by request
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to get the form value into outside the form?

Post by Christopher »

You can't directly call api.railwayapi.com via Ajax. You need a PHP script on your domain to proxy the calls to api.railwayapi.com and the return the results to your Javascript.
(#10850)
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

But you said php will run in server only,how I will make the call and pass to the JavaScript
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to get the form value into outside the form?

Post by Celauran »

echo

JS makes an AJAX request to your server. Server makes a cURL request to the remote API. Server receives a response, parses it, echoes back to client. JS receives echoed response, parses, displays.
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

Thank you so much
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to get the form value into outside the form?

Post by Celauran »

Note that this is likely to be exceedingly slow.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to get the form value into outside the form?

Post by Christopher »

Yes, that is what to proxy means.

I don't know how often these train schedules are published, but it might make more sense to have a cron job fetch all the latest schedules every 5 minutes or so. Then the Ajax call could query that data.
(#10850)
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: how to get the form value into outside the form?

Post by sathya »

Actually I tried to fetch the train name and train number,it will get instantly ,they won't update this details.
Post Reply