简体   繁体   English

将 php 变量的值解析为 HTML 输入字段

[英]Parse value of php variable to HTML input field

I'm trying to parse the value of a php variable to an HTMl input field ( element_5 ).我正在尝试将 php 变量的值解析为 HTMl 输入字段( element_5 )。 Right now I just want to write out the user input from element_1 to this field.现在我只想把element_1的用户输入写到这个字段。 This is just a dummy function that will later be replaced by a python script.这只是一个虚拟 function 稍后将被 python 脚本替换。 However, when I click on the submi t button there is no output in the field element_5 and the content of all other files disappears as well.但是,当我单击提交按钮时,字段element_5中没有output并且所有其他文件的内容也消失了。 Can you check where I go wrong?你能检查我 go 哪里错了吗? Here is my code:这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
    function display()
    {
    $val=$_POST['element_1'];
    echo '<id="element_5" name="element_5" class="element text medium" type="text" maxlength="255" value="'. $val .'">';
    //echo $val;
    }
    if(isset($_POST['submit']))
    {
       display();
    } 
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Policy Lookup</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>

</head>
<body id="main_body" >
    
    <img id="top" src="top.png" alt="">
    <div id="form_container">
    
        <h1><a>Policy Lookup</a></h1>
        <form id="form_26900" class="appnitro"  method="post" action="form.php">
                    <div class="form_description">
            <h2>Policy Lookup</h2>
            <p></p>
        </div>                      
            <ul >
            
                    <li id="li_1" >
        <label class="description" for="element_1">Source IP </label>
        <div>
            <input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_2" >
        <label class="description" for="element_2">Destination IP </label>
        <div>
            <input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_4" >
        <label class="description" for="element_4">Drop Down </label>
        <div>
        <select class="element select medium" id="element_4" name="element_4"> 
            <option value="" selected="selected"></option>
<option value="1" >TCP</option>
<option value="2" >UDP</option>
<option value="3" >ICMP</option>
<option value="4" >IP</option>

        </select>
        </div> 
        </li>       <li id="li_3" >
        <label class="description" for="element_3">Destination Port </label>
        <div>
            <input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>
            
                    <li class="buttons">
                <input type="hidden" name="form_id" value="26900" />
                
                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
        </li>
        <li id="li_5" >
        <label class="description" for="element_5">Result </label>
        <div>
            <input id="element_5" name="element_5" class="element text medium" type="text" maxlength="255" value="" readonly/> 
        </div> 
        </li>       
            </ul>
        </form> 
        <div id="footer">
            Generated by <a href="http://www.phpform.org">pForm</a>
        </div>
    </div>
    <img id="bottom" src="bottom.png" alt="">
    </body>
</html>

echo '<id="element_5" would need to be echo '<input id="element_5" . echo '<id="element_5"需要是echo '<input id="element_5" But that will duplicate the box you've already got lower down, and also it would be outside the <html> tag, making it invalid (because it's not part of the HTML document), so the browser is not obliged to show it.但这会复制你已经得到的框,并且它会在<html>标签之外,使其无效(因为它不是 HTML 文档的一部分),所以浏览器没有义务显示它。

Try this instead:试试这个:

<?php
$val = null;

if(isset($_POST['submit']))
{
   $val = $_POST['element_1'];
} 
?>

and on the element further down the page:在页面下方的元素上:

<input id="element_5" name="element_5" class="element text medium" type="text" maxlength="255" value="<?php echo ($val != null ? $val : ""); ?>" readonly/>

Note the inline PHP there to echo $val if it's not null.如果不是 null,请注意那里的内联 PHP 以回显 $val。

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

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