Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass string variable from java to php function

Tags:

java

android

php

I am a beginner java and php I must pass a string variable from Java client to php server using WebView

Is what I am doing right?

In java side:

String Coords=CoordsString;
String PHPPagePath="http://10.0.2.2/ReceiveLocation.php?Coords=" + CoordsString"; 

and on the php side: ReceiveLocation.php

 <?php
include('ConnectionFunctions.php');
Connection(); 
$x=$_GET['Coords'];
GetCoords($x);
function GetCoords($x)
{
   echo $x;
}   
?>

Is this the right way to pass the parameter Coords from Java Client to PHP Server Function?

like image 784
user1494142 Avatar asked Sep 18 '12 10:09

user1494142


Video Answer


2 Answers

To pass a parameter to PHP using the URL of the page, just append ?key=value to the URL, where key is the parameter key (i.e. the name) and value is its value (i.e. the data you are trying to transfer), and then in the PHP script you can retrieve the parameter like this:

<?php
    echo 'I have received this parameter: '.$_GET['key'];
?>

replacing 'key' with the actual name of the parameter.

This is the way PHP reads HTTP GET variables. See this for more information: http://www.php.net/manual/en/reserved.variables.get.php

Please, be careful when accepting variables from outside: they have to be "sanitized", expecially if they are going to be used in database queries or printed in the HTML page.

like image 158
gd1 Avatar answered Oct 16 '22 13:10

gd1


Modify your code as following

Java code:

String Coords=CoordsString;
String PHPPagePath="http://10.0.2.2/ReceiveLocation.php?Coords=10";

PHP code:

<?php
include('ConnectionFunctions.php');
Connection(); 

function GetCoords(x)
{
echo 'Coords = '.$_GET['Coords'];

}   
?>
like image 29
Garbage Avatar answered Oct 16 '22 14:10

Garbage