SanguineEpitaph
SanguineEpitaph

Reputation: 179

Using a PHP variable passed via AJAX

I have been searching around for an answer but none are really what I need.

Everything is working fine, the variable is being passed to my PHP file via AJAX, but now I need to know how to actually use it (for the WHERE field of a mysql query).

First, the user clicks something and it calls this AJAX:

$.ajax({ 
type: 'post', 
url: 'includes/hash.php', 
data: { 
    d: $(s).attr('id') 
},
success: function(data){ 
    console.log(data);
}
});

Then hash.php defines the PHP variable from the AJAX post:

<?php 
if(!empty($_POST['d'])){ 
    $hash = $_POST['d']; 
} ?> 

And I want it to be displayed in the following HTML in home.php:

<section id="pa"> 
    <article> 
        <h1 class="pa">PAINTINGS</h1> 
        <?php echo $hash; ?> 
    </article> 
</section>

Updated: Sorry for being unclear, I added the relevant HTML. The user clicks a link, and I eventually want to generate the WHERE field of a query based on the ID of the link they click. So I'm using AJAX to pass the variable (the ID) to the PHP file hash.php. Right now, it's not working in the MySQL query, so I am just trying to echo the variable for debugging purposes, but I get an error from PHP that it's undefined. I don't know how to use the variable outside of hash.php once it's defined via AJAX. Can I only use the variable in hash.php, or can I access it somehow in another file? I tried to include hash.php into the HTML, but it's still undefined despite the console log telling me that it IS being passed there.

Upvotes: 0

Views: 240

Answers (1)

user30899
user30899

Reputation: 27

I bet its your $(s) thats causing the problem.

Make sure that you refer to $(s) correctly better yet if you post your html code so we all can see what $(s) refers to.

Upvotes: 1

Related Questions