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>
Related
When I sanitize the input fields or text area I face a problem. When someone gave spaces and submit the form, my script accepts the form. But I want not to accept fields until there is not written at least a single character. My code is as follows.
Html
<form action="" method="POST">
<textarea name='text'></textarea>
<input type='submit' name='submit'>
</form>
Php
if(isset($_POST['submit'])){
if(isset($_POST['text']) && !empty($_POST['text'])){
//do whatever but not accept white space
}
}
You can trim whatever you want, just by using
trim()
Which removes characters from both sides of a string.
Documentaion: http://php.net/manual/bg/function.trim.php
trim and preg_replace will do this easily
<?php
echo $text = " this is niklesh raut ";
echo "\n";
$text = preg_replace('/\s+/', ' ',$text);
echo trim($text);
?>
live demo : https://eval.in/818137
OUTPUT :
this is niklesh raut
this is niklesh raut
With new line and tab : https://eval.in/818138
You can either echo out your statement:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['text'])){
echo "Please enter a value.";
}
}
Or, add the required attribute to your input field.
<form action="" method="POST">
<textarea name='text' required></textarea>
<input type='submit' name='submit'>
</form>
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>
<?php
if(isset($_POST['btnLogin'])){
$myVariable = $_POST['fieldParameter'];
if(condition){
//do something
}else{
echo "
<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>
";
}
}
?>
Notice that the variable $myVariable is contained in the main IF block. I'm trying to send the value of $myVariable to submit.php as hidden field.
Also, i enclosed all the html tags using one echo statement with double quotes.
I found related questions here in SO but can't find similar to embedding php within a long echo of html tags
I tried to put value='<?php echo $studentNo; ?>' with no success.
I want to access it in a submit.php file like this,
submit.php
<?php
$aVariable = $_POST['myVariable'];
echo $aVariable;
?>
How can I pass the value contained in $myVariable as hidden field? Is there something wrong with the way I use double and single quotes?
If you are already echoing a string you shouldn't put <?php echo "" ?> inside it again. You should concatenate your string instead. But in your case you don't even need to do that, because you're using double quotes for echoing which means you can simply just write your variable in it.
echo "<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='$myVariable;'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>";
If you were using single quotes for your echo, it would look like this:
echo '<form method="POST" action="submit.php"><br/>
<input type="hidden" name="myVariable" value="' . $myVariable . '"/><br/>
<input type="submit" name="btnSubmit" id="submit" value="Submit">
</form>';
You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the
You can try these variants (simplified):
// code before
echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
// code after
// OR //
// code before
?>
<input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
<?php
// code after
Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).
I'm making a query to the database and am showing the value in input type text as follows:
<input type='text' name='title' value="<?php echo $noticia->_title; ?>" />
What happens is that if the text coming from the database comes within "" the text does not appear because the " " of value. If I switch to '' have the same problem if the text coming from the database is inside ''. How can I solve this problem?
value="<?php echo htmlspecialchars($noticia->_title) ?>"
htmlspecialchars() will encode any HTML metacharcters in there that would otherwise break your form, e.g.
$title = 'Hello "Joe"';
<input ... value="Hello "Joe"" />
^---breaks the form
becomes
$title = htmlspecialchars('Hello "Joe"');
<input ... value="Hello "Joe"" />
Convert text to HTML with htmlspecialchars.
echo htmlspecialchars($noticia->_title);
I try to display an array within a form, which works fine, except of the array value containing an empty space 'Street Number', then it only displays the Street.
If I echo it outside of the form it works, but not within the form and the loop:
//This is able to show Street and Number
echo $Kundendatenarry[4];
//within the loop and the form it is not working anymore it is only shwoing the street:
echo"<form name='form1' method='post' action='KundeundAutoBearbeiten_Update.php' accept-charset='UTF-8'>";
for ($i=1, $max=$Kundendaten->FieldCount(); $i < $max-3; $i++)
{
echo"<pre><input size='50' name='name' type='text' id='name' value=".$Kundendatenarry[$i]."></pre><br>";
}
echo"<input type='submit' name='senden' value='Daten Ändern'><br>";
echo "</form>";
I thought "< pre >" could help, but it didn't .
Can anybody tell me what I did wrong?
These are the db entries:
echo $Kundendaten
ID,Titel,Vorname,Nachname,Strasse_Hausnummer,Postleitzahl,Stadt,Telefon,EMail,Kommentar,Weihnachtskarte,Erzeugt,Geaendert 11111,,Kurt,Heiz,Rumpenheimerstraße 121, 15625,Offenbach,,,,0,,
--> It is printed perfektly except of Rumpenheimerstraße 121 --> here it prints out Rumpenheimerstraße and not the 121
There is no string tags around your input value:
echo"<pre><input size='50' name='name' type='text' id='name' value=".$Kundendatenarry[$i]."></pre><br>";
should be:
echo '<input size="50" name="name" type="text" id="name" value="'.htmlspecialchars($Kundendatenarry[$i]).'"/><br>';
I've also added htmlspecialchars() too as its common practice to prevent user data breaking the html
Try using this get result first then echo that result in your html script
<?php
foreach(condition){
?>
<form name='form1' method='post' action='KundeundAutoBearbeiten_Update.php' accept-charset='UTF-8'>
<pre><input name='name' type='text' id='name' value="<?php echo $Kundendatenarry[$i] ?>"></pre><br>
<input type='submit' name='senden' value='Daten Ändern'><br>
</form>
<?php
}
//foreach ends
?>
Strange error, but you can slove using it