PrivateUser
PrivateUser

Reputation: 4524

php Getting value from standalone javascript

I'm using curl to get a remote page.. I have a local js file.. Its called main.js.

I want to execute that js file on that curl page.

My main.js file outputs the result as JSON string.

I would like to assign that string in a php variable. Can someone help me?

Upvotes: 0

Views: 417

Answers (2)

Jose Perales
Jose Perales

Reputation: 56

Try

1) In the main.js

`myobj = new Array();
 myobj["field1"] = field1; 
 myobj["field2"] = $("#field2").val();
 myobj["field3"] = $("#field3").val();
 myobj["field4"] = field4; `

field1 and field 4 if you have the value directly and field2 and field 3 if it comes from a html form. Now put it

`FormArray(myobj);
 var data = JSON.stringify($("#form").serializeObject());` 

Now you need to call the php function that is going to receive the data. Ye it can be confused so it is a great method.

`$.ajax({
        url:    'yourphpfile.php?,
        data:   {
            module: 'path',
            datos:  data, // Your data flying to php here
        },
        async:      false,
        type:       'POST',
        success:    function(html){

        }`

Now in yourphpfile.php that is going to receive the data put

 `$datos = json_decode($datos);
  $field1 = $datos->field1;
  $field2 = $datos->field2
  $field3 = $datos->field3
  $field4 = $datos->field4`

Hope it help you!!

Upvotes: 0

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

To execute JavaScript you need a JavaScript engine. You can do this in a headless browser like phantomjs,or you can try with v8 js extension for PHP

Upvotes: 4

Related Questions