PHP 粘性表单下拉列表和复选框

标签 php html css forms sticky

我正在写一个 php 表单,当我填写我的详细信息时,我无法让下拉框和复选框保持不变,但不要单击某些东西,它会保留其他所有内容,但那个部分没有填写。我可以为输入文本和单选按钮做这件事,但我不能为下拉菜单和复选框做这件事

<!DOCTYPE html>
<style>
@import url(stickyTextInput.css);
</style>
<?php

if(isset($_REQUEST["left"]))
{
    process_form();
}
else
{
    display_form_page('');
}
?>

<?php

function display_form_page($error)
{
    $self =$_SERVER['PHP_SELF'];    
    $first_name = isset($_REQUEST['name']) ? $_REQUEST['name']:'';
    $last_name = isset($_REQUEST['lastname']) ? $_REQUEST['lastname']:'';
    $age = isset($_REQUEST['age']) ? $_REQUEST['age']:'';
    $gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
    $color = isset($_REQUEST['color']) ? $_REQUEST['color']: '';
    $food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
?>
<html>
<head>
<title>
Forms Sticky input
</title>
<style>
@import url(stickyTextInput.css);
</style>
<style type="text/css"> 
.error
{
color:#ff0000
}
</style>
</head>
<body>
<?php
if($error)
{
    echo "<p>$error</p>\n";
}
?>
<form action= "<?php  echo $self?>" method = "post">
<h1>Forms-Sticky Input</h1>
<label>First Name:</label>
<input type="text" size="10" maxlength="40" name="name" value = "<?php echo $first_name?>">
<br>

<label>Last Name:</label>
<input type="text" size="10" maxlength="40" name="lastname" value = "<?php echo $last_name?>">
<br>

<label>Age:</label>
<input type="text" name="age" size="10" value="<?php echo $age?>">
<br>

<label>Gender</label>
<input type="radio" name="gender" value="male" <?php check($gender, "male")?>>Male
<input type="radio" name="gender" value="female" <?php check ($gender, "female")?>>Female
<br>

<label>Select favourite Colour</label>
<select name= "color">
    <option <?php checkradio($color, "Red")?>>Red
    <option <?php checkradio($color, "Blue")?>>Blue
    <option <?php checkradio($color, "Green")?>>Green
    <option <?php checkradio($color, "Pink")?>>Pink
    <option selected="selected" disabled="disabled">
</select>
<br>

<label>Food</label>
<input type="checkbox" name="food[]" value="beans" <?php checkbox ($food, "beans")?>>Beans
<input type="checkbox" name="food[]" value="crisps" <?php checkbox ($food, "crisps")?>>Crisps
<input type="checkbox" name="food[]" value="lemons" <?php checkbox ($food, "lemons")?>>Lemons
<br>



<div id="buttons">
<input type="submit" name="left" id="left" value="Submit" >
<input type="reset" name="right" id="right" value="Reset" >
</div>

</form>
</body>
</html>
<?php
}
?>

<?php
// If $group has the value $val then select this list item
function check($group, $val)
{
if ($group === $val)
{
   echo 'checked = "checked"';
}
}
?>
<?php
function checkradio($group, $val)
{
if ($group === $val)
{
  echo 'selected = "selected"';
}
}
?>

<?php
// If $group has the value $val then select this list item
function checkbox($group, $val)
{
if ($group === $val)
{
  echo 'checked = "checked"';
}
}
?>

<?php
function process_form()
{
    $error = validate_form();
    if($error)
    {
        display_form_page($error);
    }
    else
    {
        display_output_page();
    }
}
?>
<?php
    function validate_form()
    {
        $first_name = trim($_REQUEST['name']);
        $last_name = trim($_REQUEST['lastname']);
        $age = trim($_REQUEST['age']);

        $gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
        $color = isset($_REQUEST['color']) ? $_REQUEST['color']:'';
        $food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
        $error = '';

        $reg_exp = '/^[a-zA-Z\-\']+$/';
        $reg_exp1 = '[0-9]{3}';

        if(!preg_match($reg_exp, $first_name))
        {
            $error .= "<span class=\"error\">First Name is invalid (letters, hyphens, ', only)</span><br>";
        }

        if (!preg_match($reg_exp, $last_name))
        {
            $error .= "<span class=\"error\">Last Name is invalid (letters, hyphens, ', only)</span><br>";
        }

        if (!is_numeric($age))
        {
            $error .= "<span class=\"error\">Age is invalid (numbers only)</span><br>";
        }

        if (strlen($gender) == 0)
        {
            $error .= "<span class=\"error\">Select Male/Female</span><br>";
        }

        if (strlen($color) == 0)
        {
            $error .= "<span class=\"error\">Select one color</span><br>";
        }

        if (! is_array($food))
        {
            $error .= "<span class=\"error\">You must select one      food</span><br>";
        }

        return $error; 
}   
?>

<?php
function display_output_page()
{
$first_name = trim($_REQUEST['name']);
$last_name = trim($_REQUEST['lastname']);
$age = trim($_REQUEST['age']);

$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']:'';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
?>
<html>
<head><title>Form Results</title></head>
<body>
<h1>Form Results</h1>
<?php 
echo " First Name: $first_name<br/>\n";
echo " Last Name: $last_name<br/>\n";
echo " Age: $age<br/>\n";
echo " Gender: $gender<br/>\n";
echo " Favourite Color: $color<br/>\n";
echo "<ul>";
if (is_array($food))
{
    echo "Favourite Food is:";
    foreach($food as $selection)
    {
        echo "<li>$selection</li>";
    }
}
echo "</ul>";
?>
</body>
</html>
<?php
}
?>

最佳答案

可能是浏览器问题,但是大多数浏览器只需要在 html 输入元素末尾为复选框添加“checked”。但是您似乎正在输出 checked = "checked" 这可能是问题所在。看看下面的示例:

<?php
    $name = isset($_GET['name']) ? $_GET['name'] : '';
    $agree = isset($_GET['agree']) ? 'checked' : '';
    $title = isset($_GET['title']) ? $_GET['title'] : '';

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="">
        <input type="text" name="name" id="name" value="<?=$name;?>">
        <input type="checkbox" name="agree" id="agree" <?=$agree;?>>
        <select name="title" id="title">
            <option value="Mr" <?=$title == 'Mr' ? 'selected' : ''?>>Mr</option>
            <option value="Mrs" <?=$title == 'Mrs' ? 'selected' : ''?>>Mrs</option>
            <option value="Miss" <?=$title == 'Miss' ? 'selected' : ''?>>Miss</option>
        </select>
        <button type="submit">submit</button>
    </form>
</body>
</html>

处理值后,输出 html 应如下所示:

<form action="">
    <input type="text" name="name" id="name" value="test">
    <input type="checkbox" name="agree" id="agree" checked>
    <select name="title" id="title">
        <option value="Mr">Mr</option>
        <option value="Mrs">Mrs</option>
        <option value="Miss" selected>Miss</option>
    </select>
    <button type="submit">submit</button>
</form>

关于PHP 粘性表单下拉列表和复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22356260/

相关文章:

javascript - 使用 Spotify API 将我当前在 Spotify 中收听的歌曲添加到网站?

php - SQL 选择具有字符限制的所有列

php - 在单个 PHP 文件中连接两个数据库

javascript - 如何让这个进度条停在我提供的特定值

html - Bootstrap 更改导航项的背景颜色

html - 要在表 TD 中显示的垂直文本?

php - Behat:Goutte/Guzzle 通过 cURL "Warning: curl_setopt_array(): 3607 is not a valid File-Handle resource"下载文件

PHP简单翻译方法——你的看法

javascript - 增加 td 的宽度,使内容在一行中

javascript - 在这些条件下使用 Javascript 或 jquery 滚动到页面底部