Home > Software design >  php calculation keeps getting the answer as 0
php calculation keeps getting the answer as 0

Time:04-03

this is the index.php file

if(isset($_POST['submit']))
{
    echo header('Location:answer.php');
}
?>

<!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>
</head>
<body>
    <div >
        <form method="post">
            <div >
                <label> Insert P value </label>
                <input type="text" name="p" placeholder="please insert your P value">
</div>
<br>

<div >
    <label> Insert R value </label>
    <input type="text" name="r" placeholder ="please insert your R value ">
    
</div>
<br>
<div >
    <label> Insert your N value </label>

    <input type="text" name="n" placeholder=" please insert your N value">

</div>
<br>
    <button type="submit" name="submit" > Submit </button>
</body>
</html>

this is the answer.php file

<?php

$p=isset($_POST['p'])?$_POST['p']: " ";
$r=isset($_POST['r'])?$_POST['r']:" ";
$n=isset($_POST['n'])?$_POST['n']: " ";
$si=(int)$p*(int)$r*(int)$n/100;
echo "Hello this is the final answer ".$si;
?>

i want the answer to be displayed in another page that is answer.php but whatever number i am inserting i keep getting the answer as 0. Please help and thank you.

CodePudding user response:

You need to make your form post directly to answer.php. By making it post to itself and then redirecting you're losing all the submitted data - it's sent to your index page and then not transmitted again to the answer page. The redirect causes a separate GET request and doesn't contain any of the submitted data.

The other alternative is to move all the logic into index.php and not bother with a separate script at all.

  • Related