1

I need to pass my PHP variables defined (earlier in my page) in my Ajax call.

And I have a lot of variables defined so I cannot do it var by var in my code.

My first question is, is there a way in PHP to loop on every defined variable ? like get my $var1, $var2

I think I could do it earlier in my PHP page between <script> tags but this is really not clean.

My AJAX call is like so:

function add_content_in_divs(class_name, target_div) {

   var page_name = 'displayer/' + class_name + '_informations.php'

   $.ajax({
      url:page_name,
      type: 'GET',
      data: {//need to pass my PHP vars here},

         success: function (resp){
            target_div.html(resp);
         }

   })
}

My final goal is to include a part of PHP code in my div, knowing that this include contains $vars defined in my main PHP page. So my includes parts of code doesn't recognize my PHP vars in.

There is an example of code part I have to include.

if (isset($detail)) {

    echo '<div class="row"><span class="font-weight-bold">City detail: </span>' . $detail . '</div>';

} 
Kiji_T
  • 104
  • 10
  • `is there a way in PHP to loop on every defined variable ? like get my $var1, $var2`...no. Use an array, instead of storing everything in separate variables. Arrays are designed to be looped over. – ADyson May 10 '21 at 08:49
  • 1
    Or just list them in the `data`. It depends on what exactly you need to send and in what structure, but maybe just something like `data: { "var1": "", "var2": "" }` – ADyson May 10 '21 at 08:50
  • Yes I mean looping in my PHP page to store my vars in an array. And I litteraly need `$var_name` like this, that is defined in my PHP page. Can I recover it with `` in my jQuery script? – Kiji_T May 10 '21 at 08:52
  • `Can I recover it with ` ...yes, as long as that JavaScript is within a `.php` file which gets executed by the PHP interpreter – ADyson May 10 '21 at 08:54
  • Oh I see. But I don't want to do it like so, this is not clean and my page is already very consequent in code. – Kiji_T May 10 '21 at 08:55
  • So somewhere else in your ` – ADyson May 10 '21 at 08:57
  • Would it work if I do like `
    >` and then recover it with jQuery `$(div_name).attr('value')` ?
    – Kiji_T May 10 '21 at 08:59
  • yeah that would be fine. Basically if you echo that value, then the finished page delivered to your browser will contain `
    ` (or whatever `$php_var` contained when the PHP was executed).
    – ADyson May 10 '21 at 09:00
  • (i really don't wanna use script tag in my php code unless for imports) – Kiji_T May 10 '21 at 09:01
  • OK so this is still not good. I might explained wrongly, but I need my `$var` to be assigned, so I can call it in my included PHP AJAX called page. – Kiji_T May 10 '21 at 09:03
  • Maybe I could pass a whole page containing the assigned $vars in Data attribute of AJAX call ? – Kiji_T May 10 '21 at 09:06
  • 1
    first of all, avoid the GET method and instead use POST. secondly, you could easily do this by having all your variables in a form, and serializing the form in you "data" field inside your ajax – Nikos Gkogkopoulos May 10 '21 at 09:17
  • `I need my $var to be assigned` if you need to assign it to a Javascript then you must copy it from PHP to Javascript using the method I showed. Or you can copy it into a HTML element as you showed, and then use Javascript to retrieve it from there. But at some point you have to echo it into the final page, so that it's available in the browser. – ADyson May 10 '21 at 09:21
  • `Maybe I could pass a whole page containing the assigned $vars in Data attribute of AJAX call ? `...I've no idea what this even means. Doesn't appear to make much sense. Why would you want to pass a whole HTML page? And how do you imagine the receiving PHP would be able to parse it? – ADyson May 10 '21 at 09:22
  • Yes I guess it wont work since php code is interpreted before. – Kiji_T May 10 '21 at 09:26
  • Did you understand that if `$var1 = 12` in my main.php, in my ajax_called.php I need to be able to use `echo $var1` to display `12` ? – Kiji_T May 10 '21 at 09:28
  • 1
    Yes I do. But since PHP variables don't persist between requests, you must pass the variable from main.php to the page, so that Javascript can read it when it's running in the browser, and then Javascript must pass it to informations.php so it can be used there when that request runs. – ADyson May 10 '21 at 09:29
  • Well, ok but doing it like so `data: { "var1": ""}` can't work (and don't work for me). – Kiji_T May 10 '21 at 09:34
  • `don't work for me`...means what exactly? That was just an example, I don't know what your informations.php script actually needs, or in what format. But if you _did_ send that, then informations.php could collect it as `$var1 = $_GET["var1"];`. – ADyson May 10 '21 at 09:40
  • I put this in my ajax data call `data: { "detailled" : '' }`. But I still cannot manage to get my value set. – Kiji_T May 10 '21 at 09:44
  • `cannot manage to get my value set`...which value? Did you check the page source to see if the PHP was output correctly? e.g. if `$detailled` contains "sjkhjkfgd" in main.php then, `data: { "detailled" : '' }` should turn into `data: { "detailled" : 'sjkhjkfgd' }` when the page is loaded into the browser. Then in the second php script (informations) you can collect that submitted value with something like `$detail = $_GET["detailled"];` - it's not clear if you're doing that or not. Try to be more precise when describing any problems, please. – ADyson May 10 '21 at 09:46
  • Oh I'm confused, I was trying to load it in data attribute of the Ajax call. I'll try it again by doing what you told me. Sorry for blurry explainations, I really thanks you for taking time with me. But this is even not clear in my head :P – Kiji_T May 10 '21 at 09:52
  • `I was trying to load it in data attribute of the Ajax call`...yes that's fine, that's what `data: ...` does inside $.ajax. Not clear why you think that bit is an issue. – ADyson May 10 '21 at 09:55
  • P.S. I wonder if you should read https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming for some background information and context. – ADyson May 10 '21 at 09:55
  • OK so I tried and I realise it don't work because I'm not submiting anything. I'm treating datas before and once I'm on main page, here I need my `$vals`. I tried like this `echo '';` on main page and `$.ajax({data: {"$detailled_weather" : "$_GET['detail']",},`. I guess it can't work because there is no submiting. Is there something I understood wrong ? – Kiji_T May 10 '21 at 12:05
  • Thanks for the link I knew it works like this but it was a bit confused for me, it's more clear now. – Kiji_T May 10 '21 at 12:06
  • So the real question is know : how to include php datas with javascript before server interprets it ? – Kiji_T May 10 '21 at 12:11
  • You seem to be getting even more confused. `$_GET['detail']` would be used in the PHP script (informations.php) which **receives** the data from the AJAX request, not in the Javascript which sends it. Sorry if my example was not clear. https://stackoverflow.com/questions/67467296/how-to-recover-and-pass-multiple-php-variables-in-my-ajax-call?noredirect=1#comment119250909_67467296 explained it, I thought? – ADyson May 10 '21 at 12:12
  • 1
    If you wanted the Javascript to get the detail value from the hidden field, you could write `$.ajax({data: {"$detailled_weather" : $("#defined_vars").val() }`. That would then send the contents of the "defined_vars" field to informations.php – ADyson May 10 '21 at 12:19
  • 1
    Ok, I finally got it ! This is almost good, but in the included part of code, `` is displayed as a comment. I have `"detailled_weather" : '',` in my datas and `
    . $_GET['detailled_weather'] . '
    ` in my informations.php.
    – Kiji_T May 10 '21 at 12:26
  • Good, glad you got the hang of it in the end – ADyson May 10 '21 at 12:57
  • I thanks you for the time you gave to explain me. I will finally adopt puting in values of hidden div and getting it before Ajax call. I have to re-write all my includes changing vars but atleast it works ! – Kiji_T May 10 '21 at 13:43

0 Answers0