PHP retrieve data from database and show in text area -


i m trying display retrieved data database in earlier created text area. code supposed users nickname , input message, store both in database , retrieve text area, smth similar 1 man chat (whatever typed after clicking 'send message' should stored database , retrieved , shown in text area). able store database , display retrieved data anywhere outside text area. appreciated. thank you!

code:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> </head>  <body> <form method="post" action="postmessage.php"> <center>       <br />     <label for="output">chat history</label>     <br />   <textarea name="output" cols="100" rows="25" id="output"></textarea>     <br />    <label for="nickname" maxlength="16">nickname</label>     <br />     <input type="text" name="nickname" id="nickname" />     <br />     <label for="message" >type message here:</label>     <br />     <input name="message" type="text" id="message" size="87" /><input type="submit" value="send message" /> </form>  <?php   $connection = mysql_connect('localhost', 'root', ''); mysql_select_db('messenger', $connection);  $query = mysql_query('select * messages'); while ($entry = mysql_fetch_object($query)) {      printf("<p>%s <br />- <em>%s</em></p>",          htmlentities($entry->message),         htmlentities($entry->nickname)     );  } ?>   </body> </html> 

postmessage.php

<!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> </head> <?php $nickname=$_post['nickname']; $message = $_post['message'];  $nickname = mysql_real_escape_string($nickname); $message = mysql_real_escape_string($message);  $connection = mysql_connect('localhost', 'root', ''); mysql_select_db('messenger', $connection);  if (!mysql_query(     "insert messages (nickname, message) values ('$nickname', '$message')" )){     echo ("could not process send request");     };  ?>  <body> </body> </html> 

updated:

<?php session_start(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> </head> <body>  <form method="post" action="postmessage1.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" />     <br />     <label for="message" >type message here:</label>     <br />     <input name="message" type="text" id="message" size="87" /><input type="submit" value="send message" />     </div> </form> </body> </html> 

post..php:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> </head>  <body> <?php session_start(); $nickname = isset($_post['nickname'])?$_post['nickname']:""; $message = isset($_post['message'])?$_post['message']:"";  if(empty($nickname) || empty($message)){     // no data return form     header("location: chatform.php"); }  $mysqli = new mysqli("localhost", "root", "", "messenger"); /* create prepared statement */ if ($stmt = $mysqli->prepare("insert messages (nickname, message) values (?, ?)")) {     /* bind parameters 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 display content header("location: chatform.php"); ?> </body> </html> 

for form, suggest 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 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 post handler, this:

<?php start_session(); $nickname = isset($_post['nickname'])?$_post['nickname']:""; $message = isset($_post['message'])?$_post['message']:"";  if(empty($nickname) || empty($message)){     // no data return form     header("location: chatform.php"); }  $mysqli = new mysqli("localhost", "root", "", "messenger"); /* create prepared statement */ if ($stmt = $mysqli->prepare("insert messages (nickname, message) values (?, ?)")) {     /* bind parameters 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 display content header("location: chatform.php"); ?> 

taking further, can add timestamps via js , can use ajax post data instead of using form. drop redirects , session data. if user closes browser accident, , returns, should re-populate data if session still alive.

could compile chat , autosave db every min or instead of every chat action. adding leave button can kill session , save db @ time. lots of ways it.

code above untested.

edit after comments

for chat page, suggest changing textarea div better style control. here example of 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 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 post handler (that should not 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 form     header("location: chatform.php"); }  $mysqli = new mysqli("localhost", "root", "", "messenger"); /* create prepared statement */ if ($stmt = $mysqli->prepare("insert messages (nickname, message) values (?, ?)")) {     /* bind parameters 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 * 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 display content header("location: chatform.php"); ?> 

if me, improve me db schema. table include more columns: id, datestamp, sender, recipient, message. hope helps, , again, untested.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -