Alaa
Alaa

Reputation: 3

php calculation keeps getting the answer as 0

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

<div class="form-group">
    <label> Insert R value </label>
    <input type="text" name="r" placeholder ="please insert your R value ">
    
</div>
<br>
<div class="form-group">
    <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.

Upvotes: 0

Views: 48

Answers (2)

Hlabi
Hlabi

Reputation: 51

From what I saw from your code. Inside the form tag, you need to add action='answer.php', this is a direct link to the page you are requesting data from, in this case 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 class="container">
        <form method="post" action="answer.php">
            <div class="form-group">
                <label> Insert P value </label>
                <input type="text" name="p" placeholder="please insert your P value">
 </div>
<div class="form-group">
    <label> Insert R value </label>
    <input type="text" name="r" placeholder ="please insert your R value ">
</div>
<br>
<div class="form-group">
    <label> Insert your N value </label>

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

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

Upvotes: 0

ADyson
ADyson

Reputation: 61849

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.

Upvotes: 1

Related Questions