BuissonArdent Posted May 16, 2021 Share Posted May 16, 2021 Hello everyone, I have a problem with AJAX, and PHP variables ... I am using a script (script.php) which contains database writing. In the page where I am calling this script, I set the variables so that the script understands which page I am calling it from. For example, in index, I set $PAGE = "INDEX"; or in contat, $PAGE = "CONTACT". This allows script.php to know where to write to the database. (The $PAGE variable is equal to the column name) Apparently the AJAX call doesn't know any of the variables I set in the index. This looks like a separate request to script.php, however, how do you get AJAX to have access to these variables? This would prevent me from doing one script per page ... Understand that having index_script, contact_script, about_script, etc ... That quickly becomes embarassing in my folder .. There my index code. (I call it twice (include and ajax refresh) because i have two actions in script.php, on refresh, and on load) <?php $PAGE = "INDEX"; // MY VARIABLE (ONE FOR THE EXEMPLE, BUT REALLY 7 OR 8) include('script.php'); // INCLUDE FIRST, DO THE FIRST ACTION ?> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> <script type="text/javascript"> function refreshh() { $.ajax({ url: "script.php", ifModified:true, success: function(content){ $('#notif').html(content); // <div> to refresh } }); setTimeout(notif, 1000); // (1 min = 60000) } refreshh(); </script> <div id="notif"></div> If anyone have a solution for let ajax access to my variables 🙂 thank you Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/ Share on other sites More sharing options...
BuissonArdent Posted May 17, 2021 Author Share Posted May 17, 2021 (edited) J'ai entendu parler du paramètre AJAX "data", mais je ne trouve pas de réponse concrète pour "passer" mes variables Comment faire comprendre à AJAX qu'il à besoin de mes variables ??  Quote I heard about the AJAX "data" parameter, but I can't find a concrete answer to "pass" my variables How do I make AJAX understand that it needs my variables ?? Edited May 17, 2021 by Barand translation Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586583 Share on other sites More sharing options...
Barand Posted May 17, 2021 Share Posted May 17, 2021 (edited) PHP runs on the server, javascript runs on the client after the PHP has finish running and sent the page to the client. Store $PAGE in a hidden field in your html <input type="hidden" id="page-name" value="<?=$PAGE?>" > In your script you can then access it with var page = $("#page-name").val() then send it i your ajax request $.post( "script.php", {"page":page}, // data function(content) { $("#notif").html(content) }, "TEXT" ) In script.php, access the page from $_POST['page] Edited May 17, 2021 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586592 Share on other sites More sharing options...
BuissonArdent Posted May 18, 2021 Author Share Posted May 18, 2021 Ohh thakn you for the answer, I try it later and give full code if works. Thanks for the translation ! I mistake it Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586627 Share on other sites More sharing options...
BuissonArdent Posted May 19, 2021 Author Share Posted May 19, 2021 Hello Barand, I have try your code solution, but that don't works ... Ajax works, like before posting my problem here. But the variable cannot be send ... Here my index and script, can you decel any problem in my typo ?  INDEX <?php $PAGE ="its works"; ?> <input type="hidden" name="page" id="page" value="<?php echo $PAGE; ?>" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> <script type="text/javascript"> var page = $("#page").val() function refreshh() { $.ajax({ url: "script.php", data: {"page":page}, ifModified:true, success: function(content){ $('#notif').html(content); // <div> to refresh } }); setTimeout(refreshh, 1000); // (1 min = 60000) } refreshh(); </script> <div id="notif"></div> SCRIPT <?php if (isset($_POST['page'])) { $variable = $_POST['page']; } else { $variable = "doesn't work !"; } echo "<br />echo Svariable : " . $variable . "<br /><hr /><br/>" . "echo S_POST page : " . $_POST['page']; ?> I have also tryed without the "data:" but it return me an error .. Thanks for your time 🙂 Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586640 Share on other sites More sharing options...
Barand Posted May 19, 2021 Share Posted May 19, 2021 (edited) One problem I can see is that you aren't telling your ajax request to use POST so it is probably defaulting to GET Does it work if you change $_POST in script.php to $_GET? Edited May 19, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586643 Share on other sites More sharing options...
BuissonArdent Posted May 19, 2021 Author Share Posted May 19, 2021 (edited) Ohh ! It's works ! Thank you for that 🙂 I'm telling me "why ?", because the url don't have any parameters in (like ?page=value) Do you know if I can use POST method with my script ? How tell to AJAX to use POST method with my refreshing script ?( I am afraid / and not wanting someone to modify the parameter in the url (even if post can be modified too))  I have try $.POST before (and in too) $.AJAX but it not working for me ... Ty Edited May 19, 2021 by BuissonArdent Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586651 Share on other sites More sharing options...
Barand Posted May 19, 2021 Share Posted May 19, 2021 either method: "POST", or $.post (instead of $.ajax or $.get) Â Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586653 Share on other sites More sharing options...
BuissonArdent Posted May 19, 2021 Author Share Posted May 19, 2021 Thank you very much for your time Barand ! really. After several tries, I still can't get it to "POST", with the parameter method: "POST", Or change $.ajax to $.post The problem is that doing this "deactivates" the script and returns a blank page ! However, for those interested, after several tries, modifying the url and adding the parameter ?page=test does not modify the values of my variables ! In any case, thank you very much. Because now I have learned to pass variables in GET with ajax Best regards. BuissonArdent. Quote Link to comment https://forums.phpfreaks.com/topic/312714-ajax-cant-access-to-php-variables/#findComment-1586668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.