gpt4 book ai didi

php - 如何使用nl2br使用php获取textarea的数据?

转载 作者:行者123 更新时间:2023-11-29 22:15:25 25 4
gpt4 key购买 nike

我是 php 新手。我有一个网络表格。我在其中使用文本区域。我有另一个 PHP 页面,我在其中显示来自数据库的数据并应用 CRUD 操作。现在,当我单击“编辑”按钮时,问题是我的所有表单数据已从数据库中获取,但文本区域文本的格式已更改。它显示 \r\r\r\n 我使用 nl2br 但它不起作用。我想以相同的格式显示我的数据。

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

我的整页代码是

<?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>

最佳答案

您应该更改您的 php echo 代码,因为您不需要包含 !empty 验证。如果这样做,则必须在回显文本框之前创建它。我建议你将代码更改为:

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

您还可以在这里找到 nl2br 的一些文档:

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

关于php - 如何使用nl2br使用php获取textarea的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com