-4

i saw many solutions which are working for me too, but in this particular situation nothing works, i'll share the code and hope one can help me,

<head><script src="jquery-1.9.1.min.js"type="text/javascript"></script>
<script>
$(document).ready(function(){
var gg = 1;
$.post('11.php', {gg:gg});});</script>
<?php
include '11.php';
echo "</br>this is data</br>". $data;?>
</body>

and 11.php:

<?php  $data = $_POST['gg'];
echo "this is data from 11.php" .$data; ?>

i've tried before to solve this kind of problem and it worked well on localhost, but now on real hosting something goes wrong, i know it's duplicating, but can't find my mistake here, thank you in advance

Additionally: stackoverflow engine says: "This question may already have an answer here: Reference: Why does the PHP code in my Javascript not work? 2 answers" i can't see any relation with my question, but down votes are rising

bla2eOD
  • 77
  • 6
  • Are you on cross domains ? – TCHdvlp Jun 07 '13 at 10:10
  • @TCHdvlp no cross domains – bla2eOD Jun 07 '13 at 10:11
  • "This question may already have an answer here: Reference: Why does the PHP code in my Javascript not work?" i dont see anything close there to what i asked here, stop down voting – bla2eOD Jun 07 '13 at 10:18
  • @bla2eOD Read *and understand* what is written there. It does not address this particular piece of code, no, but you need to understand the background to see that what you're trying to do here is absolute nonsense. – deceze Jun 07 '13 at 10:27
  • i'm trying to solve problem in the way i can, no doubt that the answer there is what i should know to avoid falling into problems like this one, but if i can't read and understand your answer there, that means that i can't, so i need different description and i was asking for it and i got it, that is the way system works imo – bla2eOD Jun 07 '13 at 10:36

2 Answers2

1

You need to create a callback function on success you can't just include 11.php because that would not be in the ajax request therefore gg will not be passed.

$.post('11.php', {gg:gg},function(data){
    $('body').append(data);
    });
});
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • yes, now i see it's response, but how i should use it in php? – bla2eOD Jun 07 '13 at 10:16
  • All you can do in php with the information from the ajax request is within the 11.php file, when the request is returned it's only client side you can manipulate. – php_nub_qq Jun 07 '13 at 10:17
  • look, i need that integer, to make a request using php, so idea is to take that gg to php and then do something like this: $phpVar = $response->response[gg]->url; – bla2eOD Jun 07 '13 at 10:20
  • You need to do that in the 11.php file, where you set 'gg' equal to $data. Therefore your call should look something like `$phpVar = $response->response[$data]->url`. You don't need to include 11.php in your main file because it will not be passed any data it will just be included. – php_nub_qq Jun 07 '13 at 10:22
  • ok, it is much more better now, thank you for that. As i guess whole magic is in `$('body').append(data);` , what exactly it does? – bla2eOD Jun 07 '13 at 10:27
  • It appends the returned data from executing your 11.php script to the body element of your document. You can change that to whatever element you need I just used body for simplicity. You can have a look at the [append method](http://api.jquery.com/append/). And you can also `alert(data)` to see what is in the data parameter that is returned by the request. Happy coding :P – php_nub_qq Jun 07 '13 at 10:30
  • i've an idea, what if i send this request to '1.php'? Can i then use it directly in that php file? – bla2eOD Jun 07 '13 at 10:45
  • Since I have no idea what your scripts are doing I can't really tell what you're trying to do, but whatever you pass as data in your ajax call it will be sent to the file you're calling and then within that file you can do whatever it is you want to do with that data. Here's a diagram that explains how Ajax works pretty simply [click](http://courses.ischool.berkeley.edu/i153/s11/img/ajax-diagram.png) – php_nub_qq Jun 07 '13 at 10:47
0

You said that it is in hosting server then you need to change

$.post('11.php', {gg:gg} , function(data){
       alert(data);   //console.log
});

11.php file full path at the host should be given for the post..

GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • i've tried it too, but nothing changes – bla2eOD Jun 07 '13 at 10:12
  • have you alerted the result data..??check with my edit – GautamD31 Jun 07 '13 at 10:13
  • i've checked, it is ok now, but how i'm supposed to use it in php? – bla2eOD Jun 07 '13 at 10:14
  • Editing your answer, when seeing others' answers, to the same thing is even more embarrassing than giving a wrong answer at first – php_nub_qq Jun 07 '13 at 10:14
  • Look @php_nub_qq i dont need to copy your ans...I jst gave him a ans and after that I have modified it....Main thing you answered after 2min late than me...So whats yours..??copied or own...?am I ask like that..?? – GautamD31 Jun 07 '13 at 10:16
  • You edited your answer after I answered, you're just getting ridiculous and I wont be paying any more attention to you. Good day – php_nub_qq Jun 07 '13 at 10:19
  • Me too...You also answered after me...please keep in mind that...iam not getting ridiculous....why should I..?? – GautamD31 Jun 07 '13 at 10:20