Getting Data from a Textarea into a Database

PHP retrieve data from database and show in text area

For your form, I suggest the following:

<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="postmessage.php">
<div style="text-align: center;">
<br />
<label for="output">Chat history</label>
<br />
<textarea name="output" cols="100" rows="25" id="output"><?php if(isset($_SESSION['currentChat'])){ echo $_SESSION['currentChat']; }</textarea>
<br />
<label for="nickname" maxlength="16">Nickname</label>
<br />
<input type="text" name="nickname" id="nickname" value="<?php echo $_SESSION['nickName']; ?>" />
<br />
<label for="message" >Type your message here:</label>
<br />
<input name="message" type="text" id="message" size="87" /><input type="submit" value="Send Message" />
</div>
</form>
</body>
</html>

Then in your Post handler, do this:

<?php
start_session();
$nickname = isset($_POST['nickname'])?$_POST['nickname']:"";
$message = isset($_POST['message'])?$_POST['message']:"";

if(empty($nickname) || empty($message)){
// no data return to form
header("location: chatform.php");
}

$mysqli = new mysqli("localhost", "root", "", "messenger");
/* create a prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO messages (nickname, message) VALUES (?, ?)")) {
/* bind parameters for markers */
$stmt->bind_param("ss", $nickname, message);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();

// Update Session
$_SESSION['nickName'] = htmlentities($nickname);
$_SESSION['currentChat'] .= htmlentities($message);

// Redirect back to display content
header("Location: chatform.php");
?>

Taking this further, you can add TimeStamps via JS and you can use AJAX to post the data instead of using the form. Just drop the redirects and the session data. Also if the user closes the browser by accident, and returns, it should re-populate the data if the session is still alive.

Could also compile the chat and autosave it into DB every min or so instead of every chat action. Adding a leave button can kill the session and save everything to the DB at that time. Lots of ways to do it.

Code above is untested.

EDIT after comments

For your Chat page, I suggest changing the textarea to a div for better style control. Here is an example of the style: http://jsfiddle.net/Twisty/8frqcyyk/

<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<style>
.chatForm {
background: #ccc;
border: 1px solid #000;
width: 40em;
}

.chatForm ul {
padding: 0;
width: 30em;
margin: 5px auto;
}

.chatForm ul li {
list-style: none;
}

.chatForm ul li label {
display: block;
font: 1em Verdana, sans-serif;
}

.chatForm label.title {
background: #222;
border: 3px solid #222;
color: #FFF;
display: block;
font: 1em Verdana, sans-serif;
height: 1.5em;
margin: 0px auto;
width: 30em;
}

.center {
text-align: center;
}

.chatHistory {
background: #fff;
border: 1px inset #DDD;
display: block;
font: 1em Verdana, sans-serif;
height: 15em;
margin: 0px auto;
overflow-y: scroll;
padding: 2px;
text-align: left;
width: 30em;
}

.chatHistory p {
margin: 5px 0;
}

.chatHistory label {
display: inline-block;
font: bold 0.85em Arial, sans-serif;
width: 100px;
}

.chatHistory label.senderName {
color: blue;
}

.chatHistory label.nickName {
color: red;
}

.messageText {
width: 75%;
}

input[type='submit'] {
font: .85em Arial, sans-serif;
width: 20%;

}
</style>
</head>
<body>
<form method="post" action="postmessage.php">
<div class="chatForm">
<label for="output" class="center title">Chat History</label>
<div id="output" class="chatHistory">
<?php echo $_SESSION['currentChat']; ?>
</div>
<ul>
<li>
<label for="nickname">Nickname</label>
<input type="text" name="nickname" id="nickname" value="<?php echo $_SESSION['nickName']; ?>" />
</li>
<li>
<label for="message">Type your message here:</label>
<input name="message" type="text" id="message" class="messageText" /><input type="submit" value="Send" /></li>
</ul>
</div>
</form>
</body>
</html>

A few changes for your Post handler too (that should not be wrapped in HTML):

<?php
session_start();
$nickname = isset($_POST['nickname'])?$_POST['nickname']:"";
$message = isset($_POST['message'])?$_POST['message']:"";

if(empty($nickname) || empty($message)){
// no data return to form
header("location: chatform.php");
}

$mysqli = new mysqli("localhost", "root", "", "messenger");
/* create a prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO messages (nickname, message) VALUES (?, ?)")) {
/* bind parameters for markers */
$stmt->bind_param("ss", $nickname, $message);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}

// Update Session
$_SESSION['nickname'] .= htmlentities($nickname);
$results = $mysqli->query("SELECT * FROM messages");
$updateChat = "";
while($row = $results->fetch_assoc()){
$updateChat .= "<p><label>". htmlentities($row['nickname'] .":</label>";
$updateChat .= htmlentities($row['message']) . "</p>\r\n";
}
$results->free();
$mysqli->close();
$_SESSION['currentChat'] = $updateChat;
// Redirect back to display content
header("Location: chatform.php");
?>

If it were me, I would improve me DB schema. The table could include more columns: ID, DateStamp, Sender, Recipient, Message. Hope that helps, and again, untested.

How to retrieve data and display in the textarea of view after Search the data in Codeigniter

Use the following code. It works...

Controller:

 public function searchcus() {
$pgcode = $this->input->post('search');
if (isset($pgcode) && !empty($pgcode)) {
$data['customerr'] = $this->Ordering_model->search($pgcode);
$this->load->view('viewpage', $data);
} else {
redirect($this->index());
}
}

Model:

   function search($pgcode) {
$this->db->select('*');
$this->db->from('customer');
$this->db->like('code',$pgcode);
return $this->db->get()->result();
}

View page:

<div class="container">
<form method="post" action="<?php echo base_url(); ?>index.php/welcome/searchcus">
<div class="col-md-6">
<div class="form-group">
<div class="col-md-9">
<input type="text" class="form-control" name="search" id="search" placeholder="<?php echo "Customer's PG Code";?> " required />
</div>
<span class="input-group-btn">
<button class="btn btn-default" type="submit" name="submit" value="Search" ><span class="glyphicon glyphicon-search"><?php echo "Search"; ?></span></button>
</span>
</div>
</div>
</form><br/>
</div>
<?php if(empty($customerr)) { } else { foreach($customerr as $row) { ?>
<table style="width: 2000px;" class="table table-striped">
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon">Name</span>
<input id="msg" type="text" class="form-control" name="msg" value="<?php echo $row->name ?>" >
</div>
</div>
<br><br>
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon">Tel</span>
<input id="msg" type="text" class="form-control" name="msg" value="<?php echo $row->telno ?>" >
</div>
</div>
<br><br>
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon">Introducer</span>
<input id="msg" type="text" class="form-control" name="msg" value="<?php echo $row->introducer ?>" >
</div>
</div>
<br><br>
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon">Address</span>
<textarea class="form-control" rows="5" id="address"> <?php echo $row->address ?></textarea>
</div>
</div>
<br>


</table>
<?php } } ?>

sending data from textarea to mysql

<textarea id="textarea" maxlength="1000" name="summary" value="<?php echo $user_data['summary']; ?>"></textarea>

Change that line to:

<textarea id="textarea" maxlength="1000" name="summary" ><?php echo $user_data['summary']; ?></textarea>


Related Topics



Leave a reply



Submit