Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing HTML tags with Javascript / jQuery
    primarykey
    data
    text
    <p>I have some code that is working but I would like to add two things to it that I don't know how to do. </p> <p>It's a simple form that send data to my database (jQuery / AJAX) and then display the data on the web page. </p> <p>Problem 1:<br> When I refresh the page, all the message are displayed on separate lines (which is what I want) but when I add new messages and click on the submit button, all the new messages get displayed on the same line. Is there a way I could put a <br /> or force the comment to be displayed on a new line ?</p> <p>Problem 2: When I refresh index.php, only 10 messages are displayed and that's good because I used a "LIMIT 0, 10" on my query. When I type a new message and click on the button to submit to the database, the message list is "updating" by itself but ADD a new message to the list instead of replacing the 10th message by the new one.</p> <p>index.php </p> <pre><code>&lt;html&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;head&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;script src="jquery-1.10.2.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function sendMessage() { $.ajax({ url: "ajax.php", type: "POST", data: {comment : document.getElementById('message').value}, dataType:"html", success: function(data, textStatus, xhr) { $("#output").append(document.getElementById('message').value); document.getElementById('message').value = ""; } }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="addComment"&gt; &lt;input type="text" name="message" id="message" /&gt; &lt;input type="button" name="submitBtn" id="submitBtn" value="Envoyer" onclick="sendMessage();" /&gt; &lt;/form&gt; &lt;div id="output"&gt; &lt;/div&gt; &lt;?php include("class.php"); $object1 = new Shootbox(); try { $object1-&gt;showMessage(); } catch(Exception $e) { echo "Exception : ".$e-&gt;getMessage(); } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>ajax.php </p> <pre><code>&lt;?php mysql_connect("localhost", "root", "root"); mysql_select_db("PFtest"); $message = $_POST['comment']; if (strlen($message) &gt; 0) { mysql_query("INSERT INTO comments VALUES ('NULL', '".$_POST['comment']."', CURRENT_TIMESTAMP)"); echo "Message enregistré !"; } elseif(strlen($message) &lt; 0) { echo "Nothing here"; } ?&gt; </code></pre> <p>class.php </p> <pre><code>&lt;?php class Shootbox { public function showMessage() { mysql_connect("localhost", "root", "root"); mysql_select_db("PFtest"); $query = mysql_query("SELECT * FROM comments ORDER BY date DESC LIMIT 0 , 10"); if(mysql_num_rows ($query)&gt;0){ while($result = mysql_fetch_object($query)){ echo $result-&gt;message."&lt;br /&gt;"; } } else{ throw new Exception("Aucune donnée dans la base de données"); } } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload