How to keep data inside a text area after submission? - php

I have a text area where i can input text, after the form has been submitted the text from the first field goes to the second output field. Now I have the exact same things on the same page, so input field, submit and output field. What I want is when the either one of the submit buttons have been pressed, the output will stay and not clear out. Here is what I've done so far:
<head>
<title></title>
<meta charset="utf-8">
<?php
$inputText = $_GET['FirstText'];
$InputTextTwo = $_GET['SecondText'];
?>
</head>
<body>
<form action="#" method="GET">
<textarea cols="50" rows="2" name="FirstText"></textarea>
<br>
<input type="submit" name="submit_firstField" value="FirstField">
<br>
<textarea disabled="yes" cols="50" rows="2" name="SecondOfFirst"><?php echo $inputText; ?></textarea>
</form>
<hr/>
<form action="#" method="GET">
<textarea cols="50" rows="2" name="SecondText"></textarea>
<br>
<input type="submit" name="submit_SecondField" value="SecondField">
<br>
<textarea disabled="yes" cols="50" rows="2" name="SecondOfSecond"><?php echo $InputTextTwo; ?></textarea>
</form>
</body>
As you can see, when the first submit has been done and I want to do the second one, the first output disappears, and vice versa.

I have solved your problem.
The main reason is the two textareas now used different Form.
So Only one textarea value can submit at once.
I added temp hidden-input field inside each form and run the code.
And It's working well now. Please check them.
Tian Yang
<html>
<head>
<title></title>
<meta charset="utf-8">
<?php
$inputText = 'asdf';
$InputTextTwo = 'bbbb';
if(isset($_GET['FirstText'])){
$inputText = $_GET['FirstText'];
$InputTextTwo = $_GET['temp'];
}
if(isset($_GET['SecondText'])){
$InputTextTwo = $_GET['SecondText'];
$inputText = $_GET['temp'];
}
?>
</head>
<body>
<form action="#" method="GET">
<textarea cols="50" rows="2" name="FirstText"></textarea>
<br>
<input type="submit" name="submit_firstField" value="FirstField">
<input type="hidden" name="temp" value ="<?php echo $InputTextTwo; ?>" />
<br>
<textarea disabled="yes" cols="50" rows="2" name="SecondOfFirst"><?php echo $inputText; ?></textarea>
</form>
<hr/>
<form action="#" method="GET">
<textarea cols="50" rows="2" name="SecondText"></textarea>
<br>
<input type="submit" name="submit_SecondField" value="SecondField">
<input type="hidden" name="temp" value ="<?php echo $inputText; ?>" />
<br>
<textarea disabled="yes" cols="50" rows="2" name="SecondOfSecond"><?php echo $InputTextTwo; ?></textarea>
</form>
</body>
</html>

Related

PHP Form Post/Redirect/Get Header Location Error

I have a simple form that I've made to help me learn php. I'm using my local host server to host the php file (form.php). However, when I refresh the page it resubmits the form, I know I can use the post/redirect/get method to negate this problem. Except implementing the header('Location: form.php'); hasn't been working, if you could take a look at the code- and tell me what I'm doing wrong, that would be much appreciated.
Code example i.e without header('Location: form.php');
<?php
if (empty($_POST) === false) {
echo '<pre>', print_r($_POST, true), '</pre>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
This is the result...
Screen Shot without header location
then I add:
header('Location: form.php');
And I get this...
To prevent a form from resubmitting on page refresh, two methods are used:
Method 1: Use AJAX + Redirect
Submit your form using AJAX and then redirect to another page using JQuery.
Method 2: Reload the page
Refresh the page using Javascript.
Your code should be like this:
<?php
if (!empty($_POST)) {
echo '<pre>', print_r($_POST, true), '</pre>';
echo '<script type="text/javascript"> location.reload();</script>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
</body>
</html>

php form handling input value post self some characters error

i post data into form action to php self some characters make errors. This charactor are " or \ if u put " input name return into \ how to fix it
<form id="form1" name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<label for="name"></label>
<input name="name" type="text" id="name" value='<? print htmlspecialchars($_POST['name']); ?>' />
<br />
<input type="submit" name="button" id="button" value="Submit" />
<? print htmlspecialchars($_POST['name']); ?>
</form>
Im not exactly sure what the problem is but I think you are getting errors when returning special characters into an input field. If so try this: http://php.net/manual/en/function.htmlentities.php you will need to convert the special characters into html entities so the data isnt read as code which will cause issues.
I have just tested this by changing the message input to <?php echo htmlentities($_POST['name']); ?> and the characters are displaying as normal.
Use this code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name"></label>
<input name="name" type="text" id="name" value="<?php echo htmlentities($_POST['name']); ?>" />
<br />
<label for="message"></label>
<textarea name="message" id="message" cols="45" rows="5"><?php echo $message; ?></textarea>
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
echoed (<?php echo htmlentities($_POST['name']); ?>);
</body>
</html>

How to take inputted text from a textarea and display it on another page with PHP?

I'm constructing my website and I wanted to know if it was possible to write something in html's 'textarea' on one page then take that text and print it out using PHP on a new webpage that saves the text last entered.
I just wanted to know how you would go about the process. At the moment I have created a HTML form with three text boxes that I would like to be displayed once the submit button is pressed.
<div id="createmeeting">
<form method="POST">
<b>Meeting Title:</b>
<textarea name="title" cols="50" rows="2"></textarea>
<b>Date and Time:</b>
<textarea name="date" cols="50" rows="2"></textarea>
<b>Details:</b>
<textarea name="details" cols="50" rows="20"></textarea>
<input type="submit" name="go" value="Save Changes!">
First you need an action attribute in your form tag, that points to the php file that should process the form data:
<div id="createmeeting">
<form method="POST" action="targetfile.php">
<b>Meeting Title:</b>
<textarea name="title" cols="50" rows="2"></textarea>
<b>Date and Time:</b>
<textarea name="date" cols="50" rows="2"></textarea>
<b>Details:</b>
<textarea name="details" cols="50" rows="20"></textarea>
<input type="submit" name="go" value="Save Changes!">
</form>
</div>
and in targetfile.php
<?php
/* ensure that the data has been posted */
if($_SERVER['REQUEST_METHOD'] == 'POST') {
/* read the data from $_POST */
$title = $_POST['title'];
$date= $_POST['date'];
$details= $_POST['details'];
/* here you can do whatever you want with the data, output it, store it in a database ... */
echo "Title: ".$title."<br>";
echo "Date: ".$date."<br>";
echo "Details: ".$details;
}
?>

How to transfer text box from page to another page

I am new to php and I need some help.
I have two sites. And I want to display text box from my edit.php on index.php.
<form method="post">
<textarea name="content" cols="40" rows="5">
Enter your comments here...
</textarea><br>
<input type="submit" value="Submit" />
</form>
<?php
$content = $_POST["content"];?>
How do I transfer variable $content to my index page?
To address your comments. (Don't down rank me... his original question was not precise).
// on your edit page
<form method="post">
<textarea name="content"></textarea>
<input type="submit" />
</form>
// when you submit your edit page
$filename = '/mytextarea.txt';
$content = file_put_contents( $filename, $_POST['content'] );
// on your index page
$filename = '/mytextarea.txt';
$content = file_get_contents($filename);
if( ! empty( $content ) ){
echo '<div>'.$content.'</div>';
}
From edit you want to make sure your action points to index.php
edit.php
<form method="post" action="index.php">
<textarea name="myTextBox" cols="40" rows="5"></textarea>
<input type="submit" value="Submit" />
</form>
In index.php, you can access the variable with $_POST["myTextBox"], this can be outputted as text or as a value in the example below. Be sure to use htmlentities when including the value back with a form field, this will prevent "injection hacking".
index.php
<?php echo $_POST["myTextBox"] ?>
<form method="post" action="index.php">
<textarea name="myTextBox" cols="40" rows="5"><?php echo htmlentities($_POST["myTextBox"]); ?></textarea>
<input type="submit" value="Submit" />
</form>
Well, just set the action on the form to the page you want it to post to - example:
Random Page:
<form method="post" action="/index.php">
<textarea name="content" cols="40" rows="5">
Enter your comments here...
</textarea><br>
<input type="submit" value="Submit" />
</form>
Index.php:
if(isset($_POST['content']) && !empty($_POST['content'])){
echo htmlspecialchars($_POST['content']);
}
At least I think this is what you are asking?
If you are echoing a random textbox to a page, make sure to sanitize it using htmlspecialchars()
Try to action your form to your index.php :
<form method="post" action="/index.php">
</form>
With that, once you submit your form, your page index.php will be loaded, with values of your form in $_POST.
// on your edit page
<form method="post" action="index.php">
<textarea name="content"></textarea>
<input type="submit" />
</form>
// on your index page
if( $_SERVER['REQUEST_METHOD'] == 'POST' ){ // This will prevent the textarea from
// being shown unless some form posted to this page
echo '<form method="post">
<textarea name="content">'.$_POST['content'].'</textarea>
<input type="submit" />
</form>';
// If it's not set or it's empty, then it just won't show anything....
// you don't need to check for that unless you want to set a value when it is empty
}
Just change your form to post to the second site instead of to itself, then echo the $_POST['content'] on your index.php page
www.firstsite.com/edit.php
<form action="www.secondsite.com/index.php" method="post">
<ul>
<li>
<textarea name="content" cols="40" rows="5">Enter your comments here...</textarea>
</li>
<li class="submit">
<input type="submit" value="Submit" />
</li>
</ul>
</form>
www.secondsite.com/index.php
<?php echo $_POST['content']; ?>

trying to post to php file - server error

I'm a newbie here to php and Apache.
I have an html form:
<html>
<body>
<?php
<form method="post" action="contact.php"> Email: <input name="email"
type="text"/><br/> Message:<br/> <textarea name="message" rows="15" cols="40">
</textarea><br/> <input type="submit"/> </form>
</body>
</html>
I have contact.php located in what I believe is the right place, but when I submit the query, I get "The HTTP verb POST used to access path '/www/contact.php' is not allowed."
When I access contact.php as a url I get strange results (like a repeating File Download message box asking whether I want to Save or Open)
if you are using which is missing in your code.
for your below code will work.
<html>
<body>
<form method="post" action="contact.php"> Email: <input name="email"
type="text"/><br/> Message:<br/> <textarea name="message" rows="15" cols="40">
</textarea><br/> <input type="submit"/> </form>
</body>
</html>
First Thing Give Name to Submit Button and second remove
<html>
<body>
<form method="post" action="contact.php"> Email: <input name="email"
type="text"/><br/> Message:<br/> <textarea name="message" rows="15" cols="40">
</textarea><br/> <input type="submit" name="submit" /> </form>
</body>
</html>
and if you can still face an error then try to give full file path in action

Categories