PDA

View Full Version : Problem with getting Rich TEXT editor to post to SQL



Inky1231
04-30-2012, 07:49 PM
1) Script Title:
Rich Text Editor
2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex16/richtexteditor/index.htm
3) Describe problem:
I have a PHP form connected to an MSSQL table. the page works great except our user don't know HTML and they want to be able to format the information that gets entered into the form and thus into the SQL. I can get the Richtext Editor to show on the page and I can get it to retrieve the information but I can not get it to update the SQL and I don't know what I am doing wrong. any help would be appreciated.

Here is the code: Sorry I can't let you go directly to the site as you need to be an authorized user.


<?php

include ("main_fns.php"); // This file would include your database access and login functions, include this at the top of EVERY page, in order to carry the session between pages and access the database.
open_db(); // Open a database connection

$page_file = "sample.php"; // the file name of this page
$page_title = "Course Admin Area"; // can be fed into the header

include ("header.php");

// Check for logged in session variable, else prompt for login.
if (!@$_SESSION["email"]) {
loginPrompt(@$_POST["email"],$_SESSION["error"]);
} else {

// PAGE CONTENT GOES HERE

function fromhtml ($x) {
$x = preg_replace("/<p>/i","\n\n",$x);
$x = preg_replace("/<br>/i","\n",$x);
$x = preg_replace("/<li>/i","\n<li>",$x);
return $x;
}

//$PHP_SELF = $_SERVER['PHP_SELF'];
//$PHP_AUTH_USER = $_SERVER['PHP_AUTH_USER'];

//$DBhost = "localhost";
//$DBName = "courses";
//
//mssql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
//@mssql_select_db("$DBName") or die("Unable to select database $DBName");


$DBhost = "localhost";
$DBuser = "daccweb";
$DBpass = "go";
$DBName = "DACC";
mssql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mssql_select_db("$DBName") or die("Unable to select database $DBName");


$PHP_SELF = $_SERVER['PHP_SELF'];


$course_id = @$_GET["course"];

if ($course_id == "") {
//$sqlquery = "SELECT id,title,goals,outline,reference,DATE_FORMAT(updated, '%Y/%m/%d %H:%i:%S') as updated2 FROM courses WHERE division_id = '$division_id' ORDER BY id";
$sqlquery = "SELECT id, title, goals, outline, reference, deliverymode, updated2 = CONVERT(VARCHAR(19), updated, 120)
FROM courses WHERE division_id = '$division_id' ORDER by id";


$result = mssql_query($sqlquery);
$number = mssql_num_rows($result);

if ($number == 0) {
print "<html><body>";
print "No courses exist for division $division_id.";
print "</body></html>";
exit;
}

print "<b>Course list - division $division_id</b><p>\n";
print "Select a course ID to edit course information.<p>\n";
print "<table border=1 id=\"content_table\"><tr><th>ID</th><th>Title</th><th>Status</th><th>Last modified</th></tr>\n";

$i = 0;
while ($number > $i) {

$course_id = mssql_result($result,$i,"id");
$reference = mssql_result($result,$i,"reference");
$updated = mssql_result($result,$i,"updated2");

print "<tr><td>";
if ($reference == "") {
print "<a href=$PHP_SELF?division=$division_id&course=$course_id>$course_id</a>";
} else {
print "$course_id";
}
print "</td><td>";
print mssql_result($result,$i,"title");
print "</td><td>";
if ($reference == "") {
if ( (mssql_result($result,$i,"goals")=="") and (mssql_result($result,$i,"outline")=="") ) {
print "<b>No syllabus, or incomplete</b></td>";
}
} else {
print "Based on $reference";
}
print "</td><td>$updated</td>\n";
print "</tr>\n";
$i++;
}

print "</table>";

exit;
}

$sqlquery = "SELECT * FROM courses WHERE id = '$course_id'";

$result = mssql_query($sqlquery);
$number = mssql_num_rows($result);

if ($number == 0) {
print "<html><body>";
print "No course with the ID \"$course_id\" exists in the course database.";
print "</body></html>";
exit;
}

$i = 0;
$title = mssql_result($result,$i,"title");
$iai = mssql_result($result,$i,"iai");
$deliverymode = mssql_result($result,$i,"deliverymode");
$hours = mssql_result($result,$i,"hours");
$type = mssql_result($result,$i,"type");
$description = mssql_result($result,$i,"description");
$semester = mssql_result($result,$i,"semester");
$prerequisites = mssql_result($result,$i,"prerequisites");
$notes = mssql_result($result,$i,"notes");
$goals = fromhtml(mssql_result($result,$i,"goals"));
$outline = fromhtml(mssql_result($result,$i,"outline"));
$materials = fromhtml(mssql_result($result,$i,"materials"));
$evaluation = fromhtml(mssql_result($result,$i,"evaluation"));
$bibliography = fromhtml(mssql_result($result,$i,"bibliography"));
$revised = mssql_result($result,$i,"revised");

print "<html><head><title>DACC Course Syllabus - $course_id</title>";

print "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/rte/richtext.js\"></script>";

print "</head><body>\n";
?>

<script language="JavaScript" type="text/javascript">
<!--
initRTE("/rte/images/", "/rte/", "");
//-->

function submitForm() {
updateRTEs();
document.edit-release.submit();
return false;
}

</script>

<?php


print "<a href=/courses/$division_id>Back to the $division_id course list</a>";

//Begin Form
print "<form name=\"edit-course\" id=\"edit-form\" method=\"post\" action=\"write.php?course=$course_id\ onSubmit=\"return submitForm()\">\n";

print "<h3>Course Number:</h3><br />$course_id\n";

print "<fieldset>";
print "<label for=\"title\">Course Title</label>";
print "<INPUT TYPE=\"text\" NAME=\"title\" SIZE=75 MAXLENGTH=75 VALUE=\"$title\"><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"deliverymode\">Delivery Mode</label>";

?>

<script language="JavaScript" type="text/javascript">
<!--
//Usage: writeRichText(fieldname, html, width, height, buttons)
writeRichText('deliverymode', '<?php print $deliverymode; ?>', 300, 200, true, false);
//-->
</script>

<?php
//print "<TEXTAREA ROWS=5 COLS=80 NAME=\"deliverymode\">$deliverymode</TEXTAREA><p>\n";
print "</fieldset>";


print "<fieldset>";
print "<label for=\"semester\">Semester</label>";
print "<INPUT TYPE=\"text\" NAME=\"semester\" SIZE=30 MAXLENGTH=30 VALUE=\"$semester\"><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"iaicode\">IAI Code</label>";
print "<INPUT TYPE=\"text\" NAME=\"iai\" SIZE=75 MAXLENGTH=75 VALUE=\"$iai\"><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"hours\">Semester Credit Hours</label>";
print "<INPUT TYPE=\"text\" NAME=\"hours\" SIZE=50 MAXLENGTH=50 VALUE=\"$hours\"><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"description\">Course Description</label>";
print "<TEXTAREA ROWS=10 COLS=80 WRAP=\"soft\" NAME=\"description\">$description</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"prerequisites\">Prerequisites</label>";
print "<TEXTAREA ROWS=2 COLS=80 WRAP=\"soft\" NAME=\"prerequisites\">$prerequisites</TEXTAREA><p>\n";
#print "$prerequisites<p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"notes\">Notes</label>";
print "<TEXTAREA ROWS=2 COLS=80 WRAP=\"soft\" NAME=\"notes\">$notes</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"type\">Type (\"T\", \"C\", or \"T, C\")</label>";
print "<INPUT TYPE=\"text\" NAME=\"type\" SIZE=5 MAXLENGTH=5 VALUE=\"$type\"><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"goals\">Course Objectives/Goals</label>";
print "<TEXTAREA ROWS=10 COLS=80 WRAP=\"soft\" NAME=\"goals\">$goals</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"outline\">Topical Outline</label>";
print "<TEXTAREA ROWS=10 COLS=80 NAME=\"outline\">$outline</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"materials\">Textbook/Special Materials</label>";
print "<TEXTAREA ROWS=5 COLS=80 NAME=\"materials\">$materials</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"evaluation\">Evaluation</label>";
print "<TEXTAREA ROWS=10 COLS=80 NAME=\"evaluation\">$evaluation</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"bibliography\">Bibliography</label>";
print "<TEXTAREA ROWS=10 COLS=80 NAME=\"bibliography\">$bibliography</TEXTAREA><p>\n";
print "</fieldset>";

print "<fieldset>";
print "<label for=\"revised\">Revision</label>";
print "<INPUT TYPE=\"text\" NAME=\"revised\" SIZE=50 MAXLENGTH=50 VALUE=\"$revised\"><p>\n";
print "</fieldset>";


print "<input type=submit value=\"Update Record\" name=\"submit\">\n";



function rteSafe($strText) {
//returns safe code for preloading in the RTE
$tmpString = $strText;

//convert all types of single quotes
$tmpString = str_replace(chr(145), chr(39), $tmpString);
$tmpString = str_replace(chr(146), chr(39), $tmpString);
$tmpString = str_replace("'", "&#39;", $tmpString);

//convert all types of double quotes
$tmpString = str_replace(chr(147), chr(34), $tmpString);
$tmpString = str_replace(chr(148), chr(34), $tmpString);
// $tmpString = str_replace("\"", "\"", $tmpString);

//replace carriage returns & line feeds
$tmpString = str_replace(chr(10), " ", $tmpString);
$tmpString = str_replace(chr(13), " ", $tmpString);

return $tmpString;
}


print "</form>\n";

print "</body></html>";


print "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
}
print "</div>";//end indent

include ("footer.php");

?>