使用下拉菜单、javascript 和 PHP 更改 div 的内容

发布于 2024-10-15 09:55:07 字数 2753 浏览 1 评论 0原文

我是个菜鸟,但如果你帮我找到答案,我会很乐意接受它;)

我在数组 $cat_array 中有一个类别列表。 我想构建一个选择下拉列表,以便当我选择其中一个选项时,会将不同的变量传递给函数。

这是我到目前为止所拥有的: 在主文件(index.php)中,我调用一个函数:

makechoice($cat_array);

这指的是包含文件中的这个函数:

function makechoice($cat_array) {   
$dbz = new db();
$sim = new simple();
echo '<div class="choose-section">';
    echo '<select class="selbox" onchange="categoryAjaxData(\'facebook\',\'/includes/fancount.php\',this.value);">';
        foreach($cat_array as $cat) {   
            echo('<option value="'.$cat[0].'">'.$cat[1].'</option>');   
        }
    echo '</select>';
    echo 'Choose a section';
    echo '</div>';
    echo '<div id="facebook"><div class="facebook-midcut">',showfans($djnames, $djids, $djurl),'</div><div class="l-botcut"></div></div>';
}

在includes/fancount.php中,我基本上有很多不同的变量想要通过showfans传递功能。

例如 $barnames、$barids、$barurl 和 $barurl $restaurantname、$restaurantids、$restauranturl

showfans 函数使用这些变量来显示有关它们的一些数据。

这是我的 javascript 代码:

function categoryAjaxData(div,str,value)
{
    var url = str+'?catid='+value;
    ajaxData(div,url);
}

function ajaxData(div,str)
    {
        xmlHttp=GetXmlHttpObject();
        document.getElementById(div).innerHTML='<center><img src="images/loader.gif"></center>';        
        if(xmlHttp==null)
        {
            alert("Browser does not support HTTP Request")
            return
        }
            var url = str;
            xmlHttp.onreadystatechange = function(){ DescriptionstateChanged(div); };
            xmlHttp.open("GET",url,true);
            xmlHttp.send(null);
    }

function DescriptionstateChanged(div)
{
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById(div).innerHTML=xmlHttp.responseText;
    }
} 

目前: 当前 facebook div 中的内容重新加载,我可以看到加载程序 - 但我一直不知道如何将选择下拉列表中选择的选项链接到更改 showfans() 函数中的变量。

例如 我希望在下拉列表中显示“酒吧”:

echo '<div id="facebook"><div class="facebook-midcut">',showfans($barnames, $barids, $barurl),'</div><div class="l-botcut"></div></div>';

我希望在下拉列表中显示“餐厅”:

echo '<div id="facebook"><div class="facebook-midcut">',showfans($restaurantnames, $restaurantids, $restauranturl),'</div><div class="l-botcut"></div></div>';

我希望有帮助!提前致谢:)

更新:

关于 fancount.php,我一直在寻找各种不同的选项 - 但我似乎无法让它们中的任何一个工作!

我想将下拉列表中的值传递到逻辑中,以便根据所选的变量,showfans() 使用某些变量,并且对于各种变量组合,输出代码看起来与此类似:

echo '',showfans($餐厅名称、$restaurantids、$restauranturl),'';

I'm a noob but will gladly take some stick for it if you help me find the answer ;)

I have a list of categories in an array $cat_array.
I want to build a select drop-down so that when I select one of the options, a different variable is passed to a function.

This is what I have so far:
In the main file (index.php), I call a function:

makechoice($cat_array);

This refers to this function which is in an included file:

function makechoice($cat_array) {   
$dbz = new db();
$sim = new simple();
echo '<div class="choose-section">';
    echo '<select class="selbox" onchange="categoryAjaxData(\'facebook\',\'/includes/fancount.php\',this.value);">';
        foreach($cat_array as $cat) {   
            echo('<option value="'.$cat[0].'">'.$cat[1].'</option>');   
        }
    echo '</select>';
    echo 'Choose a section';
    echo '</div>';
    echo '<div id="facebook"><div class="facebook-midcut">',showfans($djnames, $djids, $djurl),'</div><div class="l-botcut"></div></div>';
}

In includes/fancount.php, I have basically have lots of different variables that I want to pass through the showfans function.

e.g. $barnames, $barids, $barurl & $restaurantname, $restaurantids, $restauranturl

The showfans function uses these variables to display some data about them.

This is my javascript code:

function categoryAjaxData(div,str,value)
{
    var url = str+'?catid='+value;
    ajaxData(div,url);
}

function ajaxData(div,str)
    {
        xmlHttp=GetXmlHttpObject();
        document.getElementById(div).innerHTML='<center><img src="images/loader.gif"></center>';        
        if(xmlHttp==null)
        {
            alert("Browser does not support HTTP Request")
            return
        }
            var url = str;
            xmlHttp.onreadystatechange = function(){ DescriptionstateChanged(div); };
            xmlHttp.open("GET",url,true);
            xmlHttp.send(null);
    }

function DescriptionstateChanged(div)
{
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById(div).innerHTML=xmlHttp.responseText;
    }
} 

AT THE MOMENT:
The content in the current facebook div reloads, I can see the loader - but I'm stuck with how to link the option selected in the select dropdown to changing the variables in the showfans() function.

E.g.
I want "Bar" in the dropdown to show:

echo '<div id="facebook"><div class="facebook-midcut">',showfans($barnames, $barids, $barurl),'</div><div class="l-botcut"></div></div>';

I want "Restaurant" in the dropdown to show:

echo '<div id="facebook"><div class="facebook-midcut">',showfans($restaurantnames, $restaurantids, $restauranturl),'</div><div class="l-botcut"></div></div>';

I hope that helps! Thanks in advance :)

UPDATE:

With regards to fancount.php, I have been looking at various different options - but I can't seem to get any of them to work!

I want to pass the value from the dropdown into a logic so that depending on the varlue chosen, certain variables are used by showfans() and the output code looks similar to this for the various combination of variables:

echo '',showfans($restaurantnames, $restaurantids, $restauranturl),'';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-10-22 09:55:07

不!好的,别挡道 :-)

您正在将 'facebook-midcut' 传递给 categoryAjaxData() 函数。最终的 DOM 查找是否知道您指的是带有 id 的 div 还是带有该类的 div?如果您使用像 jquery 这样的库,则无需进一步分类,它就不会知道您指的是哪个节点。我猜您正在尝试获取该类,因此您无法使用 getElementById()

您可能还想在 'includes/fancount.php' 的开头添加斜杠。在其开头添加斜杠意味着路径将从站点的文档根目录引用,而不是从站点内的当前位置引用。

你使用某种js库吗?查看您的 ajaxData 函数会很有用,因为问题很可能就在那里。

给你一个快速提示。了解何时在 PHP 中使用单引号和双引号。它将使您的代码更简单、更快。 echo "

"; 可以写成 echo '

'; code>

我会回来查看您是否已使用我要求的信息更新了您的帖子,然后我可以为您提供更多帮助。

更新:

我假设函数 DescriptionstateChanged(div) 是 div 实际更新发生的地方,但您从未将 xmlHttp 对象传递给它。这是一个全局对象吗?如果不是,那么您肯定需要将其传递给 DescriptionstateChanged() 函数,并且我建议它无论如何都不应该是全局对象。如果 DescriptionstateChanged() 函数是实际更新 div 的地方,您也可以发布这个函数吗?

n00b! OK, stick out of the way :-)

You are passing 'facebook-midcut' to your categoryAjaxData() function. Does the eventual DOM lookup know whether you mean the div with the id or the div with that class? If you're using a libray like jquery it wouldn't know which node you mean without further classification. I would guess you're trying to get the one with the class so you can't use getElementById().

You may also want to add a slash to the start of 'includes/fancount.php'. Adding a slash to the start of this means the path will reference from your site's document root rather than from your current location within the site.

Are you using some kind of js library? It would be useful to see your ajaxData function as the problem could very well be there.

A quick tip for you. Learn when to use single and double quotes in PHP. It will make your code simpler and faster. echo "<div id=\"facebook-midcut\">"; could be written as echo '<div id="facebook-midcut">';

I'll check back to see if you've updated your post with the info I've asked for then I can give you more help.

UPDATE:

I assume the function DescriptionstateChanged(div) is where the actual updating of the div happens but you never pass it the xmlHttp object. Is this meant to be a global object? If not then you definitely need to pass it to the DescriptionstateChanged() function and I would recommend that it shouldn't be a global object anyway. If the DescriptionstateChanged() function is where the actual updating of the div takes place can you post this function too?

已下线请稍等 2024-10-22 09:55:07

您可能想要更改此设置:

echo '<select class="selbox" onchange="categoryAjaxData("facebook-midcut","/includes/fancount.php",this.value);">';

echo '<select class="selbox" onchange="categoryAjaxData(\'facebook-midcut\',\'/includes/fancount.php\',this.value);">';

注意代码的输出将如何成为无效的 HTML:

<select class="selbox" onchange="categoryAjaxData("facebook-midcut","/includes/fancount.php",this.value);">

You may want to change this:

echo '<select class="selbox" onchange="categoryAjaxData("facebook-midcut","/includes/fancount.php",this.value);">';

with this:

echo '<select class="selbox" onchange="categoryAjaxData(\'facebook-midcut\',\'/includes/fancount.php\',this.value);">';

Note how the output of your code would be invalid HTML:

<select class="selbox" onchange="categoryAjaxData("facebook-midcut","/includes/fancount.php",this.value);">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文