0

I am currently learning the most basic PHP ever. I have 5 files.

index.php:

<html>
<head>
<title>Budget Calcule</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>

<h2>Put in your: - </h2>
<form action="functions.php" method="post">
    <h3>Income</h3>
<label>Salary: <input name="salary" type="text" /></label><br />
    <h3>Outgoings</h3>
<label>Living: <input name="living" type="text" /></label><br />
<label>Insurance: <input name="insurance" type="text" /></label><br />
<label>Communication: <input name="communication" type="text" /></label><br />
<label>Loan: <input name="loan" type="text" /></label><br />
<label>Food & Drink: <input name="foodAndDrink" type="text" /></label><br />
<label>Entertaintment / Shopping: <input name="entertainmentOrShopping" type="text" /></label><br />
<label>Transport: <input name="transport" type="text" /></label><br />
<label>Other: <input name="other" type="text" /></label><br />
<input type="submit" value="Submit" />
</form>

</body>
</html>

this is my functions.php:

<?php

include('variables.php');





if(!($_POST['Submit'])){
        if(isset($_POST['salary'])){
        header('Location: output.php');
        return $_POST['lon'];
        }else{
        echo "All fields are required";
        }
}


?>

this is my variables.php:

<?php
$salary= $_POST['salary'];
$living= $_POST['living'];
$insurance= $_POST['insurance'];
$communication = $_POST['communication'];
$loan = $_POST['loan'];
$food = $_POST['food'];
$entertaintmentOrShopping = $_POST['entertaintmentOrShopping'];
$transport = $_POST['transport'];
$other= $_POST['other'];
?>

this is my output.php file:

<?php
include('outputFunction.php');
?>

<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>

and last but not least, this is my outputFunction.php file:

<?php
include('variables.php');

function myText(){
echo "Your salary per month is: " . $_POST['salary'];
}


?>

Now you're thinking "why have he split up his code in different files?" Well first of all, I split the variables from functions.php because I wanted outputFunctions.php to get the variables from variables.php so i could echo my `$_POST['salary']; . The function myText(); outputs the text just fine, but it doesnt output the $_POST['salary'];.

I do not know why it doesnt work, I just wonder if you could be my extra eyes and see if I've done some mistake.

PS! Don't down vote my question just because you think it's stupid. I am having problem with this issue and been working on it for hours without advancing anywhere.

4

3 回答 3

1

一些东西:

您不需要包含 variables.php 文件。您正在访问的变量是全局的,您只是在创建未使用的重复项。它们也会在页面更改后消失,因为您在每个页面加载时都重新声明它们。

当您引用 $_POST['lon'] 而不是 'loan' 时,您还试图调用一个不存在的变量。

最后要真正回答你的问题:

您的 myText() 函数正在引用一个不再存在的变量。

您需要将 functions.php 和 outputFunction.php 和 output.php 合并到一个文件中,这样变量就不会丢失,并且所有处理都完成了,而无需每次打开一个新文件。我可以看到您对分隔文件的原始概念,但输出文件将是处理表单输入数据的文件。

现在在你新合并的 output.php 中,你应该有类似这样的东西:

<html>
<head>
<title>Output</title>
</head>
<body>

<?php
        if(isset($_POST['Submit'])) {
                if(isset($_POST['salary'])) { 
                        echo "Your salary per month is: " . $_POST['salary'];
                }
        } else {
                echo "All fields required.";
        }
?>

</body>
</html>

这意味着只有两个文件 - 您的表单页面和此页面。

于 2013-09-14T23:52:48.017 回答
0

还有一些提示:

如果您想检查表单是否已提交,它看起来像这样:

if(isset($_POST['Submit'])){ ... }

此外,您应该为您的提交按钮添加一个 name="" 属性:

<input type="submit" name="Submit" value="Submit" />

variables.php 的用途是什么?您不使用任何这些变量。

于 2013-09-14T23:21:40.253 回答
0

当您通过 header() 重定向用户时,存储在 $_POST 数组中的数据会丢失。

您可以直接重定向到 ouput.php

<form action="output.php" method="post"> 

并做这样的事情:

<?php
include('outputFunction.php');

if(isset($_POST['Submit'])) {
    if(isset($_POST['salary'])) {
?>

<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>

<?php

    } else {
        echo "All field required";
    }
}
?>

顺便说一句,您可以随时检查 $_POST 包含的内容,print_r($_POST); 这对于调试非常有用。

于 2013-09-14T23:48:13.153 回答