This code demonstrates how to create a basic PHP form with a text box and a button to embed links into selected text. Users can input text into the box and select a portion. Clicking the 'Embed Link' button prompts for a URL, then automatically creates a hyperlink around the selected text. Here's the code:

<!DOCTYPE html>
<html>
<head>
	<title>PHP Text Box with Link Embedding</title>
</head>
<body>
	<form action="#" method="POST">
		<label for="textbox">Enter Text:</label><br>
		<textarea id="textbox" name="textbox" rows="5" cols="50"></textarea><br>
		<input type="button" value="Embed Link" onclick="embedLink()">
	</form>
	<script>
		function embedLink() {
			var url = prompt("Enter the URL:");
			if (url != null && url != "") {
				var textbox = document.getElementById("textbox");
				var startPos = textbox.selectionStart;
				var endPos = textbox.selectionEnd;
				var selectedText = textbox.value.substring(startPos, endPos);
				var link = '<a href='' + url + ''>' + selectedText + '</a>';
				var newText = textbox.value.substring(0, startPos) + link + textbox.value.substring(endPos);
				textbox.value = newText;
			}
		}
	</script>
</body>
</html>

This script utilizes JavaScript to handle the link embedding process. When a user clicks the 'Embed Link' button, they're asked for the target URL. The script then retrieves the selected text from the textbox and inserts the link tag around it, updating the textbox with the hyperlinked content.


原文地址: https://www.cveoy.top/t/topic/mi8M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录