Michele
Michele

Reputation: 15

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). Right now I just want to write out the user input from element_1 to this field. This is just a dummy function that will later be replaced by a python script. However, when I click on the submit button there is no output in the field element_5 and the content of all other files disappears as well. Can you check where I go wrong? 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>

Upvotes: 0

Views: 600

Answers (1)

ADyson
ADyson

Reputation: 62073

echo '<id="element_5" would need to be 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.

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.

Upvotes: 1

Related Questions