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.

2 Answers2

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'
    }
});
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • @KamilKrzysztofDworak try adding the error function as I have in the link above... – DaveRandom Dec 17 '11 at 18:25
  • @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. – Kamil Krzysztof Dworak Dec 17 '11 at 18:36
  • 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? – DaveRandom Dec 17 '11 at 18:37
  • File play2.php is open and I have to send $value to file.js for example by form – Kamil Krzysztof Dworak Dec 17 '11 at 18:41
  • How do you mean send it to the file? Where is file.js? – DaveRandom Dec 17 '11 at 18:42
  • [dir1/file.php] near [dir2/jquery.js] is it important? – Kamil Krzysztof Dworak Dec 17 '11 at 18:44
  • What I mean is, do you need to *load* the file, and send `$value` in the request where you load it, or is it *already loaded* and you need to call some code that is in it? What is the end result that you want here? – DaveRandom Dec 17 '11 at 18:48
  • Ok,I have form in clear php file which can save my name for example. Now I have to pass name value to external js file and alert this value. – Kamil Krzysztof Dworak Dec 17 '11 at 18:52
  • It is `pass name value to external js file` that I don't understand - JS is client side, so what do you need to do? Call a function in a different JS file to the one where this code is? You just need to call the function name... – DaveRandom Dec 17 '11 at 19:11
  • I wonder to load music albums from sql(in php file) and load it by js file like that: var Songs = [ {album:'$result1', cover:'$result2', user:'$result3'} ]; – Kamil Krzysztof Dworak Dec 17 '11 at 19:17
  • Well, you just need to load the data from the database in the PHP script, and put it into the `$result` array so it will be converted. Then, in the `success` function, call your Javascript code that does whatever you need to do with it – DaveRandom Dec 17 '11 at 19:37
  • PHP: $data = mysql_query("SELECT * FROM music WHERE albumid=1"); $result = mysql_fetch_array($data); But what now with js file? – Kamil Krzysztof Dworak Dec 17 '11 at 19:44
  • `success: function (json) { $('#element_that_displays_result').html('Album: '+json['album']+'; Cover: '+json['cover']+'; User: '+json['user']); }` - and you should use [`mysql_fetch_assoc()`](http://www.php.net/manual/en/function.mysql-fetch-assoc.php), especially when you are putting the results into JSON – DaveRandom Dec 17 '11 at 19:49
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

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104