jcstanley Posted April 24, 2008 Share Posted April 24, 2008 Hi I am trying to create a CMS. When inputting text into a text box how can I give the user the option make a hyperlink? I guess when the form is submitted the data should be stored as text in the database but it will have the <a href....etc> tags around it. Then the other question is how do you read it back to be displayed as a clickable hyperlink on the page. I know that paragraphs are formatted using nl2br. Is there something similar for hyperlinks? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/102746-hyperlink-tags/ Share on other sites More sharing options...
947740 Posted April 24, 2008 Share Posted April 24, 2008 Use javascript and a button. When they click the button, insert the tags around the text they have selected. You will also have to work in the link for the hyperlink. If the text is literal and you do not strip all the tags, it will show up just fine. Link to comment https://forums.phpfreaks.com/topic/102746-hyperlink-tags/#findComment-526220 Share on other sites More sharing options...
jcstanley Posted April 24, 2008 Author Share Posted April 24, 2008 thanks for the suggestion. do you have an example you could show me as i am not too familiar with javascript? Link to comment https://forums.phpfreaks.com/topic/102746-hyperlink-tags/#findComment-526376 Share on other sites More sharing options...
947740 Posted April 24, 2008 Share Posted April 24, 2008 There are a few bugs, but there are always going to be bugs: <html> <head> <title>Test</title> <script language="javascript" type="text/javascript"> function addA() { // This gets the URL var link = prompt('Please enter the URL you would like to link to. (Please include the http://)','http://'); // Creates opening part of link var open = "<a href='" + link + "'>"; // Gets selected text var text = document.selection.createRange().text; // Last part of link var close = "</a>" /* You can output two ways. One, replace the selected text, or append it to the document. Right now, it does both. */ // Puts the entire string together var all = open + text + close; document.selection.createRange().text = all; // Finds area to put output var area = document.getElementById('inputarea'); // Outputs text area.innerHTML = all; } </script> </head> <body> <form name="theform" method="post"> <a href="javascript:addA();">Add A Hyperlink</a> <textarea rows="5" cols="20"> </textarea> </form> <div id='inputarea'> </div> </body> </html> If you want to see it in action, go here: http://www.iowatelecom.net/~scarruthers/text.html EDIT: There are several things in that script you can change. You can make a button instead of a link (like I mentioned). The layout can also be adjusted. If you do not want the link added to the page, just comment out the last 4 lines of code (2 already are). Link to comment https://forums.phpfreaks.com/topic/102746-hyperlink-tags/#findComment-526406 Share on other sites More sharing options...
jcstanley Posted April 24, 2008 Author Share Posted April 24, 2008 Thanks I will give it a try! There are a few bugs, but there are always going to be bugs: <html> <head> <title>Test</title> <script language="javascript" type="text/javascript"> function addA() { // This gets the URL var link = prompt('Please enter the URL you would like to link to. (Please include the http://)','http://'); // Creates opening part of link var open = "<a href='" + link + "'>"; // Gets selected text var text = document.selection.createRange().text; // Last part of link var close = "</a>" /* You can output two ways. One, replace the selected text, or append it to the document. Right now, it does both. */ // Puts the entire string together var all = open + text + close; document.selection.createRange().text = all; // Finds area to put output var area = document.getElementById('inputarea'); // Outputs text area.innerHTML = all; } </script> </head> <body> <form name="theform" method="post"> <a href="javascript:addA();">Add A Hyperlink</a> <textarea rows="5" cols="20"> </textarea> </form> <div id='inputarea'> </div> </body> </html> If you want to see it in action, go here: http://www.iowatelecom.net/~scarruthers/text.html EDIT: There are several things in that script you can change. You can make a button instead of a link (like I mentioned). The layout can also be adjusted. If you do not want the link added to the page, just comment out the last 4 lines of code (2 already are). Link to comment https://forums.phpfreaks.com/topic/102746-hyperlink-tags/#findComment-526419 Share on other sites More sharing options...
947740 Posted April 24, 2008 Share Posted April 24, 2008 I thought I fixed this, but the link is: http://www.iowatelecom.net/~scarruthers/test.html Link to comment https://forums.phpfreaks.com/topic/102746-hyperlink-tags/#findComment-526426 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.