简体   繁体   English

使用JavaScript onchange的下拉菜单不起作用

[英]Dropdown using javascript onchange doesn't work

I have a simple drop down and I want to have it so that if the user selects "STUDENT", it will display Student data from mysql. 我有一个简单的下拉菜单,我想拥有它,以便如果用户选择“ STUDENT”,它将显示来自mysql的Student数据。 But the page stays the same (it show's the "STUDENT" for one second and back to the original page),and this isn't working. 但是页面保持不变(显示“学生”一秒钟并返回原始页面),并且此页面无法正常工作。 Can someone please help. 有人可以帮忙吗?

Below is my code: 下面是我的代码:

<th width="20%">View&nbsp;Type:&nbsp;&nbsp;&nbsp;&nbsp;
            <form action="index.php" method="post" id="form1">
            <select  name="value1" onchange="document.forms.form1.submit();">
                    <option value="all">All('.$count.')</option>
                    <option value="student">STUDENT('.$stucount.')</option>
                    <option value="faculty">FACULTY('.$facount.')</option>
               </select>  

 </form>       
    </tr>
';  

try this: 尝试这个:

<select  name="value1" onchange="this.form.submit()">

all $_post should be $_POST . 所有$_post应该是$_POST

$_POST is a super global variable and all variables in php are case sensitive. $_POST是一个超级全局变量,php中的所有变量都区分大小写。

Set post data in your option and check the post data and set select one option. 在您的选项中设置发布数据,然后检查发布数据并设置选择一个选项。 Try this : 尝试这个 :

<select  name="value1" onchange="this.form.submit()">
            <option <?php echo (isset($_POST['value1']) && $_POST['value1'] == 'all')? 'selected="selected"' :  ''; ?> value="all">All('.$count.')</option>
            <option <?php echo (isset($_POST['value1']) && $_POST['value1'] == 'student')? 'selected="selected"' :  ''; ?>  value="student">STUDENT('.$stucount.')</option>
            <option <?php echo (isset($_POST['value1']) && $_POST['value1'] == 'faculty')? 'selected="selected"' :  ''; ?> value="faculty">FACULTY('.$facount.')</option>
</select>

Apart from the answer posted by Awlad try this for your second problem. 除了Awlad发布的答案之外, 尝试第二个问题。 Your code is not complete by the way, so before you echo the above form place this: 顺便说一下,您的代码并不完整,因此在回显以上表单之前,请先放置以下代码:

$value = (isset($_POST['value1'])) ? $_POST['value1'] : '';

And replace with this: 并替换为:

<th width="20%">View&nbsp;Type:&nbsp;&nbsp;&nbsp;&nbsp;
            <form action="index.php" method="post" id="form1">
            <select  name="value1" onchange="document.forms.form1.submit();">
                    <option value="all"'.($value=="all"?"selected":"").'>All('.$count.')</option>
                    <option value="student"'.($value=="student"?"selected":"").'>STUDENT('.$stucount.')</option>
                    <option value="faculty"'.($value=="faculty"?"selected":"").'>FACULTY('.$facount.')</option>
               </select>  

 </form>       
    </tr>
'; 

$value saves the POST value sent, once you click on the dropdown and based on that value, the respective option will be selected. $value保存发送的POST值,一旦单击下拉列表并基于该值,将选择相应的选项。

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

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