Lil Josh

A tech blog about web development with PHP, MySQL, jQuery, CSS3 and various other programming languages. You will not find anything about ColdFusion, ASP.NET, C# on this blog.

jQuery JSONP Cross-domain AJAX Callback Function

August 29, 2014

There may be times where you need to make an AJAX call to a resource from another domain, but browsers will not allow a simple AJAX GET. Instead you’ll need to implement a callback function and use JSONP. Here’s an example of the requesting AJAX script:

<script>
var username = 'liljosh';
var url = '//newDomain.com/callbackScript.php?username='+username+'&output=json&callback=?';
        $.getJSON(url, function (data) {
            alert(data);
        });
</script>

and the callbackScript.php could look like this:

<?php
output("Username is $_GET[username]");

// output as JSON
function output($html) {
    if(isset($_GET['callback'])) $html = $_GET['callback'] . '(' . json_encode($html) . ')';
    die($html);
  }
?>
AJAX Callback Function Cross-domain jquery JSONP

Texting with PHP

June 6, 2014

Ever needed to text someone through PHP? It’s very simple! Most cellular providers give an email address with your phone number that can be used to text that number. This is known as “email to SMS gateway”. For example if my number is (555) 555-5555 and I used AT&T then emailing 5555555555@txt.att.net would send a text to my phone. So you can probably guess how the PHP looks, but here’s an example:

<?php
    $from = "Lil Josh <info@liljosh.com>"; // sender
    $subject = "Subject appears in the text message";
    $message = "This is the contents of the text";
    
    // send text message
    mail("5555555555@vtext.com",$subject,$message,"From: $from\n"); // Verizon
    mail("5555555555@txt.att.net",$subject,$message,"From: $from\n"); // AT&T
    mail("5555555555@tmomail.net",$subject,$message,"From: $from\n"); // T-Mobile
    mail("5555555555@vmobl.com",$subject,$message,"From: $from\n"); // Virgin Mobile
    mail("5555555555@messaging.sprintpcs.com",$subject,$message,"From: $from\n"); // Sprint
    mail("5555555555@messaging.nextel.com",$subject,$message,"From: $from\n"); // Nextel
?>

For a more complete list of providers visit http://www.ukrainecalling.com/email-to-text.aspx

AT&T Nextel PHP Sprint T-Mobile texting Verizon Virgin Mobile

Recursion in PHP

May 29, 2014

Using recursion can be a little hard to grasp if you are new to the concept. In it’s simplest form it’s a function that calls upon itself to create a loop. It is commonly used for traversing tree structures. This example shows PHP using recursion to get a list of all the child pages under a folder structure.

<?php

function getPages($folder, $pages=array()) {
  //function returns everything in the folder 1 level
  $assets = getChildren($folder); 

  foreach($assets as $asset) {
    if($asset->type == 'folder') {
      $pages = getPages($asset->path, $pages);
    } else if ($asset->type == 'page') {
      array_push($pages, $asset->name);
    }
  }

  return $pages;
}

?>

recursion

example function PHP recursion

jQuery Mobile Dynamicly Appending to Listview via AJAX issue

March 9, 2014

With jQuery Mobile you can create a Listview of list items but I wanted to populate that list dynamically via AJAX. The problem I was getting is it would work fine when I loaded it the first time but then I wanted to nuke (clear) the list and repopulate it with new content. If you’ve gone through half the trouble I went through to get this to work then I know this is going to help you out! So I won’t drag this out any longer… here’s the solution:

//empty the list
$("#myList").empty();

//do some stuff to populate the content
myListContent += '<li><a>some content</a></li>';

//append the new content and create a new listview and refresh it
$("#myList").append(myListContent).listview().trigger("create");
$("#myList").listview('refresh');
AJAX issue resolved jquery jquerymobile listview