简体   繁体   中英

How to echo a PHP variable in a HTML input field?

I'm trying to get the output as Discount price & Total saved. I have written all the code but I'm not getting the output. I don't understand why it's happening.

PHP code:

<?php 

if (isset($_GET['submit']))     {
    
    $price = $_GET["price"];
    $percent = $_GET["percent"];
    $discount = $price-($price*$percent/100);
    $saved = $price-$discount;
}
?>

I want to show the output in a simple HTML tag value, but it's not working.

<table align="center">
  <tr>
    <td>Discounted Price: </td>
    <td><input type="text" name="discount" value='<?php echo $discount; ?>'</td>
  </tr>
  <tr>
    <td>Total Saved: </td>
    <td><input type="text" name="totalsaved" value='<?php echo $saved; ?>' </td>
  </tr>
</table>

One of the reasons it can be is that isset($_GET['submit']) is not true. Try to debug by doing something like this and see if you can see the hardcoded discount and saved value.

if (isset($_GET['submit']))     {
    
    $price = $_GET["price"];
    $percent = $_GET["percent"];
    $discount = $price-($price*$percent/100);
    $saved = $price-$discount;
} else {
    $discount = 10;
    $saved = 10;
}

Problem solving

Without seeing more of the code it's hard to say what may or may not be wrong however here are some general ideas to check what is happening and test the code...

  1. Check that all of your get variables were submitted correctly:

     // Put this code at the top of the page which the form is submitted to echo "<pre>"; var_dump($_GET); /* It should output something like: array(3) { ["price"]=> string(3) "100" ["percent"]=> string(2) "10" ["submit"]=> string(6) "Submit" } */
  2. Set a query string in the URL and pass it directly to the webpage:

     /webpage.php?submit=submit&percent=10&price=100

Solution

I'm going to assume that you have two webpages:

  1. input.php : where the user enters the data to be used in the second page.
  2. output.php : which returns the calculated discounts etc.

input.php

The first file should look something similar to this:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Input Document</title>
</head>
<body>
    <form action="/output.php" method="get">
    Price: <input type="text" id="price" name="price"><br>
    Dicount percentage: <input type="text" id="percent" name="percent"><br>
    <input type="submit"  id="submit" name="submit" value="Submit">
</form>
</body>
</html>

When the user goes to this page the user will enter the required data in the form and click on submit.

NB

  • The method of the form is set to get
  • The name of the inputs must be set to the values expected in the PHP

output.php

<?php

if( isset($_GET['submit']) ){
    $original_price    = $_GET["price"];
    $discount_percent  = $_GET["percent"];
    $discounted_price  = $original_price - ( $original_price * $discount_percent / 100 );
    $amount_saved      = $original_price - $discounted_price;
}
else{
    header("location:/input.php");
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Output Document</title>
</head>
<body>
    <table align="center">
    <tr>
        <td>Discounted Price: </td>
        <td>
            <input type="text" name="discount" value="<?php echo $discounted_price; ?>">
        </td>
    </tr>
    <tr>
        <td>Total Saved: </td>
        <td>
            <input type="text" name="totalsaved" value="<?php echo $amount_saved; ?>">
        </td>
    </tr>
</table>
</body>
</html>

Alternatively as one page...

<?php

if (isset($_GET['submit']))     {
    $original_price    = $_GET["price"];
    $discount_percent  = $_GET["percent"];
    $discounted_price  = $original_price - ( $original_price * $discount_percent / 100 );
    $amount_saved      = $original_price - $discounted_price;

    $output = '
    <table align="center">
        <tr>
            <td>Discounted Price: </td>
            <td>
                <input type="text" name="discount" value="'.$discounted_price.'">
            </td>
        </tr>
        <tr>
            <td>Total Saved: </td>
            <td>
                <input type="text" name="totalsaved" value="'.$amount_saved.'">
            </td>
        </tr>
    </table>
    ';
}
else{
    $output = '
    <form action="?" method="get">
        Price: <input type="text" id="price" name="price"><br>
        Dicount percentage: <input type="text" id="percent" name="percent"><br>
        <input type="submit"  id="submit" name="submit" value="Submit">
    </form>
    ';
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Complete Document</title>
</head>
<body>
    <?php echo $output; ?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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