0

Can someone please help me, I want just to pass variable from java to php. Some jquery code :

$('#').keyup(function() {
        $.ajax({
            url: url,
            type: "get",
            data: some_data_to_send_to_url,
            success: function(data){
             var javaScriptVariable = data;                
            }
        });

      });

And I just want in the same php file do this :

$phpVariable = javaScriptVariable;

And then do some operations in php.

Thanks in advance :)

EDIT :

Thank, but I don't have problem with data that is sent to url: "data.php", This file receive some data, do some operation and return new data. And I have problem with this new data “success: function(data)” that comes from this file, and I want to assign this data to normal php variable.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
arsen99
  • 11
  • 4
  • 3
    Didn't you just get `data` from the server? Why do you want to send it back? – Felix Kling Mar 22 '13 at 09:48
  • Well, did you try it, and did it work ? – adeneo Mar 22 '13 at 09:48
  • `$phpVariable = javaScriptVariable;` - You can't do this because the JS variable exists in the browser and the PHP variable exists on the webserver. But you're already using the `$.ajax()` method, which is one way of passing data from JS to PHP... (As an aside, Java and JavaScript are not the same thing.) – nnnnnn Mar 22 '13 at 09:48
  • you can use the answer of the following post : http://stackoverflow.com/q/15565203/1035257 – Code Lღver Mar 22 '13 at 09:49
  • Thank, but I don't have problem with data that is sent to url: "data.php", This file receive some data, do some operation and return new data. And I have problem with this new data “success: function(data)” that come from this file, and I want to assign this data to normal php variable. – arsen99 Mar 22 '13 at 10:18
  • As already said, PHP runs on the server side. When you receive the data, you are at the client side and there is no PHP. You could send it back to the server by doing another Ajax call, but I don't know whether this is what you want/need. – Felix Kling Mar 22 '13 at 10:37
  • Thanks for all help, I found solution. I just put “data” that comes from ajax in hidden input. Then when user do something, this data from ajax is used to make different things. – arsen99 Mar 29 '13 at 12:20

3 Answers3

0

jQuery doc states :

jqXHR.done(function(data, textStatus, jqXHR) {}); An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details.

Javascript

var some_data_to_send_to_url = {"yourDataKey" : "yourValue"};

('#yourID').keyup(function() {          //Your "#" is not a valid selector
    $.ajax({
        url: url,
        type: "get",
        data: some_data_to_send_to_url,

    }).done(function(data){             //Updated
         var javaScriptVariable = data;                
       });

 });

Php

$phpvariable = $_GET["yourDataKey"];
//Stuff
echo $yourReturn;       //This is what you sent == javascriptvariable

Anyway, as said previously, PHP is server side exclusively, and javascript / jQuery is client side exclusively. So you won't assign data to a PHP var on your jQuery's done().

Maen
  • 10,603
  • 3
  • 45
  • 71
0

refer this code to send data to php page as json object.

function send()
    {
    var ip=new Object();
    ip.session_id="312fdfwf1343r";
    var inputParam=JSON.stringify(ip);
    var module="module1";
     $.ajax({
                         type: "POST",
                     url: phpurl
                         data: {inputParam:inputParam,module :module},   
                     dataType: "json",
                         success: function(msg)
                         {
                                 }
              });                
    }
Dineshkumar
  • 351
  • 1
  • 8
0
$('#').keyup(function() {
       $.ajax({
         url:  "data.php", // php file where you want to send data
         type: "get",
         data: {"some_data_to_send_to_url" : "yourValue"}, // this data will be sent
         success: function(data){
           // do something on success
         }
      });
  });

Then in your "data.php" file

$data = $_GET["some_data_to_send_to_url"];

Lucky Soni
  • 6,811
  • 3
  • 38
  • 57