使用 PHP 根据用户输入的下拉列表中动态更改 HTML


dynamically change html based on user input in a drop down with php

我是 php 的新手,我正在尝试动态更改用户在下拉列表中做出选择时显示的表单。 我可以显示下拉列表,但在提交时它不显示任何内容。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Forms</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
If  (!isset($_GET['userChoice']))
{ //open the if block to write the html to the screen
?>
<h1>Show your html option form</h1>
<form method="GET" action="thirdtry.php" name="userChoice">
            <select size="1" id="choice" name="choice">
                <option selected="selected">Choose which form you would like</option>
                <option value="feedback">Feedback Form </option>
                <option value="help">Help Request Form</option>
            </select>
            <input type="submit">
</form>
<?php
}//close the if block
     else{ //show feedback
        if($_GET['userChoice']=="feedback")
        {
        ?>
            <h1>Show the feedback form</h1>
            <form id="fb" name="fb"  method="post" >
    <fieldset>

        <label>First Name: 
            <br />
                <input type="text" name="fName" id="fName" />
        </label>
            <br /> 
        <label>Last Name: 
            <br />
                <input type="text" name="lName" id="lName" />
        </label>
            <br />
        <label>Street Address:  
            <br />
                <input type="text" name="address" id="address" />
        </label>
            <br />
        <label>City 
            <br />
                <input type="text" name="city" id="city" />
        </label>
            <br />
        <label>State 
            <br />
                <input type="text" name="state" id="state" />
        </label>
            <br />
            <label>Postcode 
            <br />
                <input type="text" name="postcode" id="postcode" />
        </label>
            <br />
            <label>Country 
            <br />
                <input type="text" name="country" id="country" />
        </label>
            <br />
        <label>Email Address: 
            <br />
                <input type="text" name="FromAddress" id="FromAddress" />
        </label>
        <br />
        <label>Your message:
            <br />
                <textarea name="message" rows="7" cols="30"></textarea> <br />
        </label>
            <div>How many stars would you give this site? <br />
            (We love 5 stars!)<br />
            <input type="radio" name="rating" value="1" />1<br />
            <input type="radio" name="rating" value="2" />2<br />
            <input type="radio" name="rating" value="3" />3<br />
            <input type="radio" name="rating" value="4" />4<br />
            <input type="radio" name="rating" value="5" />5<br />
            </div>
            <br />
        <input type="submit" />
        <input type="reset" />
    </fieldset>
</form>

        <?php
        }
            else
            {//must be help form
            ?>
            <h1>Show the help form</h1>
            <form id="help" method="post" >
    <fieldset>
        <label>First Name: 
            <br />
                <input type="text" name="fNameHelp" id="fNameHelp" />
        </label>
            <br /> 
        <label>Last Name: 
            <br />
                <input type="text" name="lNameHelp" id="lNameHelp" />
        </label>
            <br />
        <label>Street Address:  
            <br />
                <input type="text" name="addressHelp" id="addressHelp" />
        </label>
            <br />
        <label>City 
            <br />
                <input type="text" name="cityHelp" id="cityHelp" />
        </label>
            <br />
        <label>State 
            <br />
                <input type="text" name="stateHelp" id="stateHelp" />
        </label>
            <br />
            <label>Postcode 
            <br />
                <input type="text" name="postcodeHelp" id="postcodeHelp" />
        </label>
            <br />
            <label>Country 
            <br />
                <input type="text" name="countryHelp" id="countryHelp" />
        </label>
            <br />
        <label>Email Address: 
            <br />
                <input type="text" name="FromAddress" />
        </label>
        <br />
        <label>Your message:
            <br />
                <textarea name="messageHelp" rows="7" cols="30"> </textarea> <br />
        </label>
            <div>How Would you like to be contacted?<br />
            (Choose all that apply) <br />
            <input type="checkbox" name="ckemail" id="ckemail" value="email" onclick="return validateHelpForm()"/>email<br />
            <input type="checkbox" name="cktelephone" id="cktelephone" value="telephone" onclick="return validateHelpForm()"/>telephone<br />
            </div>
            <br />
        <input type="submit" />
        <input type="reset" />
    </fieldset>
</form>
            <?php
            }//close the if block
        }
            ?>  

</body>
</html>

您正在检查错误的变量:

If  (!isset($_GET['userChoice']))

应该是

If  (!isset($_GET['choice']))

另外,你为什么要大写If?(这不会引起问题 - 这只是奇怪的;)

首先,你是希望页面刷新,还是希望它动态更改而不刷新,使用 Javascript。 从您的简要描述中,我认为您希望在不刷新的情况下进行更改。

作为一个"新手",我建议你使用Jquery作为你的框架,而不是成熟的javascript。 这是我认为你想要完成的。

由于 PHP 是一种服务器端编程语言,因此如果不刷新,就无法动态更改网页。 Javascript和Jquery是客户端的,所以很多事情都可以在你的浏览器中发生,不需要或刷新。

链接:这是我刚刚创建的示例。

////////jquery/javascript///////
$('#changeme').change(function(){
var value = $('#changeme').val();
if(value ==  1){
    $('#dis1').show();
    $('#dis2').hide();
}
else if(value == 2){
    $('#dis1').hide();
    $('#dis2').show();     
}
else{
    $('#dis1').hide();
    $('#dis2').hide();
}

});
////////HTML///////
<select id="changeme">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<div style="display:none;" id="dis1">Displaying 1</div>
<div style="display:none;" id="dis2">Displaying 2</div>