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.
A few things:
You don't need to include a variables.php file. The variables you're accessing are global and you're just creating duplicates that aren't being used. They also go away after the page changes since you're re-declaring them each page load.
You are also trying to call a variable that doesn't exist when you reference $_POST['lon'] instead of 'loan'.
And finally to actually answer your question:
Your myText() function is referencing a variable that is not there anymore.
You need to merge functions.php and outputFunction.php and output.php into one file so the variables aren't lost and all the processing is done without opening a new file each time. I can see your original concept for separated files but an output file is going to be the file to process the input data from the form.
Now in your newly merged output.php, you should have something resembling this:
<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>
This means only two files - your form page and this page.
A few more tips:
If you want to check if the form was submitted, it has look something like this:
if(isset($_POST['Submit'])){ ... }
Also, you should add a name="" attribute to your submit-Button:
<input type="submit" name="Submit" value="Submit" />
And what is the variables.php for? You don't use any of those variables.
When you redirect the user via header() the data that is stored in the $_POST array gets lost.
You could directly redirect to ouput.php
<form action="output.php" method="post">
And do something like this:
<?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";
}
}
?>
By the way you can always check what your $_POST contains with print_r($_POST);
This can be very useful for debugging.
Related
I have these three pieces of code which are supposed to be 2 different forms that send the data to the php. But when I am done with filling in the second form the data of the first form is gone. I can't use 2 different php files because I have to send the data to planyo using file_get_contents.
1st file, testform 1:
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform1">
<label for="naam">naam:</label>
<input type="text" name="naam"><br>
<input type="hidden" name="action" value="form1">
<input type="submit" value="form1">
</form>
</body>
</html>
2nd file, testform 2:
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform2">
<label for="achternaam">achternaam:</label>
<input type="text" name="achternaam"><br>
<input type="hidden" name="action" value="form2">
<input type="submit" value="form2">
</form>
</body>
</html>
test.php file:
<?php
$firstname = "";
$lastname = "";
switch($_POST['action']) {
case 'form1':
$firstname = $_POST['naam'];
echo "form 1 gelukt";
header("Location: http://localhost:8081/greenjoy/testform2.html");
break;
case 'form2':
$lastname = $_POST['achternaam'];
echo "form 2 gelukt";
echo "$firstname" . "$lastname";
break;
}
?>
Modify your files like this:
testform1.html: no change.
testform2.html, change it into a php file (testform2.phtml):
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform2">
<label for="achternaam">achternaam:</label>
<input type="text" name="achternaam"><br>
<input type="hidden" name="action" value="form2">
<input type="hidden" name="naam" value="<?php echo $_GET['firstname']; ?>">
<input type="submit" value="form2">
</form>
</body>
</html>
test.php:
<?php
$firstname = "";
$lastname = "";
switch($_POST['action']) {
case 'form1':
$firstname = $_POST['naam'];
echo "form 1 gelukt";
header("Location: http://localhost:8081/greenjoy/testform2.html?firstname=$firstname");
break;
case 'form2':
$firstname = $_POST['naam'];
$lastname = $_POST['achternaam'];
echo "form 2 gelukt";
echo "$firstname" . "$lastname";
break;
}
?>
This demonstrate the concept, but you should add code to verify if $_GET['firstname'] is set (and react if not).
The idea is to carry the firstname value with you when going to testform2.
Ensure your application flow does not allow a user go to testform2 before going to testform1. This could be done by: if $_GET['firstname'] is not set when calling testform2, redirect the user to testform1 automatically.
Remember that each call to test.php is indendant of the other one. This is why the value set when processing testform1 is not available in testform2.
Another method that could be used is to set the value from testform1 into a session or a database and retrieve it when accessing testform2. More complicated.
Ask yourself if exposing the parameter to testform2 is a security risk or not. If yes == use sessions or database! If not, you can use what I propose.
Another solution if you want to hide your $_GET values, you could encrypt the parameters.
I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>
How do we send php page attribute values to a php class method?. I want to get php attribute values into a php class method. following is the way i tried.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<form id="test" method="post" action="testPhp.php">
<label>Name</label>
<input type="text" name="n1" id="name" />
<input type="submit" value="ADD NAME">
</form>
</body>
</html>
Now I want to get this name value in method in separate php file
testPhp.php
<?php
class getParam{
public function getFormParm(){
$name = $_POST['n1'];
}
}
?>
You have no valid action declared as the forms action. Try $_SERVER['self'] as the form action, like so:
<body>
<form id="test" method="post" action="<?php echo $_SERVER['self']?>">
<label>Name</label>
<input type="text" name="n1" id="name" />
<input type="submit" name='submit' value="ADD NAME">
</form>
</body>
And to get the result of this, do the following:
<?php
echo $_POST['n1'];
?>
The way you have the class set up, you aren't actually doing anything with the class. Nothing is being executed. You can do something like this:
<?php
class getParam{
public function getFormParm(){
$name = $_POST['n1'];
return $name;
}
}
// This is the part that you are missing
$getParam = new getParam();
echo $getParam->getFormParm();
Though I enter data and hit Submit it always echoes the else part always. I know it isn't the type of question to be asked on Stackoverflow but...
<html>
<head>
<title>Sticky Form</title>
</head>
<body>
<form method="POST" action=<?php echo $_SERVER['PHP_SELF'] ?>>
<label for="Name">Name</label>
<input type="text" name="FName">
<input type="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['FName'];
echo "$f_name";
}
else
{
echo "Not set!";
}
?>
</body>
</html>
Change this:
<input type="submit">
to
<input type="submit" name="submit">
P.S: key name in global arrays comes from users input ($POST,$_GET,$_COOKIE), if you want to change its key, you need to change that element's name!