Pass PHP variable into html file - php

I'm trying to pass a variable in PHP in the same page. The form is:
<form name="form1" method="POST" action="index.php">
<input type="text" name="F1" value="" size="35">
</br>
<input type="submit" name="B1" value="Go!">
</form>
and the PHP script is:
<?php
if(isset($_POST['B1']))
{
$u=$_POST['F1'];
$document=new domDocument('1.0', 'WINDOWS-1251');
$document->loadHTML
('<html>
<head>
<title></title>
</head>
<body>
echo $u;
</body>
</html>');
$document->formatOutput=true;
$document->encoding='WINDOWS-1251';
$document->saveHTMLFile("output.php");
}
?>
The script is meant to create an "output.php" file that prints the content of the text area decleared in the form, but it not works, what can I do?
Thanks!

change this:
$document->loadHTML
('<html>
<head>
<title></title>
</head>
<body>
echo $u;
</body>
</html>');
to
$document->loadHTML
('<html>
<head>
<title></title>
</head>
<body>
'.$u.'
</body>
</html>');

You can also use like
$document->loadHTML
("<html>
<head>
<title></title>
</head>
<body>
$u
</body>
</html>");

Related

Post data that can be accessed by multiple pages

I am trying to find a way for multiple pages to access the data provided from a single HTML form submission.
Currently, I have 4 pages...
index.php (containing my HTML form)
functions.php (where the HTML
form sends data to)
results1.php
results2.php
Can anyone please let me know where I'm going wrong or point me in the right direction?
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
<input type="text" name="value1">
<input type="text" name="value2">
<input type="submit">
</body>
</html>
functions.php
<?php
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php include_once("functions.php") ?>
</head>
<body>
<?php echo $value1, $value2; ?>
</body>
</html>
You can do it using PHP Session.
Try with the given code below.
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
Value 1: <input type="text" name="value1"><br>
Value 2 :<input type="text" name="value2"><br>
<input type="submit">
</body>
</html>
functions.php
<?php
session_start();
$_SESSION["value1"] = $_POST["value1"];
$_SESSION["value2"] = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php session_start(); ?>
</head>
<body>
<?php echo 'Value 1:'. $_SESSION["value1"]; ?><br>
<?php echo 'Value 2:'. $_SESSION["value2"]; ?>
</body>
</html>

PHP code is converted as comments in browser and $_REQUEST is not displaying any data?

I have created two simple web pages, one is written in HTML, and the second is written in PHP. In the HTML page, I include form tag with action attribute and its attribute value is index.php(second page) and index.php(second page) include $_REQUEST but no value is displayed.
html page
<html>
<head>
<title>testing of php</title>
</head>
<body>
<h3>here the value will be displayed</h3>
<form method="post" action="index.php">
please enter your name:
<input type="text" name="don"></input><br>
<input type="button" value="send"></input>
</form></body>
</html>
php page
<html>
<head>
<title>testing of php</title>
</head>
<body>
<?php
echo "hello:";
echo $_REQUEST['don'];
?>
</body>
</html>
The type should be submit not button to submit the form using post method :
<input type="submit" value="send"></input>
You need to make another input type.
Working code:
HTML
<html>
<head>
<title>testing of php</title>
</head>
<body>
<h3>here the value will be displayed</h3>
<form method="post" action="index.php">
please enter your name:
<input type="text" name="don"></input><br>
<input type="submit" value="send"></input>
</form></body>
</html>
PHP
<html>
<head>
<title>testing of php</title>
</head>
<body>
<?php
echo "hello:";
echo $_REQUEST['don'];
?>
</body>
</html>
https://www.w3schools.com/html/html_form_input_types.asp
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit

How to get php variable from an action form and use it in the form thats activating the action

I have two files here as a test the first one which is this below and when I click submit it suppose to do the action on the next page but I want to know how to get retrieve athe $life variable from the action php file and put it in the normal html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
Second file which is the php file:
<?php
$life ="Yo";
?>
check this code. you need to run this code in server
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
include_once 'edit.php';
echo $life;
?>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
and this is your include edit.php
<?php
$life = 'Ok';
?>
then your first file show ok when you run this code

Execute function on php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I recently start the php programming. However, I encountered a problem about running a function on php file. My php file is listed as follows:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="Test()" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function Test()
{
echo "Goodbye";
}
?>
</body>
</html>
After running the program, a webpage is observed with A button entitled submit and the word "Hello". However, After click on the button, the webpage "This page cannot be displayed" is observed. But I respect the word "Goodbye" is shown. I transferred the php code to another file, but the problem was not resolved.
actions in forms aren't functions like JavaScript. You are trying to run Test() on submit which isn't a thing. What you want is action to be action=" "
In reality you should be doing something like this:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
if(isset($_POST['submit'])){
echo "Goodbye";
}
?>
</body>
</html>
If you WANT to use a function....
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function test(){
echo "Goodbye";
}
if(isset($_POST['submit'])){
test();
}
?>
</body>
</html>
Lets say he file name is test.php where you have put all your code so the action values should be test.php
<form method="post" action="test.php" >

Notice:Undefined Index [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP Undefined index / variables
(2 answers)
Closed 9 years ago.
I am trying to learn PHP.I can't run this example.But I think codes are true.I am trying it at my localhost.How can i run it?
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name"fname">
<input type="submit">
</form>
<?php
$name=$_REQUEST['fname'];
echo $name;
?>
</body>
</html>
Error:
( ! ) Notice: Undefined index: fname in C:\wamp\www\index.php on line 12
You haven't submitted the form yet so $_POST['fname'] does not exist. Try this:
<?php
// turns off all errors and notices, recommended for a production website
// comment out this code if on development environment, it will make you a better programmer
error_reporting(0);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name"fname">
<input type="submit">
</form>
<?php
if(isset($_REQUEST['fname']))
{
echo $_REQUEST['fname'];
}
?>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if(isset($_POST['fname'])){
$name=$_POST['fname'];
echo $name;
}
?>
</body>
</html>
You need to check if the variable is set, otherwise you will end up with that error message.
Do something like:
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">Name:
<input type="text" name "fname">
<input type="submit">
</form>
<?php
if (isset($_REQUEST['fname'])) {
$name = $_REQUEST[ 'fname'];
echo $name;
}
?>
</body>
</html>

Categories