4

Is it possible to insert an active link to an input textbox?

I tried using an <a> tag inside the value of html but its not working.

<?php $email = "<a href=\"example@link.com\">example@link.com </a>"; ?>   

<input type="text" id="email" name="email" value="<?php echo $email; ?>">  

It only returns the text without the hyperlink value.

ffrenzi
  • 41
  • 1
  • 5

3 Answers3

5

A couple things are wrong here...

  1. You're not escaping your quotes. Therefore the PHP is invalid.
  2. You're trying to put HTML inside a attribute, which is also invalid.

The only alternative I could see being used here is an HTML element with contenteditable="true" applied. This makes it so an element (per say a <div>) can have it's content be modified.

<?php $email = "<a href=\"example@link.com\">example@link.com </a>"; ?>   
<div id="fake-email" contenteditable="true"><?php echo $email; ?></div>

Then see this related question if you're doing a form.

Edit:

If you're trying to do a form, then this is one example:

document.getElementById("form").onsubmit = function(){
    document.getElementById("email").value =  
    document.getElementById("fake-email").innerText || document.getElementById("fake-email").textContent;
}

While your form is:

<form action="..." method="..." id="form">
   <div id="fake-email" contenteditable="true"></div>
   <input type="hidden" id="email" name="email" />
</form>
Community
  • 1
  • 1
  • There is no need to put a reminder in every answer you create to have user accept it – charlietfl Dec 22 '14 at 02:54
  • @charlietfl The OP is new, this is their first question, usually newbies don't accept answers without a notice. –  Dec 22 '14 at 02:56
  • they get reminded, still not a common practice to do what you are doing – charlietfl Dec 22 '14 at 02:59
  • @charlietfl [According to this](http://meta.stackoverflow.com/questions/251288/dealing-with-an-answer-that-wasnt-accepted-maybe-because-a-user-is-a-newbie-in) it's okay to remind them. There is no need to argue over this, so lets drop it. –  Dec 22 '14 at 03:06
  • @Xero http://meta.stackoverflow.com/a/251298/ states *"If you comment in this situation...*. They mean to comment and not place it in an answer. Therefore, placing a comment under the OP's question will suffice, if OP hasn't responded. Nobody's attacking you; we're just saying so please don't take things personally. I have many unaccepted answers myself, so I understand. – Funk Forty Niner Dec 22 '14 at 04:46
2

No, it isn't possible. Input values will always be rendered as plain text. If the user doesn't need to edit the link I would just put it beside the input.

Otherwise you might want to look into WYSIWYG Editors. Links to two of the most popular below.

TinyMCE

CKEditor

Jack Traynor
  • 139
  • 2
0

You need to escape quotes when including it in your php variable.

<?php $email = "<a href=\"example@link.com\">example@link.com </a>"; ?> 

You need to use a backslash when you're using double quotes.

Alternatively, you can write it as such:

 <?php $email = '<a href="example@link.com">example@link.com </a>'; ?> 

If you start with single quotes, then you don't need to escape the double quotes. \

I strongly suggest you read up on escaping characters when need be.

Dimitri
  • 1,906
  • 3
  • 25
  • 49