php 如何根据 <select> 更改内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14652144/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change content depending on a <select>?
提问by elizur424
I need help on 2 things since I'm not savy in PHP: Content that changes depending on the:
我需要两件事的帮助,因为我对 PHP 不精通: 内容根据以下情况而变化:
<select>
    <option>Option 1</option>
    <option>Option 2</option>
</select>
I think this can be done using PHP, by giving names to the options, having PHP get the names, creating variables depending on those names and putting an "include" that shows the changing content.
我认为这可以使用 PHP 来完成,方法是为选项命名,让 PHP 获取名称,根据这些名称创建变量并放置一个显示更改内容的“包含”。
Here's the model for the script:
这是脚本的模型:
READ Select 
IF User selects "variable1, varialbe2,etc" 
THEN Display "page1,page2,page3" 
*The page changes within the page so that if the user wants to change it again they dont have to go back...Maybe this can be done by using:
READ Select 
IF User selects "variable1, varialbe2,etc" 
THEN Display "page1,page2,page3" 
*页面在页面内更改,这样如果用户想再次更改,他们就不必返回......也许这可以可以通过使用:
<?php include 'page1.php'; ?>
That content will include a button, and depending on that button, I want a text box to be filled with certain text.
该内容将包含一个按钮,根据该按钮,我希望文本框填充特定文本。
<input type="button" value="">
Then Depending on the value of that button the PHP will put text into a textbox. From what I've read this will be using $request and $post ... (This of course is separate PHP)
然后根据该按钮的值,PHP 会将文本放入文本框中。从我读过的内容来看,这将使用 $request 和 $post ...(这当然是单独的 PHP)
回答by Strixy
There may be a couple of different ways to accomplish your goal. If we stick to using PHP as you've requested then something like this is probably best. I've used a case/switch here for security purposes and a POST instead of a GET to prevent URL manipulation although with the case/switch you have addressed a lot of URL manipulation anyway so it becomes a matter of preference for you.
可能有几种不同的方法来实现您的目标。如果我们坚持按照您的要求使用 PHP,那么这样的事情可能是最好的。出于安全目的,我在这里使用了 case/switch 和 POST 而不是 GET 来防止 URL 操作,尽管使用 case/switch 你已经解决了很多 URL 操作,所以它成为你的偏好问题。
<?php
$page = null;
if(isset($_POST['page'])){
    $page = $_POST['page'];
}
switch($page){
    case 'page3': include_once('/path/to/page3content.php'); break;
    case 'page2': include_once('/path/to/page2content.php'); break;
    case 'page1': include_once('/path/to/page1content.php'); break;
    default: include_once('/path/to/defaultcontent.php'); break;
}
?>
<form name="myform" action="" method="post">
    <select name="page" onchange="this.form.submit()">
        <option value="page1"<?php if($page == "page1"){ echo " selected"; }?>>Page 1</option>
        <option value="page2"<?php if($page == "page2"){ echo " selected"; }?>>Page 2</option>
        <option value="page3"<?php if($page == "page3"){ echo " selected"; }?>>Page 3</option>
    </select>
</form>
However, depending on your file structure, you can accomplish something similar even easer with just a little basic Javascript and HTML.
但是,根据您的文件结构,您可以使用一些基本的 Javascript 和 HTML 来完成类似甚至更容易的事情。
<select name="page" onchange="window.location=this.value">
    <option value="/path/to/page1.php">Page 1</option>
    <option value="/path/to/page2.php">Page 2</option>
    <option value="/path/to/page3.php">Page 3</option>
</select>
回答by Sammitch
You need to have a namefield in your selecttag, and valuefields in your optiontags:
您需要name在select标签中有一个字段,并且标签中有value字段option:
<form name='myform' action='myform.php' method='POST'>
  <select name='myselect'>
    <option value='1'>Option 1</option>
    <option value='2'>Option 2</option>
    <option value='3'>Option 3</option>
  </select>
  <input type='submit'>
</form>
Then the PHP page will receive the selection via $_POST['myselect'].
然后 PHP 页面将通过$_POST['myselect'].
回答by prakashchhetri
You can submit the form onChange event of the select option.
您可以提交选择选项的表单 onChange 事件。
Then you can run a $_GETand display the appropriate content for that particular page by comparing the value of $_GET['page']
然后您可以运行 a$_GET并通过比较 的值来显示该特定页面的适当内容$_GET['page']
  <form action="page.php" method="get">    
    <select name="page" onchange="this.form.submit()">
            <option value="page1">Page 1</option>
            <option value="page2">page 2</option>
        </select>
    </form>
        <?php
        if(isset($_GET['page'])){
            if($_GET['page']=="page1"{
             //content for page 1
            }elseif($_GET['page']=="page2"{
             //content for page 2
            }
        }
        ?>
In this way you wont be needing the submit button as well. You can also redirect it to certain page by comparing the values of $_GET['page']however you will need to include this form in every page with appropriate method to get your work done.
这样你就不需要提交按钮了。您还可以通过比较 的值将其重定向到某个页面,$_GET['page']但是您需要使用适当的方法将此表单包含在每个页面中才能完成您的工作。
回答by jnthnjns
You could also bypass the submit button and use the following:
您还可以绕过提交按钮并使用以下内容:
<form action="index.php" name="myForm"  method="post">
    <select onchange="window.location.href = this.value" name="select">
        <option value="index.php?page=yourpage">yourpage</option>
        <option value="index.php?page=yourpage2">yourpage2</option>
    </select>
</form>
Then where your content would go you could:
然后你的内容会去哪里,你可以:
<?php 
    if (isset($_GET['page'])) {
            if ($_GET['page'] == "yourpage") {include("pages/page1.php");}
            else ($_GET['page'] == "yourpage2") {include("pages/page2.php");}
    } else {include("pages/default.php");}
?>

