在AJAX页面上包括PHP变量以进行SQL连接吗? [英] Include PHP variables on AJAX page for SQL connection?

查看:67
本文介绍了在AJAX页面上包括PHP变量以进行SQL连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主文件,该文件正在发出一些ajax请求以更新MySQL结果,但是该数据库正在根据子目录中的配置连接到更改.(/app/目录的index.php具有 include:'../main.php'以及相应的SQL连接)

将此用于我的ajax请求:

 < script>$(function(){setInterval(function(){$ .ajax({类型:"GET",快取:false,网址:<?php echo'../fetch.php?year=$year&month=$month&date=$date'";?> ;,成功:function(html){$(.list").html(html);}});},5000);});</script> 

fetch.php是我的SQL查询的副本

这只是返回一个sql错误,因为它没有继承它应该连接的数据库,因此它什么也没有连接.数据:{club:<?php echo $ club;?>}} ,然后将 $ club 分配给fetch.php文件上的相应数据库,但似乎破坏我的ajax请求(没有错误,它只是不轮询).

我如何使包含的页面使用依赖于其他地方声明的变量的ajax?

解决方案

嗯,我是个白痴.我在URL(& club = $ club )中添加了一个变量,然后根据 $ _ GET ['club'] 分配了ajax应该连接到的数据库

那么容易,但我想不起来

I have one main file that is making a few ajax requests to update MySQL results, however the database it is connecting to changes based on a config in a subdirectory. ( /app/directory's index.php has include: '../main.php' as well as the appropriate SQL connection)

Using this for my ajax request:

<script>   $(function() {
    setInterval(function() {
        $.ajax({
            type: "GET",
            cache: false,
            url: <?php echo "'../fetch.php?year=$year&month=$month&date=$date'";?>,

            success: function(html) {
                $(".list").html(html);
           }

        });
    }, 5000); });

</script>

fetch.php is a copy of my sql query

This just returns an sql error because it isnt carrying over the database it should be connecting to, so its connecting to nothing. data: {club:<?php echo $club;?>} and then assigning $club to the appropriate database on the fetch.php file, but it seems to be breaking my ajax request (no error, it just doesnt poll).

How do i make an included page that uses ajax dependent on variables declared elsewhere?

解决方案

Well i'm a moron. I added a variable in the URL (&club=$club) and then assign the database the ajax should connect to based on $_GET['club']

So easy yet i just couldnt think of it

这篇关于在AJAX页面上包括PHP变量以进行SQL连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆