0

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.

4
  • 2
    It's difficult to understand your question / problem. Everything looks fine in your code; given a valid POST request with d parameter (as you are doing correctly via JavaScript), your includes/hash.php script should be able to use $_POST['d'] Commented Feb 18, 2014 at 2:34
  • is hash.php returning any value? echo $hash in hash.php and you should be able to see a string representation on the success: function (data){console.log(data)} Commented Feb 18, 2014 at 2:35
  • 1
    look in the network tab of your browser console for the parameters of your post request Commented Feb 18, 2014 at 2:37
  • I have updated the post. hash.php DOES return a value, but I want to use it in a separate file. How do I access the variable outside of hash.php? Thanks! Commented Feb 18, 2014 at 11:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

$(s) refers to one of the arguments of a function. It is working correctly, though.
please paste your html code here, we can help you:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.