Skip to content

Instantly share code, notes, and snippets.

@apfelbox
Created September 6, 2012 12:39
Show Gist options
  • Save apfelbox/3655825 to your computer and use it in GitHub Desktop.
Save apfelbox/3655825 to your computer and use it in GitHub Desktop.
POST example
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
</head>
<body>
<input type="text" name="length" value="500" id="length" />
<button>Send</button>
<script>
$("button").on(
"click",
function ()
{
var length = parseInt($('#length').val());
$.post(
"b.php",
{ text: getText(length) },
function (data)
{
console.log("Size: " + _format_bytes(length));
if (length == data.text_length)
{
console.info("ok: " + length + " == " + data.text_length);
}
else
{
console.error("error! sent: " + length + ", received: " + data.text_length);
}
},
"json"
);
}
);
function getText (length)
{
return new Array( length + 1 ).join( "a" );
}
function _format_bytes($a_bytes)
{
if ($a_bytes < 1024) {
return $a_bytes +' B';
} else if ($a_bytes < 1048576) {
return Math.round($a_bytes / 1024) +' KiB';
} else if ($a_bytes < 1073741824) {
return Math.round($a_bytes / 1048576) + ' MiB';
} else if ($a_bytes < 1099511627776) {
return Math.round($a_bytes / 1073741824) + ' GiB';
} else if ($a_bytes < 1125899906842624) {
return Math.round($a_bytes / 1099511627776) +' TiB';
} else if ($a_bytes < 1152921504606846976) {
return Math.round($a_bytes / 1125899906842624) +' PiB';
} else if ($a_bytes < 1180591620717411303424) {
return Math.round($a_bytes / 1152921504606846976) +' EiB';
} else if ($a_bytes < 1208925819614629174706176) {
return Math.round($a_bytes / 1180591620717411303424) +' ZiB';
} else {
return Math.round($a_bytes / 1208925819614629174706176) +' YiB';
}
}
</script>
</body>
</html>
<?php
$length = isset($_POST['text']) ? strlen($_POST['text']) : 0;
echo json_encode(['text_length' => $length]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment