1

I need to call a javascript function and use the results(Json) as variable for PHP. I have the following function:

<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script>    
    <script>        
        $(function () {
            
            var baseUrl = 'https://XXXXXXXXX/';
            var uid = 'XXXX';
            var pwd = 'XXXX';
                        
            
            GetProd('DT.X0CEB.009', '1');

           function GetProd(cod_prod, qtd) {
                jQuery.support.cors = true;
                $.ajax({
                    url: baseUrl + 'api/Prod?ref=' + encodeURIComponent(cod_prod) + '&qtd=' + qtd,
                    type: 'GET',
                    jsonp: "callback",
                    dataType: 'jsonp',
                    username: uid,
                    password: pwd,
                    success: function (data, textStatus, xhr) {
                        document.write(data);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert(xhr.responseText);
                   }
            });
        }
    });            
    </script>

This returns this:

Object { Ref: "DT.X0CEB.009", Desc: "Monitor UltraHD P2715Q 68.6cm 27"", State: 0, Price: Object, Tax: Object, Stock: 2, Availability: null }

Can anybody help me how to use this fields (ref, desc, state, ...)in php? Regards Pedro

bodyfarmer
  • 402
  • 1
  • 7
  • 17
Pedro
  • 21
  • 3
  • Unless you have a JavaScript engine **on the server** that you can call from PHP (which is possible, but complicated and probably not what you're really trying to do), you can't do this. Let's think through the steps: 1. User requests a page. 2. The server gets the request and runs the PHP, generating an HTML file to return. 3. That file goes back to the client. 4. The client runs any JavaScript code on the page. – T.J. Crowder Apr 22 '15 at 15:15
  • possible duplicate of [How to call a JavaScript function from PHP?](http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – ChrisGPT was on strike Apr 22 '15 at 15:16

1 Answers1

1

You are sending them as GET params therefore they should be available in your endpoint via $_GET. You could also try sending a data object via POST and retrieve them with $_POST in your php API.

$.ajax({
  url: 'yourUrl',
  method: 'POST',
  data: {
    param1: param1,
    param2: param2
  },
  success: function(data) {
    //do something with data
  }
});

PHP

$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
Joe Fitter
  • 1,309
  • 7
  • 11
  • Thank you for the awswer but I didn't get it, in the function(data) i have document.write(data);. To get the variables i should use $data=$_GET['data']; in PHP? – Pedro Apr 22 '15 at 15:59
  • no, PHP is processed on the server, you will need to post back to PHP again if you want to process the results of the call. I would send another ajax request on success and include the results of the first call. – Joe Fitter Apr 22 '15 at 16:03