Ever wanted to make an upload script, but don't want the page to refresh? Try AJAX.

In this tutorial we'll use an iframe to simulate AJAX behaviour.

Firstly, we "might" want to create a simple upload form:

 
<p id="f1_upload_process">Loading...<br/><img src="loader.gif" />
<p id="result">
<form action="admin/addpost.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
		File:
<input name="myfile" type="file" />
<input type="submit" name="submitBtn" value="Upload" />
	</form>
 
	<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
 

The important part here is the enctype, always have the enctype specified if you're uploading files. The first paragraph is the little Loading... box that'll show up when we press the submit button. The second <p> "result" is to display the results (We've uploaded or We failed). And lastly the iframe that is actually submitting the data, specified through the target="upload_target" in the form. Also, the forms onsubmit triggers the startUpload(); function.

Now we want to add some Javascript functions:
Read More...



Posted in PHP, Tutorials at July 14th, 2008. 9 Comments.
 
$string = preg_replace("/(http:\/\/)?([a-zA-Z0-9\-.]+\.[a-zA-Z0-9\-]+([\/]([a-zA-Z0-9_\/\-.?&%=+])*)*)/", '<a href="http://$2">$2</a>', $string);

That little gem right there'll take any length of text and turn the urls in it into hyperlinks. Cool or what? :-)



Posted in PHP, Web development at July 13th, 2008. No Comments.

Hey,

So, yeah, I've found out a new trick in PHP! The function I'm talking about is parse_str(). To quote the PHP page:

Description

void parse_str ( string $str [, array &$arr ] )

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

Which means it takes in arguments such as


a=1&b=2&c=3

and turns it into an array in which


array(3) {
  ["a"]=>
  string(1) "1"
  ["b"]=>
  string(1) "2"
  ["c"]=>
  string(1) "3"
}

Note the ampersand (&) in front of the second variable, which is a variable pointer. This means that it takes in the address of the second variable and is able to edit the exact variable you passed to the function in real-time. So, if you pass an empty variable called $foo to it. The $foo variable will be an array with the results from the function. Why it doesnt just return an array is a mystery that'll remain unanswered, but I guess you could just call that PHP.

Read More...



Posted in PHP, Tutorials at July 9th, 2008. 2 Comments.

After reviewing some of my code, I found out I had been doing quite a lot of stuff in deprecated or just plain bad coding. The methods / functions described are PHP examples only.

Ternary Operator

Huh what does that do?!

 
$a = ($b >= 0) ? "b is positive" : "b is negative";

The basic markup of this 'method' is as follows:

$variable = (expression) ? "value if true" : "value if not";

So the example above would set $a to "b is positive" if ($b ?= 0) and would set it to "b is negative" if $b < 0.

So that means it's shorthand for:

 
if ($b > 0)
$a = "b is positive";
else
$a = "b is negative";

Another way I've seen myself do something similar is explained by the following code:

 
$a = "b is negative";
if ($b >= 0) $a = "b is positive"

Comparison operators

Everybody knows about using a double equals sign to compare two values. But have you ever heard of the triple-=? The triple equals-sign is to compare the values AND the types of the variables, cough:

 
if ("1" == true) echo '1 == true'; // Outputs 1 == true
if ("1" === true) echo '1 === true'; // Not true
if ("1" === 1) echo '"1" === 1'; // Not true

I think you understand ;)



Posted in PHP, Tutorials at July 7th, 2008. No Comments.

I've kinda written a CAPTCHA image file :) Code after the jump.
Code explanation is right behind it; (Please view full).

 
<?php
session_start();
$numlines = 4;		// 4 lines/rectangles
$numcrap = 15;		// 15 bullcrap letters behind it.
$str = md5(microtime());
$crap = base64_encode(md5(sha1(microtime())));
$str = substr($str,0,5);
// Str = the string shown in the captcha
$_SESSION['captcha'] = $str;

Up until here, we've just initialized some variables. They kinda speak for themselves. $crap is merely some random nonsense to be displayed behind the actual text. Also, we've already registered the captcha string in the SESSION.
Read More...



Posted in PHP, Tutorials at May 23rd, 2008. 1 Comment.