0

From jquery file to php:

$.post('http://botk-online.com/play2.php', {
 'rate': Pontuacao.misses
});

How to modyficate this code to pass $data variable from php file to js external file and read it value.

3
  • I don't understand - which JS external file do you mean? Commented Dec 17, 2011 at 17:34
  • Are you wanting some Javascript in an external file loaded on the calling page to interact with the $data returned from your play2.php script? Commented Dec 17, 2011 at 17:36
  • You want to use the success handler: api.jquery.com/jQuery.post/#example-5 Commented Dec 17, 2011 at 17:38

2 Answers 2

1

PHP

<?php

  $result = array('data' => 'this is some data');
  echo json_encode($result);

Javascript:

$.ajax({
    url: 'http://botk-online.com/play2.php',
    type: 'post',
    data: {
        rate: Pontuacao.misses
    },
    dataType: 'json',
    success: function(json) {
        alert('Data is: '+json['data']);
        // Alerts: 'Data is: this is some data'
    }
});
Sign up to request clarification or add additional context in comments.

13 Comments

@KamilKrzysztofDworak try adding the error function as I have in the link above...
@Dave, it works for me if i want to pass rate variable from js to php but i can't send $value from php to external js file.
How do you mean send $value from php to external js file - is the file already loaded in the browser, or do you need to load the other file into the browser, or call a function defined in another file, or something else?
File play2.php is open and I have to send $value to file.js for example by form
How do you mean send it to the file? Where is file.js?
|
0

Use the success handler, which will run if the code returns successfully:

jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

Note the , success(data, textStatus, jqXHR) above. For example:

$.post('http://botk-online.com/play2.php', {
        'rate': Pontuacao.misses
    }, function (data) {
        doStuff(data);
    }
});

http://api.jquery.com/jQuery.post/#example-5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.