Retrieve text from a textarea after submission - php

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>

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>

How to carry data from a form on one file to another using PHP sessions?

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>

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

Undefined index with session

I'm developing a chatbox script, and I have this page that checks if session is set, and if so, the certain elements of code should be hidden with jQuery. Here are my pages:
input.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#import "stil.css";
</style>
<title>Untitled Document</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="postme.js"></script>
<?php
include_once('check.php');
?>
</head>
<body>
<div id="wrap">
<div id="chat">
<div id="main">
</div>
<div id="input">
<form name="form"action="test.php" method="post">
<input type="text" name="tekst" id="msg" size="72" />
<input type="submit" name="dugme" value="posalji" id="dugme" />
</form>
</div>
</div>
</div>
<div id="black">
</div>
<div id="name">
<form name="yname">
<input type="text" name="tekst2" />
<input type="button" name="dugme2" value="Enter" onclick='send()' />
</form>
</div>
</body>
</html>
sesion.php:
<?php
session_start();
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
?>
check.php:
<?php
include('sesion.php');
if (!isset($sesion_n)){
echo "<script type='text/javascript'>$('#black').hide();$('#name').hide();</script>";
}
?>
postme.js:
function send(){
$.post('sesion.php',{ime:yname.tekst2.value},function(val){
if(val!=null) {
$('#black').fadeOut();
$('#name').hide();
alert(val);
}
}
)};
So the problem is that I get this error every time I run the page:
Notice: Undefined index: ime in C:\wamp\www\AJAX\sesion.php on line 3.
So can someone tell me what I'm doing wrong here?
if(isset($_POST['ime']))
{
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
}
it seems that $_POST['ime']; is undefined and that means that you are not posting it i guess.
Are you sure that yname.tekst2.value is the correct way to access the value of the field?
If you have firebug you can check in the "console" tab what parametrs have been posted.
It appears you're loading check.php manually. That'd be a GET request, and will trash your stored value, as _POST won't be set on those pages. Probably won't be the cause of the undefined index problem, but something to consider.
Check that the session's ID value stays constant between requests. If it's different each time, you're getting a brand new blank session on each request.

Categories