$.get("test.php", function(data){
$('#myFormField').val(data);
});
$.load('myScript.php', function(data) {
//fill a hidden textbox with your script output and reveal it
$('#textBox').val(data).show();
)};
Category Archives: PHP
PHP Error handing.. ..your way
error_reporting(0);
$old_error_handler = set_error_handler("userErrorHandler");
function userErrorHandler ($errno, $errmsg, $filename, $linenum, $vars)
{
$time=date("d M Y H:i:s");
// Get the error type from the error number
$errortype = array (1 => "Error",
2 => "Warning",
4 => "Parsing Error",
8 => "Notice",
16 => "Core Error",
32 => "Core Warning",
64 => "Compile Error",
128 => "Compile Warning",
256 => "User Error",
512 => "User Warning",
1024 => "User Notice");
$errlevel=$errortype[$errno];
//Write error to log file (CSV format)
$errfile=fopen("errors.csv","a");
fputs($errfile,"\"$time\",\"$filename:
$linenum\",\"($errlevel) $errmsg\"\r\n");
fclose($errfile);
if($errno!=2 && $errno!=8) {
//Terminate script if fatal error
die("A fatal error has occurred. Script execution has been aborted");
}
}
Recursive Iterating..
function directoryScan($dir, $onlyfiles = false, $fullpath = false) {
if (isset($dir) && is_readable($dir)) {
$dlist = Array();
$dir = realpath($dir);
if ($onlyfiles) {
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
} else {
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
}
foreach($objects as $entry => $object){
if (!$fullpath) {
$entry = str_replace($dir, '', $entry);
}
$dlist[] = $entry;
}
return $dlist;
}
}
//Return contents of ‘Folder’ – Relative Paths and Files & Folders.
$result = directoryScan('folder', false, false);
//Return contents of ‘Folder’ – Full Paths and Files & Folders
$result = directoryScan('folder', false, true);
//Return contents of ‘Folder’ – Relative Paths and Only Files.
$result = directoryScan('folder', true, false);
//Return contents of ‘Folder’ – Full Paths and Only Files
$result = directoryScan('folder', true, true);
AJaX request
Simple AJAX request
$('#loginSubmit').on('click', function(e){
e.preventDefault();
checkLogin();
})
function checkLogin()
{
$.ajax({
url: "ajax/login.php",
type: "POST",
data: {
username: $("#form_signin #email").val(),
password: $("#form_signin #password").val()
},
success: function(response)
{
if(response == 'true'){
window.location.replace("/saved-route.php");
} else {
$("#errorMessage").replaceWith(''+response+'
');
}
}
});
}
Glob(ing) through a directory
Loop through all files in a folder with GLOB
foreach( glob('/functions/*.php') as $filename) {
include $filename;
}
PHP page load time
Display the page load time…
This function is used on the ADMIN sections of my own sites : http://www.maggsweb.co.uk
PHP date dashes to slashes
Converting a date format, simply and quickly.
This function is used on my own site : http://www.maggsweb.co.uk
Display tweets from RSS with twitter style links
Based on an original post here:
http://www.farinspace.com/twitter-feed-website-integration/
but modified lisghtly to mimic Twitter.
\1@\2', $v);
$v = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $v);
$v = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1\\2'", $v);
$v = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1\\2'", $v);
$v = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2@\\3", $v);
return trim($v);
}
?>
display filesize
Nicely displaying the filesize, using bytes, megabytes, etc..
= 1024) {
$size /= 1024;
$pos++;
}
return round($size,1)." ".$type[$pos];
}
?>
prevent caching of files
Version them in their with their timestamp. This way, when they change, the link will change and the browser will reload the sitemap… Otherwise it will be fine to be cached as it wont have changed.