Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Change this code:</p> <pre><code>jQuery('form').ajaxForm({ target: '#noteReturn', success: function() { $('#noteReturn').fadeIn('slow'); } }); </code></pre> <p>To this:</p> <pre><code>jQuery('form').ajaxForm({ target: '#noteReturn', dataType: 'json', success: function(data) { $('#noteReturn' + data.id).html(data.note).fadeIn('slow'); } }); </code></pre> <p>And this code:</p> <pre><code>&lt;?php $note = $_POST['note']; $id = $_POST['bid']; $sql = "INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')"; $result = mysql_query( $sql ); if($result) { echo " $note"; } ?&gt; </code></pre> <p>To this:</p> <pre><code>&lt;?php $note = mysql_real_escape_string($_POST['note']); $id = mysql_real_escape_string($_POST['bid']); $sql = "INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')"; $result = mysql_query( $sql ); if($result) { print json_encode(array("id" =&gt; $id, "note" =&gt; $note)); } ?&gt; </code></pre> <h2>What happened?</h2> <p>The change to the PHP code is making use of PHP's <a href="http://us3.php.net/json_encode" rel="nofollow noreferrer"><code>json_encode</code></a> function to print out the id of the business to which the note was added as well as the actual note text. In the javascript code, I added the <code>dataType</code> of 'json' to tell the script what format of response to expect. Once the request is received in the <code>success</code> callback, the <code>data</code> variable is an object with the values we passed through <code>json_encode</code>. So <code>data.id</code> has the business id and <code>data.note</code> has the new note. Using jQuery's <a href="http://docs.jquery.com/Attributes/html" rel="nofollow noreferrer"><code>html()</code></a> manipulation function, the inner html of the div is updated to the latest note. The div selector uses the id we passed, so we can update the corresponding div.</p> <p>Also, this is slightly off topic, but make sure you always use <a href="http://us2.php.net/mysql_real_escape_string" rel="nofollow noreferrer"><code>mysql_real_escape_string</code></a> when putting values into a query like you are. If you do not use this, your queries will be vulnerable and susceptible to injection attacks, and they are not pretty. If a customer decided to enter a note value of <code>');DROP TABLE businesses;</code> you'd really feel the pain. Preferably, switch to <a href="http://us.php.net/pdo" rel="nofollow noreferrer"><code>PDO</code></a> or <a href="http://us.php.net/mysqli" rel="nofollow noreferrer"><code>MySQLi</code></a> and use <a href="http://us.php.net/manual/en/pdo.prepared-statements.php" rel="nofollow noreferrer"><code>prepared statements</code></a>, as they are the 'correct' way of doing queries nowadays.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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