Can I self submit back to a textarea box? - php

I am able to create a textarea box that will accept text and store that text to the $_POST super global, but I can't get the text to "come back" to the box once I submit it. (The form is self submitting). If I run a simple echo on the submitted data, however, it displays fine (as shown toward the end of the script below.
<!DOCTYPE html> <body> <?php require("Connection_to_WS.php");
echo ("<form action='Edit_Thread_Description.php' method='post'>");
IF (ISSET($_POST['revised_thread_descr'])) {
$revised_thread_descr=($_POST['revised_thread_descr']);
ECHO "Edit the Revised_Thread_Description here: <br> <textarea name='revised_thread_descr' rows='5' cols='50' value= $_POST[revised_thread_descr]"; // Fails to return any text on Submit.
?><p></textarea></p><br><?php
}
ELSE {$revised_thread_descr= '[some default]';
ECHO "Edit the Revised_Thread_Description here: <br> <textarea name= 'revised_thread_descr' rows='5' cols='50' value= $revised_thread_descr";
?><p></textarea></p><br><?php
}
ECHO '<br>';
echo $_POST['revised_thread_descr']; // Succeeds in returning POST text from the textarea box upon Submit (but outside of the textarea box).
ECHO '<br>';
echo "Click 'Submit': <input type='Submit' name='submit' value='Submit'/>";
echo '<br>';
mysqli_close($connection);
?>
</body> </html>
Doing the same sort of thing was a breeze using "<input type", but I've sunk hours into getting <textarea to cooperate. I I'd be grateful for any assistance.

As Ann Sophie said, there is no "value" property on the textarea element
(https://www.w3schools.com/tags/tag_textarea.asp)
if you want to dynamically append content to it, you can use :
<?php if (isset($_POST['revised_thread_descr'])): ?>
<textarea><?= $_POST['revised_thread_descr'] ?></textarea>
<?php else: ?>
//
Note that you have to echo it, in my example I used alternative syntax,
(http://php.net/manual/fr/control-structures.alternative-syntax.php)
which I think is much more cleaner when you works with PHP + HTML
<?= XXX ?> is short for <?php echo XXX; ?>

I botched a response via 'comment', above. This continues that comment:
This code works, but won't repopulate the box with remarks submitted back to the script via the POST super global.
IF(ISSET($_POST['revised_thread_descr'])):
$revised_thread_descr=($_POST['revised_thread_descr']); ?>
<p> Revised_thread_descr - Edit here:</p><textarea
name='revised_thread_descr' rows='5' cols='50'
<p></textarea></p><br>
<?php
ELSE:
$revised_thread_descr= '[some default]'; ?>
<p> Revised_thread_descr - Edit here:</p><textarea name= 'revised_thread_descr' rows='5' cols='50'
<p></textarea></p><br>
<?php
ENDIF;
echo "Click 'Submit': <input type='Submit' name='submit' value='Submit'/>";
echo '<br>';
mysqli_close($connection);
?>
</body>
</html>
This code, with just a slightly different placement of the <p> tags, gobbles up and displays all html material that comes after the closing </textarea> tag.
IF(ISSET($_POST['revised_thread_descr'])):
$revised_thread_descr=($_POST['revised_thread_descr']); ?>
<p> Revised_thread_descr - EDIT HERE:</p><p><textarea name= 'revised_thread_descr' rows='5' cols='50'
</textarea></p><br>
<?php
As in the screenshot of the browser rendering, below.
Thanks, btw, for getting me to try that alternate syntax! Less confusing.

Bless you! I'd given up and was going for a workaround. I put in that tag caret as you suggested and it all worked. Here is the gist of it, with everything working, and the textarea box populating correctly. Thank you so much for your patience and persistence. Tell me it gets easier... .
<!DOCTYPE html> <body>
<?php
echo ("<form action='Textarea_Example.php' method='post'>");
// The first IF only executes after the script has run once and created a POST value. On the second run, the first IF executes and successfully populates the textarea box with the latest POSTed value
IF (ISSET($_POST['revised_thread_descr'])): ?>
<p>Edit current thread description:<p>
<textarea name= 'revised_thread_descr' rows='5' cols='50'>
<?php echo $_POST['revised_thread_descr'] ?>
</textarea>
<?php ELSE:
$revised_thread_descr = 'some default'; ?>
<p>Edit current thread description:<p>
<p><textarea name= 'revised_thread_descr' rows='5' cols='50'>
The textarea box opens with this in it, but only on the first run. Then it successfully switches to the value typed to the textarea box and saved to POST
</textarea>
<?php ENDIF; ?>
</p>
<?php
// here's the submit button
echo "Click 'Submit': <input type='Submit' name='submit' value='Submit'/>";
?>
</body> </html>

Related

How to get Multiple Inputs?

I want to get Multiple Inputs and use them later when Press Submit Button
<!DOCTYPE html>
<html>
<head>
<title>Class Details</title>
</head>
<body>
<?php
$noOfSections=0;
$sectionNameArray= array();
$sectioerr="";
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(!empty($_POST["noOfSections"]))
$noOfSections = $_POST["noOfSections"];
else
$sectioerr="Must Enter Number of Sections";
}
?>
<form action="classdetails.php" method="POST">
<p>Enter Number of Sections : </p>
<input type="text" name="noOfSections">*<?php echo $sectioerr?>
<input type="submit" name="submit" value="Enter"><br>
</form>
<form action="classdetails.php" method="POST">
//Thats the real area about which I am Asking Questions
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){//it is USED to prosses THE STATEMETS BELOW only when the above Submit Button is pressed
echo "<p>Enter Names of Sections : </p><br>";
for($x=0;$x<$noOfSections;$x++){
echo "Enter Name of Section No. ".($x+1)."<br>";
echo "<input type=\"text\">*";
}
echo "<input type=\"submit\" name=\"submitnames\"><br>";
}
?>
</form>
</body>
</html>
I want to Get Multiple Inputs when I press Submit Button Having name attribute "submitnames"
Try adding [] to the end of your dynamic input names
echo "<input type=\"text\" name=\"names[]"\>*";
Edit :Thanks to Magnus Eriksson for pointing out my mistake in the comments.

How can I make the text input be sent to another page?

I'm running this locally, the page that features this code has this address:
search.php?page=content
<?php if (isset($_GET['page'])) {
$id=$_GET['page']; if ($id=='content') echo "
<div id='content'>
<form action='search.php?page=generated' method='POST' name='value'>
<input type='text'>
<input type='submit' value='Send'>
</form>
</div>";} else echo "";
?>
I want the text inputted in this form to be sent to
search.php?page=generated
I thought the right way to do this was:
#$temp=$_POST['value'];
echo $temp;
But nothing ever gets sent to 'value'. What am I doing wrong?

Editing posted form data using jquery/php

i have a basic form that posts textarea content to the same page. I have that working but I'm trying to get the posted content back into the form, however the textareas disappear after you submit it. This probably sounds bizarre.
<p><?php echo ''.nl2br($_POST['textbox2']); ?></p>
how can i press a button like "edit" and take that $_POST data and put it back into the textarea that it came from.
Any help for this crazy problem I'm having would be appreciated.
Thanks.
check this out: http://php.net/ternary#example-123 and here's my example:
<?php
$textbox2 = isset($_POST['textbox2']) ? $_POST['textbox2'] : '';
?>
<input type="text" name="textbox2" value="<?php echo $textbox2; ?>" />
If you have the variable declared in your php code you can add it into the textarea as follows:
<?php
$text = $_POST['formText'];
?>
<form>
<textarea name="formText">
<?php echo $text;?>
</textarea>
<input type="submit" value="Submit" />
</form>
A working example is here: http://www.phlume.com/chad/testtest/test.php

Array of forms in PHP

First, thanks for taking a look at this. I am trying to create an array of forms that acts as a dynamically sized results list. From the results that were given the user can click 'detail' (a submit button) to get further information on the result which is why I am attempting to create an array of forms. Here is what I had tried, which compiled but the buttons aren't doing anything. Any help would be great :)
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$numbers=array(1,2,3,4,5);
$listsize=count($numbers);
for($currentnum=0;$currentnum <$listsize;$currentnum ++){
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
</form>
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
</body>
</html>
This is really just meant to demonstrate what I am trying to do (thought that would be easier without functions out of scope of question).
Try changing the HTML for the button:
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
Should be:
<input type="submit" value="Submit" name="button<?PHP echo $currentnum?>" />
Close bracket for your for loop so that your check is inside it:
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
Should be:
<?php
echo "<br/>";
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
}
?>
If you learn to indent your code you'll find these kinds of bugs much easier to spot!
Other than that you're good to go...
You need to change your input types from 'button' to 'submit' so that they submit the forms, then you need to move
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
inside of the for loop

print the value to textarea,

I am a beginner and self learner. This might be easy question but it is creating some problem to me. I think I missed something somewhere.
When I write some text in textarea and hit Find and replace button(leaving other two fields empty), the value captured from textarea should appear in the textarea itself and error message should appear outside the textarea. textarea should not be blank. I think message are working fine.
I am not sure if the problem is with the button or action='' in form.
<?php
//find and replace string
//using str_replace(), takes three parameters, $findword, $wordtoreplace, $userinput
if(isset($_POST['text']) && isset($_POST['find']) && isset($_POST['replace'])){
$paragraph=nl2br(htmlentities($_POST['text']));
$find_string=$_POST['find']; //assign the value to be found to the variable
$replace_string=$_POST['replace']; //assign the value to be replaced
if(empty($paragraph)){
echo 'No text to search for.';
}
elseif(empty($find_string)){
echo 'Enter some text to find.';
}
elseif(empty($replace_string)){
echo 'Enter some text to replace with.';
}
else{
echo str_replace($find_string, $replace_string, $paragraph);
}
}
?>
<form action='' method='POST'>
<textarea name='text' rows=20 cols=100 value='<?php echo $paragraph; ?>'></textarea
<br />
<label>Search For</label>
<input name='find' value='<?php echo $find_string; ?>'></input>
<br />
<label>Replace with</label>
<input name='replace' value='<?php echo $replace_string; ?>'></input>
<br />
<button>Find and Replace</button>
</form>
<textarea> doesn't have the value attribute. Provide the content like this:
<textarea name='text' rows='20' cols='100'><?php echo $paragraph; ?></textarea>
Btw, the closing bracket of </textarea was missing
You have to put the output in the midle of textarea open and close tag:
<textarea name='text' rows=20 cols=100><?php echo $paragraph; ?></textarea>

Categories