Home > Software engineering >  PHP $_POST values wont send?
PHP $_POST values wont send?

Time:04-19

I am trying to create a short PHP script that takes $_POST data from a form and does a little math. It generates a table based on the input and an accumulator. However, I canont seem to get the $_POST form input to send to the .php file. I have the correct attributes set for the form tag and I have the method set to "post" in my .html file. I get a jarbled mess when I click the submit button. Im not entirely sure what the deal is. I'm a beginner at this stuff. here's my PHP script

        <?php
        isset($_POST['loan'])
        $amount = ($_POST['$amount']);
        $payments = ($_POST['$payments']);
        
        welcome();
        createTable($amount, $payments);
        
    function welcome(){
            print (<header><h1>Welcome to your personalized Loan Calculation</h1></header>
            print <table class=center>);
            print <thead><tr><th>Month</th><th>Balance</th></thead>;
        }   
    function createTable ($amount, $payments){
            $balance = $amount;
            $month=1;
                while $balance >= 0);{
                    $balance = $balance-$payments;
                    
                        if $balance < 0{
                            else ($balance = 0) ;
                    finish($balance, $month);}
                    
                createReport($month, $balance);

            $month  
           }
          }
    function createReport ($month, $balance){
            print ("<tr><td>$month</td><td>$balance</td></tr>");
        }
        function finish($amount, $balance, $month) {
                print ("<tr><td> $month </td><td> $balance </td></tr>");
                print ("</tbody><tfoot>");
                print ("<tr><td colspan=2>Your original balanace was $amount </td></tr>");
                print ("<tr><td colspan=2>Developed by Me :) </td></tr>");
        }
        ?>

Now, I've added and removed all of the double-quotes around the table rows several times thinking this was the issue. Using XAMPP to test it out.

any suggestions? been stumped on this for a few days now.

This image is what is rendered when submit button is clicked.

CodePudding user response:

the file should be with .php extension to be able to render PHP

CodePudding user response:

There is a lot of error in your code. make sure that every statement in php end with ; you missed it in your code in a lot of places so try this, hope you'll get what are you looking for.

    <!-- add this code into index.html -->
    <!DOCTYPE html>
    <html lang="en">

    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        body {
          margin: 0;
          padding: 0;
          box-sizing: border-box;
          width: 100%;
          height: 100vh;
          display: flex;
          justify-content: center;
          align-items: center;
          flex-direction: column;
        }
    
        input {
          padding: 10px;
        }
        
        th,
        td {
          border: 1px solid;
        }
        
        table {
          border-collapse: collapse;
        }
      </style>
    </head>

    <body>      
    <form action="test.php" method="POST">
        <label for="amount">Your current Balance.</label>
        <br>
        <input type="number" name="amount" id="amount" placeholder="Your balance is 340" min="0" value="340">
        <br><br>
        <label for="payments">Your payment.</label>
        <br>
        <input type="number" name="payments" id="payments" value="50" placeholder="Your payment" min="0">
        <br><br>
        <input type="submit" name="loan" value="loan">
      </form>
    </body>

    </html>

    <!-- add this code into test.php file -->
    <style>
    td{
        border: 1px solid;
        width: 300px;
        text-align: center;
    }
    table{
        border-collapse: collapse;
        border: 1px solid;
    }
    </style>
    <?php
    function welcome(){
        echo ("<header><h1>Welcome to your personalized Loan Calculation</h1></header>");
    }   
    if (isset($_POST['loan'])){
        $amount = ($_POST['amount']);
        $payments = ($_POST['payments']);
    }
    function createReport ($month, $balance){
        print ("<table >
            <tr>
                <td>month</td>
                <td>balance</td>
            </tr>
            <tr>
                <td>".$month."</td>
                <td>".$balance."</td>
            </tr>
        </table>");
    }
    function finish($amount, $balance, $month, $payments) {
        echo "<tbody>
            <tr>
                <td>".$month."</td>
                <td>".$amount."</td>
                <td>".$balance."</td>
                <td>".$payments."</td>
            </tr>
            </tbody>";
    }


    welcome();
    createTable($amount, $payments);
    function createTable ($amount, $payments){
        // your math goes here change it as you want
        $balance = $amount;
        $month=0;
        echo "Your balance history...";
        echo "<table><thead>
        <tr>
            <th>month</th>
            <th>starting balance</th>
            <th>current balance</th>
            <th>your payment</th>
        </tr>
        </thead>";
        // if you want to run a loop and print every month like tranjection use while loop, and comment if
        // while start here------------
        while ($balance>$payments) {
            $balance = $balance-$payments;
            $month  ;
            finish($amount, $balance, $month, $payments);
        }
        // while end here----------------



        // if you want print just last payment use 'if' and comment 'while' 
        // if start here---------------
        // if ($balance > $payments){
        //     $balance = $balance-$payments;
        //     finish($amount, $balance, $month, $payments);
        //     echo "<br>";
        // }
        // if end here-------------
        // your math end here-
        
        
        echo "</table>";
        echo "<br>your current balance.";
        createReport($month, $balance);
        echo "<p><strong>Devloped by me ?</strong></p>";


    }

    ?>

  • Related