How to get a PHP variable value from another PHP file? - php

how to take the value from another .php file help me fix this:
<html>
<head>
</head>
<body>
<form name='answer' action="file2.php" method="post">
<input type="text" name="what">
<input type="submit" value="Login">
</form>
<div><?php echo $answer; ?></div>
</body>
</html>
And the code of file2.php file is as below:
<?php
if ($_POST['what']==animal){
$answer="dog";
}else{
$answer= "not animal";
}
I would like to know how can I get the value of the variable $answer with button, then what should I put at the line :

Just use requiere
<?php require 'file2.php'; ?>

Use require_once() to include file2.php in your php file, then in your file you can access the variables in file2.php.
require_once("path of file2.php");
echo $answer;

Related

Get variable from Post Data (PHP)

I really don't know why my code isn't working! Let me clear it by example..
I got a file named 'index.html' example...
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="test">
<input type="submit">
</form>
</body>
</html>
And of course the form action file test.php .. example..
<?php
$something = ($_POST["test"]);
// from here I have some PHP codes and this "something" variable will be process in this codes and will print out something spacial..
?>
Now example If I post "Hello, It's not working" then the output will show a spacial design.
But Instead process, it's just printing out whatever I submit in that form.
But when I manually add the variable to "something" and if I execute the "test.php" . Example..
$something = "Hello, It's not working";
Then it works perfectly..
And yes. Also tried GET method.. It's still same as POST.
This is my first question here..
Thanks for any help and suggestions!
First Convert, index.html to php and follow this code:-
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" value="" name="test">
<input name="submit" value="submit" type="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$something = $_POST["test"];
}
?>
That's correct _POST:
<?php
//NO ($_POST["test"])
$something = $_POST["test"];
?>

How come nothing is appearing when I add if statements to my PHP code?

I am trying to make a form that echos out whatever the user inputs into the 'name' field, but, I cannot figure out why when I add an if statement, nothing in the code shows up.
Here is my index.php code:
<?
if (empty($_POST['name'])) {
echo "Hello world!";
} else {
echo "Hello $_POST['name']";
}
?>
<html>
<head>
<?php include 'header.php'; ?>
</head>
<body>
<form action="index.php" method="post">
<p>Name: <input type="text" name="name"></p>
<input type="submit" name="submit" value="Go">
</form>
</body>
<footer>
<?php include 'footer.php'; ?>
</footer>
</html>
The valid syntax for adding an array into echo is:
echo "Hello {$_POST['name']}";
Or concatenate it using:
echo "Hello " . $_POST['name'];
Reference: echo
Make sure you enable errors while debugging:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Problem, seems to be with php short tag <? which might not enabled, try using <?php or enable short tag in php.ini
The page is reloading index.php with your form. change <form action="index.php" method="post"> to <form action="" method="post">
You can't echo array inside "" quotes.
Try this (var in {}):
echo "Hello {$_POST['name']}";
Or this (with string operator):
echo "Hello " . $_POST['name'];

How to handel php error

I'm using two files one is of html and another is of php
In my html file I have created this
code for my html file : index.html
<html>
<body>
<form method='get' action='ans.php'
<input type="text" name="name">
<input type="button" name="submit">
</form>
</body>
</html>
When user enter the input into the input tag it will take the user to the ans.php file in which I have coded some php
My php file: ans.php
<?php
$text= $_GET['name'];
echo $text
?>
if(isset($_GET['name']))
{
echo $_GET['name'];
}
else
{
}
Use empty() to check if the variable is present and not empty or false. Add a check -
if(!empty($_GET['name'])){
$text= $_GET['name'];
echo $text;
}
If the variable may contain false then Hanky 웃 Panky's answer will work.

Response from one page to another page in php without ajax

I am just started with Php and have some doubts.
I created 2 pages one.php and two.php
ONE.php
<body>
<form method="post" action="TWO.php">
First Number<input type="text" name="txt1"><br>
Second Number<input type="text" name="txt2"><br>
<input type="submit">
</form>
</body>
TWO.php
<body>
<?php
$sum=$_POST["txt1"] + $_POST["txt2"];
echo $sum;
?>
</body>
I POST values form one.php to two.php. Two.php calculates the sum and echo's the result.
My query is would I be able to get the sum echoed on one.php using just php and its important to post the data on another page and get the response from there.
Yes you are. Simply, handle the form submission (the POST request) in one.php. When the request for one.php is not POST just show the form.
The typical way is:
// ONE.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$content = "Sum is " . $_POST["txt1"] + $_POST["txt2"];
}
else {
$content = <<<EOC
<form method="post" action="ONE.php">
First Number<input type="text" name="txt1"><br>
Second Number<input type="text" name="txt2"><br>
<input type="submit">
</form>
EOC;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>ONE</title>
<meta charset="utf-8"> <!-- or whatever charset you are using -->
</head>
<body>
<?php echo $content ?>
</body>
</html>
Edited, the OP needs the two files but print the result on one.php
In order to pass data between two files you can:
Set the data in the first file (two.php) and require the second (one.php, where you would print the value)
Use PHP sessions.
With the latter you need to do a (well you don't need to but it's mean to work with a) page redirect, so my recommended approach for this case is use the former. The code could be something like:
// TWO.php
<?php
// you should probably check if $_POST['txt1'] and $_POST['txt2'] does really exists and throw and error if not...
$sum = $_POST["txt1"] + $_POST["txt2"];
require "ONE.php" // careful if you're on a *nix file system the NameCase is extremely important!
// ONE.php
<?php
if (isset($sum)) {
$content = "Sum is " . $sum;
}
else {
$content = <<<EOC
<form method="post" action="TWO.php">
First Number<input type="text" name="txt1"><br>
Second Number<input type="text" name="txt2"><br>
<input type="submit">
</form>
EOC;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>ONE</title>
<meta charset="utf-8"> <!-- or whatever charset you are using -->
</head>
<body>
<?php echo $content ?>
</body>
</html>
Try this code:
<body>
<?php
if($_POST){
$sum = $_POST["txt1"] + $_POST["txt2"];
echo $sum;
}else{
?>
<form method="post" action="">
First Number<input type="text" name="txt1"><br />
Second Number<input type="text" name="txt2"><br />
<input type="submit">
</form>
<?php } ?>
</body>
You can try with this, in only one page:
ONE.php
<html>
<head></head>
<body>
<form method="post" action="ONE.php">
First Number<input type="text" name="txt1"><br>
Second Number<input type="text" name="txt2"><br>
<input type="submit">
</form>
<?php
// Verify if $_POST["txt1"] and $_POST["txt1"] are defined
// (when form is submit $_POST, $_GET and other $_ PHP vars
// are set). If form isn't submitted, set 0 on each variable
// to perform sum. Is necessary check values to avoid PHP
// Warnings/Errors (In this case with isset function. There
// are many different ways to perform it)
$txt1 = isset($_POST["txt1"]) ? $_POST["txt1"] : 0;
$txt2 = isset($_POST["txt2"]) ? $_POST["txt2"] : 0;
$sum = $txt1 + $txt2;
//Print sum result
echo 'Sum is: '.$sum;
?>
</body>
</html>
For comparations I'm using ternary operators, that simplify/minimize code of traditional if/else. Also you can use traditional if/else (simple compare) or switch(multiple compare)

ECHO page1.php textarea values into page2.php

I have 2 php files in my folder. In page1.php, there's a textarea, user should enter some values in it. In page2.php, it will grab what is in the textarea and work with its program. But I can't find a command that grabs the value in textarea. Can someone help me?
page1.php:
<?
$hello = "hello";
?>
<html>
<input type = "text" name = "user_input">
</input>
</html>
page2.php
<?
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
?>
So, is there anyone that can solve this? =/
Use $_GET or $_POST in page2.php
page1.php
<?
$hello = "hello";
?>
<html>
<form method="get" action="page2.php" enctype="multipart/form-data">
<input type = "text" name = "user_input">
<input type="submit">
</form>
</html>
page2.php
<?
$text=$_GET['user_input'];
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
echo $text;
?>
You may use either $_GET['user_input'] or $_POST['user_input'].
The difference is, you can see the data in the url (visible to everyone) when using GET method and not in the other method.
Also, always use <input> elements (which you want to pass to another file) inside a <form> and specify action="file.php", to where you want to pass data, and the method, either method="get" or method="post", like;
<form method="get" action="page2.php">
also specify the method to grab data in the target file also, like;
$text=$_GET['user_input']; or $text=$_POST['user_input'];
And in your case, you may use;
Method 1
<?php
$hello = "hello";
?>
<html>
<form method="get" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_GET['user_input'];
echo $text;
?>
Method 2
<?php
$hello = "hello";
?>
<html>
<form method="post" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_POST['user_input'];
echo $text;
?>
If you want to share the data over a number of pages, you may do this using PHP Session or saving the data in a cookie.
1. Using Sessions
<?php
session_start();
$_SESSION['data'] = 1; // store session data
echo "Pageviews = ". $_SESSION['data']; //retrieve data
?>
Make sure you add session_start(); on every page you want to handle session
You can read more about php sessions here www.tizag.com/phpT/phpsessions.php/
2. Using Cookie
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
and retreive it using
echo $_COOKIE["user"];
You can read more about php sessions here http://www.w3schools.com/php/php_cookies.asp
hope this helps...:)
basically your page1.php is a page with some form in it with a text area. Now user will have to fill it and submit the form to page2.php. You can't echo it's content like that, because that will be on browser subject to user actions. Use a form and submit the data to page2.php. Like this:
page1.php
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="t1">
</textarea>
</form>
</body>
</html>
page2.php
<?php
$textAreaContents = isset($_POST['t1'])?$_POST['t1']:'';
echo "You submitted: ".$textAreaContents;
?>
if i were you i should use sessions for this. that is where they were made for..
example:when user clicks on submit.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['post'] = $_POST;
}
that is where every post variable will be put in a session.
and your inputbox will be something like this..
<textarea name="message" type="text" value="" rows="0" cols="0" placeholder="" ><?php if(isset($_SESSION['post'])){echo $_SESSION['post']['message'];} ?></textarea>
?>
note that you now can use every post variable that you used in your form by echo (example)
echo $_SESSION['post']['message']
where message is the name of the inputbox. in this case of the textarea
don't forget that at the end when you don't want to use the session anymore use session_destroy(); otherwise you will keep having it in your form. and don't forget session_start(); above every page where you are planning to use sessions ( it must be at 1st line of your document at all times)

Categories