hi

i am trying to, on a button click, display data retrieved from mysql database in a text area but having no success. Below is the code i used. All help given is appreciated

print "<center><TEXTAREA NAME=news ID=news COLS=70 ROWS=10>".$news."</TEXTAREA></center>";

if(isset($_POST['show2'])){
		$cn = connect_db();
		$cn_sql = "SELECT message from messageboard";
		$con_res = mysql_query($cn_sql);
		$news = $con_res;

Dani AI

Generated

Two quick facts that explain why you saw nothing: the textarea in your first post was printed before the database result was turned into a string, and the raw DB result/resource isn’t the message text you can drop straight into a textarea. You have two realistic ways to fix this: submit the form and let PHP re-render the page with the fetched text, or request the text from the server with JavaScript and fill the textarea without reloading. and were right about the AJAX option; was right that the query result needs fetching into a usable value.

Server-side (simple, safe): fetch the message rows, join them into one string, escape for HTML, then output into the textarea on the server-rendered page. The example below uses PDO and htmlspecialchars so it’s safe on modern PHP.

// server-side: fetch messages and build a single string
$stmt = $pdo->query('SELECT message FROM messageboard');
$rows = $stmt->fetchAll(PDO::FETCH_COLUMN);
$news = implode("\n\n", $rows);
?>
<textarea id="news" name="news"><?php echo htmlspecialchars($news, ENT_QUOTES, 'UTF-8'); ?></textarea>

AJAX (no reload): call a tiny endpoint that returns plain text (no HTML wrapper), then set textarea.value to the response. Use fetch() and have the endpoint return UTF-8 text; avoid any extra whitespace or BOM before the response.

fetch('messages.php')
  .then(r => r.text())
  .then(t => document.getElementById('news').value = t)
  .catch(console.error);

Troubleshooting notes: ensure the DB query actually returns rows, make sure the textarea id matches your JS, and always escape output to prevent XSS (see the htmlspecialchars usage). Also avoid old deprecated extensions — prefer PDO or mysqli with prepared statements for safety and compatibility. Finally, ’s short-echo approach is fine only after you’ve built and escaped the $news string on the server.

Recommended Answers

All 5 Replies

PHP is not a language you can use to modify a page's content after it has been loaded on the client side.

To do this you might want to try AJAX but i'm no real expert with it.

Otherwise, you could figure out something with an iframe where you would load a php script querying the mysql database and then retrieving its content with javascript but that's far fetch I believe.

hi

i am trying to, on a button click, display data retrieved from mysql database in a text area but having no success. Below is the code i used. All help given is appreciated

print "<center><TEXTAREA NAME=news ID=news COLS=70 ROWS=10>".$news."</TEXTAREA></center>";

if(isset($_POST['show2'])){
		$cn = connect_db();
		$cn_sql = "SELECT message from messageboard";
		$con_res = mysql_query($cn_sql);
		$news = $con_res;

Hi,,
we can write as....


<textarea name="txt_addr" >
<?php echo $rows["address"]; ?>
</textarea>

Ajax is the way to do this. You need to send the form to a php script with ajax and then have it return the data to the textarea.

Here is a simple ajax request to get you started:

function ajaxHandler(endParams, returnId)
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try
		{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} 
	catch (e)
		{
			// Internet Explorer Browsers
			try
				{
					ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} 
			catch (e) 
				{
					try
						{
							ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} 
					catch (e)
						{
							// Something went wrong
							alert("Your browser broke!");
							return false;
						}
				}
		}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
				{
					document.getElementById(returnId).innerHTML = ajaxRequest.responseText;
				}
		}
	
	ajaxRequest.open("GET", "pageThatProcessesRequest.php" + endParams, true);
	ajaxRequest.send(null); 
}

then you just add an 'onclick='ajaxHandler("?getVar=something", "idToReturnDataTo");' to the button and it will return the data to the id that matches 'idToReturnDataTo' as .innerHTML (may not be the best for textareas, if theres a problem change the .innerHTML to .value .

How does this SQL work?
did you try doing var_dump($news);

cn = connect_db();
		$cn_sql = "SELECT message from messageboard";
		$con_res = mysql_query($cn_sql);
		$news = $con_res;

Assuming that you wrote your own db connect somewhere and it is correct,
Your out put is a mysql resouce, an probably an array since you are selecting message which will return ALL of the messages.
the resource needs to be passed into:

$news = mysql_fetch_array($con_res);

which will convert this into a PHP readable array that can be read out.
If connect_db(); is calling the php mysql functions and is connecting to a selected database, then all you need to do is call it;:

connect_db();

I think you should review all of the MySQl related code as well.

Try this..
<TEXTAREA NAME=news ID=news COLS=70 ROWS=10><?= $news ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.