Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax: displaying notification on another page, after successfully saved data to database
    primarykey
    data
    text
    <p>I have standed out for awhile now with a thing i want to do. Thing i want to do is to display a message(confirmation, "You have earned 2 points") at the top (message like stackoverflow). </p> <p>Ive did this storing a confirmation message using session variable, and then at index.php there is a ajax script that loads session.php each 1 second, for checking if there's some message to display.</p> <p>BUT i wonder, cant this be done in another way? I mean, does it need to check each 1 second all the time</p> <p>My site is a design on index.php, and then you are browsing the rest inside a frame.</p> <p>Anyway, i already got a solution for this as you see, i think it may request too much to the server.</p> <p>So i wonder, cant this be done in another way? I mean, does it need to check each 1 second all the time?</p> <p>Isnt it possible to, if you have inserted a comment correctly, and stored session variable called user_message, then tell index.php to show the #box.</p> <p>My comment "form" is on show.php, which just contains a normal text area and a submit button. Now it stores the message through a JS script that passes it all to a string, to insert.php which inserts with normal mysql query to the database, and stores a session variable. (source code below) </p> <p>Here's some coding: session.php:</p> <pre><code>&lt;?php session_start(); if(isset($_SESSION['user_message'])) { echo 1; } ?&gt; </code></pre> <p>div box in index.php:</p> <pre><code>&lt;div id='box' onclick="closeNotice()" style="display: none"&gt; Hey, &lt;b&gt;&lt;? echo $pusername; ?&gt;&lt;/b&gt; - &lt;? echo $_SESSION["user_message"]; ?&gt; &lt;a href="#" class="close-notify" onclick="closeNotice()"&gt;X&lt;/a&gt; &lt;/div&gt; &lt;?php $_SESSION["user_message"] = null; ?&gt; </code></pre> <p>ajax script that refresh each 1 second(that i am using now):</p> <pre><code>function checkSession(){ $.ajax({url: "session.php", success: function(data){ if( data == 1){ $('#box').show(); }else{ $('#box').hide(); } }}); } setInterval('checkSession()',1000); </code></pre> <p>Script that passes string to insert.php which inserts the comment to the database:</p> <pre><code>var nocache = 0; function insert() { document.getElementById('insert_response').innerHTML = "Please Wait .. " var fID= encodeURI(document.getElementById('fID').value); var kommentar= encodeURI(document.getElementById('kommentar').value); nocache = Math.random(); http.open('get', 'insert.php?fID='+fID+'&amp;kommentar=' +kommentar+'&amp;nocache = '+nocache); http.onreadystatechange = insertReply; http.send(null); } function insertReply() { if(http.readyState == 4){ var response = http.responseText; document.getElementById('insert_response').innerHTML = ''+response; } } </code></pre> <p>The form on show.php:</p> <pre><code>&lt;div id="insert_response"&gt; &lt;/div&gt; &lt;form action="javascript:insert()" method="post"&gt; &lt;textarea id="kommentar" name="kommentar"&gt;&lt;/textarea&gt;&lt;br /&gt; &lt;input type="hidden" name="fID" id="fID" value="&lt;? echo $_GET["id"]; ?&gt;" /&gt; &lt;input type="submit" name="Submit" value="Sæt ind!"/&gt; &lt;/form&gt; </code></pre>
    singulars
    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.
 

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