Posted by & filed under HTML, Javascript.

HTML actions:

The action attribute specifies where to send the form-data when a form is submitted. In HTML5, the action attribute is no longer required. We can use javascript to send data on the backend using AJAX.

http://www.w3schools.com/tags/att_form_action.asp

Callbacks in javascript:

In general, a callback function is used once another function you have called finishes. A good example of this is AJAX requests: most libraries have a function that allows you to send a request to the server in the background without refreshing the page (this uses AJAX). You generally provide two callback functions to this AJAX function: a success function, and a failure function.

For more information:
http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/
http://recurial.com/programming/understanding-callback-functions-in-javascript/

Posted by & filed under Javascript, jQuery.

After you submit a form, you reset it with the following code:

 $("form#myform")[0].reset();

For example on my Magic8Ball website, I created a function to submit the form and reset it immediately, clearing out the input elements.

 var  formData = $("#form").serializeArray();
  $.ajax({
    url : "speaktome.php",
    type: "POST",
    data : formData,
    success: function(result)
    {
      $(".youranswer").hide().html('

' + result + '

').fadeIn(5000); $.ajax({url:"update.php",success:function(result){ $(".freshdata").hide().html(result).fadeIn(); $("#form")[0].reset(); // Resets Input }}); } });

Posted by & filed under jQuery.

Found this great bit of code on SO. Basically, it changes your cursor to the progress or wait symbol whenever AJAX is started on your page. Nice for forms or submissions by the user.

$('body').ajaxStart(function() {
$(this).css({'cursor':'wait'})
}).ajaxStop(function() {
$(this).css({'cursor':'default'})
});

By the way here is a great page from w3schools on cursor types!

Posted by & filed under Javascript, JSON, PHP.

I had this problem recently when I used made a call to Yahoo Finance’s Stock quotes API for my Chinese website about US Stocks.

Yahoo’s API returns the stock data in JSON form, but you can choose to handle it as a php object or associative array.

First, this post by Michael Nitschinger really helped me see my choices: Handling-JSON-like-a-boss-in-PHP.

The problem was that the information I wanted about the stock was nested deep in the JSON. So it seemed I had two choices, to treat the data like a PHP Object or Associative array.

First, you need to use PHP to decode the information correctly. If you want an object, you need to use:

$result = json_decode($string);

If you want to use it like an associative array, use:

$result = json_decode($string, true);

Then to grab nested items within the data, you have to grab it with code like below:

If the data is in object form, use:

$lastTrade = $data->query->results->quote[0]->LastTradePriceOnly;

To grab nested results like a associate array, use this style:

$lastTrade = $data["query"]["results"]["quote"][0]["LastTradePriceOnly"];

Posted by & filed under PHP.

I have run into the same annoying problem for a long time and finally found an easy solution. Whenever forms are submitted via PHP (by a user or myself) and apostrophes or quotation marks are added in forms, they are escaped using mysqli_real_escape_string and entered into the database without a hitch. However, when outputting the data from the database, any text or strings suddenly get cut off, disappear or don’t work wherever the apostrophe and quotations appear.

htmlspecialchars to the rescue! Rather than a simple echo of the text or string directly from the database, use htmlspecialchars() around the string to convert apostrophes and quotation marks into ASCII strings and does not mess with the PHP markup.

 

for example:

<?php

$string =  row['string'];

echo htmlspecialchars($string);

?>

 

 

Posted by & filed under jQuery.

Found a fantastic image rollover solution on StackOverflow recently, with this little piece of jQuery and no other plug-ins needed your rollovers works easily and seamlessly…

HTML

<img data-other-src="big-zebra.jpg" src="small-cat.jpg">
<img data-other-src="huge-elephant.jpg" src="white-mouse.jpg">
<img data-other-src="friendly-bear.jpg" src="penguin.jpg">

Javascript

$('img').on('mouseenter mouseleave', function() {
 $(this).attr({
  src: $(this).attr('data-other-src'),
'data-other-src': $(this).attr('src')
})
});

Posted by & filed under MySQL, PHP.

I’ve created submission forms with the textarea box, but when I echoed out the submission, all the carriage returns disappeared! Of course you don’t want to force the user to type <br /> or <p> tags into the posts. Actually, I learned that the carriage returns did not disappear, you simply need to add a bit of code to when you output the content. By using nl2br all the carriage returns appear again~!

 

PHP Code:

$data = mysql_query('SELECT text FROM table');
while ($row = mysql_fetch_assoc($data))
{
   echo "<p>".nl2br($row['text'])."</p>\n";