在页面重新加载到自己的PHP后保持文本框中的值?

时间:2015-03-30 11:38:10

标签: php forms drop-down-menu onchange retain

我在表单中有两个文本框和两个下拉列表。在文本框中填充值并从下拉列表中选择值后,页面将重新加载并根据下拉列表中选择的值填充其他下拉列表。但文本框中的值不存在。如何保留这些文本框值。

<html>
<head>

</head>
<body>
<form action="selectmultiplepost.php" method="post" name="myform">

First Name<input type="text" name="firstname" value=""><br>
Last Name<input type="text" name="lastname" value=""><br>


Industry Name <select name="industryid" id="industrySelect" onchange="document.location.href = 'selectmultiple.php?industryid=' + this.value">
<option>SELECT</option>
<?php
include('config.php');

$query = "select IndustryId, IndustryName from industrytable";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
{
    while($fetch = mysql_fetch_array($result))
    {
        ?>
        <option value="<?php echo $fetch['IndustryId'];?>"><?php echo $fetch['IndustryName'];?></option>
        <?php
    }
}


?>
</select><br>
Skills <select name="skillname">
<option>SELECT</option>
<?php
include('config.php');

if(isset($_GET['industryid']))
{
$industryID = $_GET['industryid'];
}
else
{
$industryID = "";
}

$skillQuery = "select SkillId, SkillName from skilltable where Industryid='".$industryID."'";
$resultSkill = mysql_query($skillQuery);
if(mysql_num_rows($resultSkill) > 0)
{
    while($fetch = mysql_fetch_array($resultSkill))
    {
        ?>
        <option value="<?php echo $fetch['SkillId'];?>"><?php echo $fetch['SkillName'];?></option>
        <?php
    }
}

?>

</select><br>
<input type="submit" name="submitform" value="submit"/>
</form>

<script>

document.getElementById('industrySelect').value = "<?php echo $_GET['industryid'];?>";
</script>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

是的,我认为你需要了解一些关于工作原理的基础知识,所以我将忽略你使用mysql_ *扩展的事实,你不应该再使用它,因为它已被弃用。

好的,首先要记住的是,每当你a)从浏览器启动页面和/或b)按下提交按钮时,php脚本/页面将从上到下执行。

关于您的实际问题,这意味着创建First NameLast Name输入字段的表单的两行将在每次执行时对其进行编码时运行。

因此,如果你想重新加载用户输入的内容,你必须自己动手,我担心它不是由魔法完成的。

<form action="selectmultiplepost.php" method="post" name="myform">

First Name<input type="text" name="firstname" value=""><br>
Last Name<input type="text" name="lastname" value=""><br>

因此,您需要检查用户是否输入了任何内容,就像您在页面后面的industryid中所做的那样,如果是这样,请将该值放入value=""属性中。像这样的东西

<?php
    $fn = !empty($_POST['firstname']) ? $_POST['firstname'] : '';
    $ln = !empty($_POST['lastname']) ? $_POST['lastname'] : '';
?>
    First Name<input type="text" name="firstname" value="<?php echo $fn;?>"><br>
    Last Name<input type="text" name="lastname" value="<?php echo $ln;?>"><br>

我还注意到后面的代码使用的是$_GET,但是您的<form标记告诉浏览器使用method="post"所以您应该使用$ _POST数组而不是$ _GET数组一直在你的代码中。

我也在你的代码中看到很多其他错误,但由于它们不是你问题的主题,我会留下那些让你提出其他问题

答案 1 :(得分:0)

我认为简单的方法是这样的:

from permissive_dict import PermissiveDict
cfg = PermissiveDict()
cfg.tt = {'0': 'aeroplane'}  # this is ok
cfg.tt = {0: 'aeroplane'}  # this does not raise errors, but this is what I want to use!

cfg.tt[0]) == cfg.TT[0] == cfg.tT[0] == cfg.Tt[0] == 'aeroplane'

<form action="selectmultiplepost.php" method="post" name="myform">  
First Name<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])){
echo $_POST['firstname'];} ?>">

<br>
Last Name<input type="text" name="lastname" value="<?php if(isset($_POST['lastname'])){
echo $_POST['lastname'];} ?>">

<!-- begin snippet: js hide: false console: true babel: false -->

顺便说一下,对于SQL查询,请使用PDO类来实现安全性!

答案 2 :(得分:-1)

这样,

First Name<input type="text" name="firstname" value=isset($_GET['firstname'])? $_GET['firstname']:""><br>

Last Name<input type="text" name="lastname" value=isset($_GET['lastname'])? $_GET['lastname'] :""><br>