PHP form adds a <br> in front of the text

General discussions related to php

Moderators: macek, egami, gesf

Inky1231
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Jan 19, 2012 8:01 am

PHP form adds a <br> in front of the text

Postby Inky1231 » Thu Sep 13, 2012 7:25 am

I need help! I am using a PHP editing form (previous person built) that submits data into an MSSQL database this works fine however when the information is submitted into the database and later retrieved the the form page (for editing) there is a < BR> at the beginning of the text. If the fields start with a < ul> that < br> is not inserted. I have been trying to figure out how to stop this. One thing I tried ends up removing all < br> in the text which makes the formatting disappear, How can I keep it from inserting the < br> at the top but keeping the < br> everywhere else? here is the code I have: ( I am giving you everything up to the first field where I have the issue appear) Any help would be appreciated!


Code: Select all

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'];


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

if ($course_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);

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;

$description = fromhtml(mssql_result($result,$i,"description"));
print "<html><head><title>DACC Course Syllabus - $course_id</title>";

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

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

function submitForm() {
   updateRTEs();
   document.edit-course.submit();
   return false;
}
</script>
<?php   
print "</head><body>\n";

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


print "<fieldset>";
 print "<label for=\"description\">Course Description</label>";
$description=addslashes(preg_replace('`[\r\n]`','',$description));
  ?>
   <script language="JavaScript" type="text/javascript">
<!--
  writeRichText('description', '<?php print $description; ?>', 200, 300, true, false);
//-->
</script>
  <?php

   print "</fieldset>";


Got a solution: I needed to replace

Code: Select all

$x = preg_replace("/<br>/i","\n",$x);
with

Code: Select all

$x = preg_replace("/^<br(\/|)>/i","\n",$x);

Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 3 guests

cron