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...<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...
Popularity: 27% [?]
PHP, Tutorials
$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? :-)
Popularity: 18% [?]
PHP, Web development
regex, regular expressions
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 ] )
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...
Popularity: 8% [?]
PHP, Tutorials
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 ;)
Popularity: 7% [?]
PHP, Tutorials
Hey,
You've probably heard of AJAX. It's an acronym for "Asynchronous Javascript And XML". It basically means you could use Javascript for updating a page, submitting info without refreshing and so forth. I've only just found out the basics of AJAX myself. It's not hard, but it requires (very) basic understanding of Javascript and HTML.
The biggest part about using AJAX is understanding the XMLHttpRequest() object. This object has a wide range of properties, functions and what not. But for basic AJAX you'll only need a couple.
Initializing the XMLHttpRequest object
function GetXML() {
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
return xmlHttp;
}
That is the function I'm using to get the object. Usually you could just use the XMLHttpRequest(), but since the (oh-so-loved) Internet Explorer doesn't support it. You need to make a complete function and have it return the object.
You can now use:
ajax = getXML();
to get the object instantiated.
Read more...
Popularity: 7% [?]
Tutorials, Web development
Recent comments!