简体   繁体   English

使用PHP在同一页面上的下拉菜单的所选项目

[英]using the selected item of drop down menu on same page using php

i have created the drop down menu using php and i want to use the selected value of menu on url of the current page. 我已经使用php创建了下拉菜单,我想在当前页面的URL上使用菜单的选定值。 How can i do this without redirecting to the next page. 我如何做到这一点而不重定向到下一页。 the code for creating dropdown menu is: 用于创建下拉菜单的代码是:

echo '<select name="Company">';
if($data['count']>0){
foreach($data['results'] as $key=>$value){
echo "<option value='".$value['CID']."'>".$value['Name']."</option>";
}
}
echo "</select>";

Hum? 哼? Dont know what you exactly want. 不知道你到底想要什么。 You want to create a dropdown menu and use the selected value on the current php page? 您要创建一个下拉菜单并在当前php页面上使用选定的值吗?

That wont work. 那行不通。 Ill show you how it works: 病给你看它是如何工作的:

PHP renders page -> gives it to you. PHP渲染页面->给您。

When you see the page, the php-script is already done. 当您看到页面时,php脚本已经完成。 So you have to call your page again and give it the value via $_POST ... 因此,您必须再次调用您的页面,并通过$ _POST为其赋值...

echo <form action="page.php" method="post">
echo '<select name="Company">';
if($data['count']>0) {
    foreach($data['results'] as $key=>$value){
        echo "<option value='".$value['CID']."'>".$value['Name']."</option>";
    }
}
echo "</select></form>";

... or $_GET (this only works in combination with JS!) ... ...或$ _GET(仅与JS结合使用!)...

echo "<a href='page.php?company=companyname'>Im a link!</a>"

You need to get drop down value using javascript or JQuery 您需要使用javascript或JQuery获取下拉值

In Javascript: 用Javascript:

<script type="text/javascript">
var myselect=document.getElementById("Company")
for (var i=0; i<myselect.options.length; i++){
 if (myselect.options[i].selected==true){
  alert("Selected Option's index: "+i)
  break
 }
}
</script>

In JQuery 在jQuery中

$('select[name=Company]').val()

It's not entirely clear how you want to use this value, but you can refer to it on the same page using javascript. 尚不清楚要如何使用此值,但可以使用javascript在同一页面上引用它。 Consider the code below, which will update the line "Your company is:" whenever a new option is selected from the dropdown box. 考虑下面的代码,只要从下拉框中选择一个新选项,它将更新“您的公司是:”行。

<script type="application/javascript">
    function update_box() {
        selected_value = document.getElementById('company_dropdown').value;
        document.getElementById('your_company').innerText = selected_value;
    }
</script>

<form action="page.php" method="post" id="my_form" name="my_form" onchange="update_box()">
    <select name="company_dropdown" id="company_dropdown">
        <option value='A'>A</option>
        <option value='B'>B</option>
        <option value='C'>C</option>
    </select>
</form>


Your company is: <span id="your_company" name="your_company"></span>

If I am getting it right, you want to use the selected value from dropdown on the same page url via querystring. 如果我做对了,您想通过querystring在同一页面url上使用下拉列表中的选定值。 You can do that like following:- 您可以按照以下步骤进行:

echo '<select name="Company" onchange="location.href = location.href+'?var=' + this.value">';
if($data['count']>0){
foreach($data['results'] as $key=>$value){
    echo "<option value='".$value['CID']."'>".$value['Name']."</option>";
}
}
echo "</select>";

When you will change the value in dropdown, current page will refresh with the selected option value in querystring. 当您更改下拉列表中的值时,当前页面将使用查询字符串中的所选选项值进行刷新。 You can use the querystring value as following:- 您可以使用querystring值,如下所示:

<?php echo isset($_GET['var']) ? $_GET['var'] : ''; ?>

You can also do it via use of hash value without refreshing the page 您也可以通过使用哈希值来执行此操作,而无需刷新页面

When ever you change the dropdown on the onchange event you can write the code 每当您更改onchange事件的下拉列表时,您都可以编写代码

onchange="window.location.hash(this.value); " onchange="window.location.hash(this.value);

it will set a hash value in the url and then you can write a function which will detect the change in the hash value as the page is not refreshing you need to track the change in the hash value and then you can show the selected menu on the basis oh the hash value. 它会在url中设置一个哈希值,然后您可以编写一个函数来检测哈希值的变化,因为页面没有刷新,您需要跟踪哈希值的变化,然后可以在哈希值的基础。

use this to detect the change in the hash value 用它来检测哈希值的变化

$(document).ready(function() {
    $(window).bind('hashchange', function(e) {
        var hashStr = window.location.hash;
        hashStr = hashStr.substring(1, hashStr.length);
        hashArray = hashStr.split('&');
        // here you will get the array of all the parameters you passed in the array 
    });
});  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM