Pass string variable from java to php function

16,075

Solution 1

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'];

}   
?>

Solution 2

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.

Solution 3

use $_GET['parameter_name'] in PHP if you send parameter using GET method

OR

use $_POST['parameter_name'] in PHP if you send parameter using POST method

and its alternate is also $_REQUEST if you send parameter using POST OR GET.

Share:
16,075
user1494142
Author by

user1494142

Updated on June 22, 2022

Comments

  • user1494142
    user1494142 almost 2 years

    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?