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>
Related
I know there are other posts about this, but I still can't seem to see where the code is incorrect.
I'm trying to pass a variable from one page to another via session: Below are the two pages; excuse some of the variable names as I was just plotting them in quickly.
main.php
<?php
session_start();
session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
result.php
<?php
session_start();
echo $_SESSION['itemId'];
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Previously I didn't have the unset in there. However, it hasn't made a difference.
The value is getting stored in the session, its just not passing it through to the other page.
Main page should looks like this:
<?php
session_start();
//remove session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
remove session_unset(); line after start_session()
and why r u destroying session in result page.
if you want to destroy then
store session value in variable
$id = $_SESSION['itemId'];
echo $id;
this code will help u
I'm trying to make a form that acts as a search engine and returns results. However, the data from the user's entry is either unable to save to a session or the session cannot be passed to another file. Here is the code for the "home" search page and the "Search-Engine" results page.
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
<?php session_register(); session_start(); ?>
<?php $_GET['query'] = $_SESSION['Query']; ?>
</body>
</html>
Search-Engine.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
<?php session_start(); ?>
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I don't know the exact purpose of using Sessions in your form. But you are doing in a wrong way by starting Session in middle of page and using Sessions within the form. You can add value in Sessions in another page after submitting the form.
You can update your files in the below way:
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" name="query" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
</body>
</html>
Search-Engine.php
<?php session_start();
$_SESSION['Query'] = $_GET['query']; ?>
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
Say I have a page with a textarea which acts as an input.
Then I have a Submit button and right under everything i have the
output textarea.
Now what I want to do is when the input has been submitted and
sent into the output text area, how can I then retrieve the text from the output area.
This is the code i have:
<head>
<?php error_reporting(0);
$OutputText = $_GET['OutputText'];
?>
</head>
<body>
<form action="#" method="_GET">
<textarea name="InputText">
hi
</textarea>
<input type="submit" name="submitFirstInput">
</form>
<textarea name="OutputText">
<?php echo $_GET['InputText']; ?>
</textarea>
<hr>
<p>Output String Length: <?php echo strlen($OutputText); ?> </p>
</body>
for reasons I dont understand, it cant define the $OutputText,
Do they both have to be in a form? As i have understood form's is only to send data, and testing it didn't help much either.
Keep in mind this is just a barebones version of the original, essentially i have some Input text and then through some logic it gets modified, therefor i want some statistics for the output result. So just getting the first input isnt rather useful..
adding some javascript you can sync the two textarea:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(window).load(function(){
$("#one, #two").on("change keyup", function(){
$("textarea").not($(this)).val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText" id="one"></textarea>
<textarea name="OutputText" id="two"></textarea>
<input type="submit" name="submitFirstInput">
</form>
<hr>
<?php echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length:
<?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
Textarea must be inside the form tag, and the method must be GET (or POST)
try this:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText">hi</textarea>
<input type="submit" name="submitFirstInput">
<textarea name="OutputText"><?php echo $_GET['InputText']; ?></textarea>
</form>
<hr>
<?php //echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length: <?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
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
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