简体   繁体   中英

How to use nl2br to fetch data of textarea using php?

I am new in php. I have a web form. In which i use textarea. I have another PHP page where I display data from db and applying CRUD operation. Now here is the problem when I click on Edit button my all form data has been fetched from db but the formatting of textarea Text has been changed. It shows \\r\\r\\r\\n I use nl2br but its not working. I want to display my data in same formating.

<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo !empty(nl2br($text))?(nl2br($text)):'';?> </textarea><td>

My Full Page Code is

<?php
    require 'database.php';
    $id = null;
    if ( !empty($_GET['id'])) {
        $id = $_REQUEST['id'];
    }

    if ( null==$id ) {
        header("Location: index.php");
    }

    if ( !empty($_POST)) {
        // keep track post values
        $file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
$text = mysql_real_escape_string( $_POST['text'] );


    // update data
    $valid = true;
        if ($valid) {
            $pdo = Database::connect();
          //  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE test  set file_name ='$file_name', ref_no ='$ref_no', to_name ='$to_name',
                                        confidential ='$confidential', designation = '$designation', date ='$date',
                                        solutation ='$solutation', entity ='$entity', add_1 ='$add_1',
                                        thank_you ='$thank_you', add_2 ='$add_2', yours_truly ='$yours_truly',
                                        add_3 ='$add_3', sign_name ='$sign_name', city ='$city',
                            s_designation ='$s_designation', heading_line_1 ='$heading_line_1', encl_line_1 ='$encl_line_1',
                            heading_line_2 ='$heading_line_2', encl_line_2 ='$encl_line_2', heading_line_3 ='$heading_line_3',
                            encl_line_3 ='$encl_line_3', text ='$text'  WHERE id ='$id'";
            $q = $pdo->prepare($sql);
            $q->execute(array($file_name,$ref_no,$to_name,$confidential,$designation,$date,$solutation,$entity,$add_1,
            $thank_you,$add_2,$yours_truly,$add_3,$sign_name,$city,$s_designation,$heading_line_1,$encl_line_1,$heading_line_2,
            $encl_line_2,$heading_line_3,$encl_line_3,$id));
            Database::disconnect();
            header("Location: index.php");
        }
     else {
          }
    }
         $con=mysqli_connect("localhost","MY_LOGIN","MY_PASSWORD","MY_DATABASE");

         $id2 = $_GET['id'];
        $sql = "SELECT * FROM test where id='$id2'";

            $result=mysqli_query($con,$sql);

            $row= (mysqli_fetch_array($result,MYSQLI_ASSOC));


        $file_name = $row['file_name'];
$ref_no = $row['ref_no'];
$to_name = $row['to_name'];
$confidential = $row['confidential'];
$designation = $row['designation'];
$date = $row['date'];
$solutation = $row['solutation'];
$entity = $row['entity'];
$add_1 = $row['add_1'];
$thank_you = $row['thank_you'];
$add_2 = $row['add_2'];
$yours_truly = $row['yours_truly'];
$add_3 = $row['add_3'];
$sign_name = $row['sign_name'];
$city = $row['city'];
$s_designation = $row['s_designation'];
$heading_line_1 = $row['heading_line_1'];
$encl_line_1 = $row['encl_line_1'];
$heading_line_2 = $row['heading_line_2'];
$encl_line_2 = $row['encl_line_2'];
$heading_line_3 = $row['heading_line_3'];
$encl_line_3 = $row['encl_line_3'];
$text = mysql_real_escape_string( $row['text'] );

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="newstyles.css">
    <script src="js/bootstrap.min.js"></script>
    <script>
    function show_confirm(){
        return confirm("Copy is being created....");
        window.location.href='index.php';
    }
</script>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script>
$(document).ready(function() {
    $("#date").datepicker({
    dateFormat: "yy-mm-dd"
    }).datepicker();
});   

</script>

</head>

<body>
<form action="edit4.php?id=<?php echo $id; ?>" method="POST" > 
 <table border="0" class="DivTableBorder" width="840px">
      <tr>
        <td class="DivSubHeaderCellTop"  colspan="6">Letters</td>
</tr> <tr>  </td>  </tr>
  <tr>
   <td class="DivCellText" width="80px">File Name </td>
    <td class="DivCellText" width="480px" colspan="3"><input name="file_name" type="text" id="file_name" 
    value="<?php echo !empty($file_name)?$file_name:'';?>" class="inputRemarks"  />
    </td>
      <td class="DivCellText" width="80px">Referance #</td>
    <td class="DivCellText" width="200px"><input name="ref_no" type="text" id="ref_no" 
    value="<?php echo !empty($ref_no)?$ref_no:'';?>" class="inputRemarks"  />
    </td> </tr>

<tr ><td bgcolor="#999999" colspan="4"></td>
 <td class="DivCellText" width="80px"></td>
    <td class="DivCellText" width="200px"></td>
</tr>
  <tr>
   <td class="DivCellText" width="80px">To - Name</td>
    <td class="DivCellText" colspan="3"><input name="to_name" type="text" id="to_name" 
    value="<?php echo !empty($to_name)?$to_name:'';?>" class="inputRemarks"  />
    </td>
<td class="DivCellText" width="80px">Confidential</td>
    <td class="DivCellText" width="200px">
    <?php if($confidential == "on"){ ?>
    <input name="confidential" type="checkbox" checked="checked" id="confidential" value="on"  />
    <?php }else{ ?>
    <input name="confidential" type="checkbox" id="confidential" value="on"  />
    <?php } ?>

    </td> </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Designation</td>
    <td class="DivCellText" colspan="3"><input name="designation" type="text" id="designation" 
    value="<?php echo !empty($designation)?$designation:'';?>" class="inputRemarks"  />
    </td>
     <td class="DivCellText" width="80px">Date :</td>
    <td class="DivCellText" width="200px">
            <input name="date" type="text" id="date" value="<?php echo $date; ?>" />
    </td> </tr>

  <tr>
   <td class="DivCellText" >&nbsp;</td>
    <td class="DivCellText" colspan="3">&nbsp;</td>
    <td class="DivCellText" width="80px">Solutation</td>
    <td class="DivCellText" width="200px" >

    <select name='solutation' id='solutation' size='1' STYLE='width: 95%' value="<?php echo !empty($solutation)?$solutation:'';?>" >
    <option value='Others' >[--Others--]</option>
    <option value='Dear Sir' >Dear Sir</option>
    <option value='Madam' >Madam</option>
    </select>
    </td> </tr>

  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Entity</td>
    <td class="DivCellText" colspan="3"><input name="entity" type="text" id="entity" 
    value="<?php echo !empty($entity)?$entity:'';?>" class="inputRemarks"  />
    </td>
    <td class="DivCellText" width="80px">&nbsp;</td>
    <td class="DivCellText" width="200px" >

    <input name="txtSolutation" type="text" id="txtSolutation" 
    value="" class="inputRemarks"  />
    </td> </tr>

  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add-1</td>
    <td class="DivCellText" colspan="3"><input name="add_1" type="text" id="add_1" 
    value="<?php echo !empty($add_1)?$add_1:'';?>" class="inputRemarks"  />
    </td>
         <td class="DivCellText" width="80px">Thank You.</td>
    <td class="DivCellText" width="200px" ><input name="thank_you" type="text" id="thank_you" 
    value="<?php echo !empty($thank_you)?$thank_you:'';?>" class="inputRemarks"  />
    </td> 
  </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add-2</td>
    <td class="DivCellText" colspan="3"><input name="add_2" type="text" id="add_2" 
    value="<?php echo !empty($add_2)?$add_2:'';?>" class="inputRemarks"  />
    </td>
         <td class="DivCellText" width="80px">Yours truly</td>
         <td class="DivCellText" width="200px" >
    <select name='yours_truly' id='yours_truly' size='1' STYLE='width: 95%' value="<?php echo !empty($yours_truly)?$yours_truly:'';?>" >
    <option value='1' >Yours truly</option>
    <option value='2' >Regards</option>
    </select>

    </td> </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add-3</td>
    <td class="DivCellText" colspan="3"><input name="add_3" type="text" id="add_3" 
    value="<?php echo !empty($add_3)?$add_3:'';?>" class="inputRemarks"  />
    </td>
    <td class="DivCellText" width="80px">Signature-Name</td>
    <td class="DivCellText" width="200px" >

    <select name='sign_name'  id='sign_name' size='1' style='width:95%' value="<?php echo !empty($sign_name)?$sign_name:'';?>"> 
        <option value='1' >Adnan Afaq</option>
        <option value='2' >Muhammad Shahzad Saleem</option>
        <option value='3' >Adnan Dilawar</option>
        <option value='4' >Rana Muhammad Nadeem</option>
        <option value='5' >Jhangeer Hanif</option>
    </select>
    </td> </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;City</td>
    <td class="DivCellText" colspan="3"><input name="city" type="text" id="city" 
    value="<?php echo !empty($city)?$city:'';?>" class="inputRemarks"  />
    </td>
    <td class="DivCellText" width="80px">S-Designation</td>
    <td class="DivCellText" width="200px">
    <select name='s_designation' id='s_designation' size='1' STYLE='width: 95%' value="<?php echo !empty($s_designation)?$s_designation:'';?>" >
    <option value='1' >Managing Director</option>
    <option value='2' >Chief Operating Officer</option>
    <option value='3' >Manager Ratings</option>
    <option value='4' >Unit Head Ratings</option>
    </select>
    </td>
  </tr>
  <tr>
   <td class="DivCellText" width="80px">Heading Line-1</td>
    <td class="DivCellText" width="480px" colspan="3"><input name="heading_line_1" type="text" id="heading_line_1" 
    value="<?php echo !empty($heading_line_1)?$heading_line_1:'';?>" class="inputRemarks"  maxlength="55"/>
    </td>
     <td class="DivCellText" width="80px">Encl: Line-1</td>
     <td class="DivCellText" width="200px" >
     <input name="encl_line_1" type="text" id="encl_line_1" value="<?php echo !empty($encl_line_1)?$encl_line_1:'';?>" class="inputRemarks"  />
    </td>
  </tr>  
  <tr>
   <td class="DivCellText" width="80px">Heading Line-2</td>
    <td class="DivCellText" width="480px" colspan="3"><input name="heading_line_2" type="text" id="heading_line_2" 
    value="<?php echo !empty($heading_line_2)?$heading_line_2:'';?>" class="inputRemarks"    maxlength="55" />
    </td>
     <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line-2</td>
     <td class="DivCellText" width="200px" >
     <input name="encl_line_2" type="text" id="encl_line_2" value="<?php echo !empty($encl_line_2)?$encl_line_2:'';?>" class="inputRemarks"  />
  </tr>  
  <tr>
   <td class="DivCellText" width="80px">Heading Line-3</td>
    <td class="DivCellText" width="480px" colspan="3"><input name="heading_line_3" type="text" id="heading_line_3" 
    value="<?php echo !empty($heading_line_3)?$heading_line_3:'';?>" class="inputRemarks"    maxlength="55" />
    </td>
     <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line-3</td>
    <td class="DivCellText" width="200px">
         <input name="encl_line_3" type="text" id="encl_line_3" value="<?php echo !empty($encl_line_3)?$encl_line_3:'';?>" class="inputRemarks"  />
    </td>
  </tr>
<tr ><td bgcolor="#999999" colspan="6"></td></tr>
<tr ><td colspan="6">
 <table  border="0" class="DivTableBorder" width="840px">
 <tr>
<td class="DivCellText" colspan="4">
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo !empty(nl2br($text))?(nl2br($text)):'';?> </textarea><td>
 </tr>
<tr>
<td width="100"><input type="submit" name="submit" value="Create Copy" onclick="return show_confirm();" class="blueButton"></input></td>
<td width="100"><input type="reset" name="reset" value="Cancel" class="blueButton"  /> </td>
<td width="100">

  <input type="submit" name="submit" value="Save" class="blueButton"></input>
   </td>
<td width="303">  <input type="submit" name="submit" value="Update" class="blueButton"> </input> </td>
<td width="209">
                          <a class="btn" href="index.php">Back</a> </td>
</tr>

</table>




</form>

<?php

$_POST['submit']="";

if($_POST['submit'] == "Create Copy"){
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("pacra1", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];

$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
$text = mysql_real_escape_string( $_POST['text'] );
//$txtTitle = mysql_real_escape_string( $_POST['txtTitle'] );
//$txtRational = $_POST['txtRational'];


//Insert Query of SQL
$query = mysql_query("INSERT INTO test(file_name, ref_no, to_name, confidential, designation, date, solutation, entity, add_1, thank_you, add_2, yours_truly, add_3, sign_name, city, s_designation, heading_line_1, encl_line_1, heading_line_2, encl_line_2, heading_line_3, encl_line_3, text) 

                                        values 

    ('$file_name', '$ref_no', '$to_name', '$confidential', '$designation', '$date', '$solutation', '$entity', '$add_1', '$thank_you', '$add_2', '$yours_truly', '$add_3', '$sign_name', '$city', '$s_designation', '$heading_line_1', '$encl_line_1', '$heading_line_2', '$encl_line_2', '$heading_line_3', '$encl_line_3', '$text')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}

//mysql_close($connection); // Closing Connection with Server

}
?>

</body>
</html>

You should change your php echo code as you dont need to include the !empty verification. If you do then you will have to create that before you echo the textbox. I suggest you change your code to this:

<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo nl2br($text);?> </textarea><td>

You can also find some documentation of the nl2br here :

http://php.net/manual/en/function.nl2br.php

Upon inserting to the database, apply the nl2br - as you are likely to be presenting that data as text on a page more often than an textarea.

$mysql->real_escape_string(trim(nl2br($_POST['text'])))

Where $mysql is the mysqli database connection

Disclaimer As Bartdude mentioned, this is not especially good practice, since the information in the database should be as free from tags as possible - however, if this data is to be used in HTML pages only, then this solution should work.

Then when returning the data to a textarea, simply remove the <br> tags, the newline characters will be there anyway and should be interpreted by the browser correctly.

str_replace("<br>", " ", $row['text'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM