Jump to content

Ajax can't access to php variables ?


BuissonArdent

Recommended Posts

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

Link to comment
Share on other sites

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 by Barand
translation
Link to comment
Share on other sites

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 by Barand
  • Thanks 1
Link to comment
Share on other sites

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 🙂

Link to comment
Share on other sites

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 :D

Edited by BuissonArdent
Link to comment
Share on other sites

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 :D Best regards. BuissonArdent.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.