Skip to content

Instantly share code, notes, and snippets.

@Eddy-Barraud
Last active May 6, 2019 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eddy-Barraud/588cdb79beb82dbc634e8f378c954b21 to your computer and use it in GitHub Desktop.
Save Eddy-Barraud/588cdb79beb82dbc634e8f378c954b21 to your computer and use it in GitHub Desktop.
Sample for converting textarea containing links with php & ajax
<?php
$q = trim($_POST["q"]);
$lines = explode(PHP_EOL, $q);
$result = "";
foreach ($lines as $key => $value) {
$result .= $value . PHP_EOL;
}
echo $result;
exit;
?>
<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str) {
if (str.length <= 2) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("POST", "gethint.php", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var params = 'q=' + escape(str);
xmlhttp.send(params);
}
}
</script>
</head>
<body>
<center>
<h1>Link Converter</h1>
<textarea rows="20" cols="150" onkeyup="showHint(this.value)"></textarea>
<p><textarea rows="20" cols="150" id="txtHint"></textarea></p>
</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment