how to use the nl2br() function - or addressing textarea formatting issues
the textarea does not pass line breaks to mysql - the result in the html output is one long string of text. I would like the user to input into the textarea using the enter key to create new paragraphs.
I am not a php programmer and rely on the dreamweaver data tools and developer tool kit to create my dynamic content.
Please send sample code for a form that addresses and corrects this issue. I do not know how/where to modify the code to correct this issue.
I have searched online but nowhere have I found an example of how to actually write the code. I can do a great deal of manual modification of php code but this one has me stumped for some reason..
thanks in advance !!!!
here is the index.php code which contains the form:
PHP:
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO tw_responses (id, tweet, response, permalink) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['tweet'], "text"),
GetSQLValueString($_POST['response'], "text"),
GetSQLValueString($_POST['permalink'], "text"));
mysql_select_db($database_twitter, $twitter);
$Result1 = mysql_query($insertSQL, $twitter) or die(mysql_error());
$insertGoTo = "QA.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_twitter, $twitter);
$query_Recordset1 = "SELECT * FROM tw_responses";
$Recordset1 = mysql_query($query_Recordset1, $twitter) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<?php $rs = mysql_fetch_array($resultsetquery);
$string = nl2br($rs['response']);
echo($string); ?>
FORM:
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table width="461" align="center" cellpadding="4" cellspacing="4">
<tr valign="baseline">
<td width="71" align="right" nowrap="nowrap">Question:</td>
<td width="360"><input type="text" name="tweet" value="" size="50" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right" valign="top">Answer:</td>
<td><textarea name="response" cols="50" rows="15"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" />
<input type="reset" name="Reset" id="button" value="Reset" /></td>
</tr>
</table>
<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
Here is QA.php code which contains the output:
PHP
<?php require_once('../Connections/twitter.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
mysql_select_db($database_twitter, $twitter);
$query_Recordset1 = "SELECT * FROM tw_responses ORDER BY id ASC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $twitter) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
mysql_select_db($database_twitter, $twitter);
$query_Recordset1 = "SELECT * FROM tw_responses ORDER BY id DESC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $twitter) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
OUTPUT:
<?php do { ?>
<table width="95%" border="0" cellpadding="4" cellspacing="2">
<tr>
<td><a name="<?php echo $row_Recordset1['id']; ?>"></a></td>
</tr>
<tr>
<td><p><strong><em>Question: </em></strong><a href="http://www.halopets.com/test/admin/QA.php#<?php echo $row_Recordset1['id']; ?>"><?php echo $row_Recordset1['tweet']; ?></a></p></td>
</tr>
<tr>
<td><p><strong><em>Answer: </em></strong><?php echo $row_Recordset1['response']; ?></p></td>
</tr>
<tr>
<td><hr width="95%" size="1" noshade="noshade" /><br /></td>
</tr>
</table>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
