Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to display the value from a PHP variable in the value of an HTML text input field. Currently the text input field after submit just prints the contents of the PHP script. The script is called submit_type.php

<?php
    if(isset($_POST['submit'])) {
            $wire_type = $_POST['wire_type_option'];
            header("Location: ./guest.html");
    }
    else {
            echo "loop";
    }
?>

I have already tried the solution from the question using a PHP variable in an text input value = statement as seen in the guest.html line 39, but it doesn't seem to work

<input type="text" name="type" value="<?php echo htmlspecialchars($wire_type); ?>" />

The submit button on the main index.html page redirects you to guest.html (almost identical to the index.html file, with the exception of line 39, where the value is set)

For reference, here is guest.html

<!DOCTYPE html>
<html>Current
    <head>
            <title> Thermocouple Database</title>
            <link rel="stylesheet" type="text/css" href="main.css">
    </head>

    <body>
            <section>
                    <h1> Login to upload a calibration</h1>
                    <form>
                            <div class='login'><label for="username"> Username: </label></div>
                            <input type="text" name="username">
                            <div class='pass'><label for="password"> Password: </label></div>
                            <input type="text" name="password">
                            <input type="submit" value="Submit">
                            <br> <br> <br> <br>
                    </form>
            </section>
            <section>
                    <h1> Which thermocouple type do I have? </h1>
                    <label> My wire looks like: </label>
                    <form method="post" action="submit_type.php">
                    <select name='wire_type_option'>
                            <option value="J-type"> J-type </option>
                            <option value="K-type"> K-type </option>
                            <option value="T-type"> T-type </option>
                            <option value="E-type"> E-type </option>
                    </select>
                    <input type="submit" name="submit" value="Submit wire type">
                    </form>
                    <!-- <form method="get" action="action.php"> -->
                    <div id="results">
                            Result: <label name="result_label"> </label>
                            <div id="result_table">
                                    <label> Type: </label>
                                    <input type="text" name="type" value="<?php echo htmlspecialchars($wire_type); ?>" />
                                    <label> Temperature Range: </label>
                                    <input type="text" name="min_temp">
                                    <label> - </label>
                                    <input type="text" name="max_temp">
                                    <br> <br> <br>
                                    <label> Published calibration </label>
                                    <br> <br>
                                    <label> Voltage: </label>
                                    <input type="text" name="in_voltage">
                                    <label> Calibrated Temperature: </label>
                                    <input type="text" name="out_temp">
                                    <br> <br>
                                    <label> Temperature: </label>
                                    <input type="text" name="in_temp">
                                    <label> Calibrated Voltage: </label>
                                    <input type="text" name="out_voltage">
                            </div>
                    </div>
                    <!-- <input type="submit" value="Submit"> -->
                    <!-- </form> -->
            </section>
    </body>

</html>

What am I missing here? I am still new to web development, so any and all help is appreciated!

share|improve this question
    
Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. – Fred -ii- Mar 31 '15 at 3:44
1  
Your page has the extension of html, change it to php for the php code to be executed before sending the page to the browser. – jeff Mar 31 '15 at 3:45
1  
"The submit button on the main index.html" - Did you instruct Apache to treat .html files as PHP? If not, then you can't run PHP directives. Rename it to .php or tell Apache to treat them as PHP. Same thing goes for guest.html and any other .html files you may be trying to run PHP directives. – Fred -ii- Mar 31 '15 at 3:45
    
Not sure if that variable will persist even if you change it to be handled as PHP. There's probably a better solution for what you're trying to do. – rtheunissen Mar 31 '15 at 3:46
    
Changing the file extension did not help. Wouldn't the variable persist if it were a session variable? – user2227821 Mar 31 '15 at 17:34
 $wire_type = $_POST['wire_type_option']; // assignment operator 
 header("Location: ./guest.html"); // redirecting to guest.html

if you want to display $wire_type to another page you need to add it in session variable or need to create $_GET request, although i do not suggest using $_SESSION variable.

Fixes that you need to done for $_GET

 header("Location: ./guest.php?wi_type=".$wire_type); or  header("Location: ./guest.php?wi_type=".$_POST['wire_type_option']);

How to access wi_type on guests.php

echo $_GET['wi_type'];

Fixes for $_SESSION

$_SESSION['wire_type'] = $_POST['wire_type_option']
header("Location: ./guest.php")

on guest.php

echo $_SESSION['wire_type'];

If you know that you will be needing wire_type alot more times on different pages go with $_SESSION

-- Updates

session_start(); put it on the top 
if(isset($_POST['submit'])) {
       $_SESSION['wire_type']  = $_POST['wire_type_option'];
        header("Location: ./guest.php");
}
else {
        echo "loop";
}

You have to have .php ext with session_start(); at top for your guests file.

share|improve this answer
    
I tried in guest.php <input type="text" name="type" value="<?php echo $_SESSION['wire_type']; ?>" /> but the text field is still blank – user2227821 Mar 31 '15 at 16:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.