dongzhan7909 2014-02-18 02:31
浏览 34

使用通过AJAX传递的PHP变量

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.

  • 写回答

1条回答 默认 最新

  • dongqiao1158 2014-02-18 03:01
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改