ffrenzi
ffrenzi

Reputation: 41

How to insert an active link to an input textbox?

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=\"[email protected]\">[email protected] </a>"; ?>   

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

It only returns the text without the hyperlink value.

Upvotes: 4

Views: 2743

Answers (3)

user3117575
user3117575

Reputation:

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=\"[email protected]\">[email protected] </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>

Upvotes: 5

Dimitri
Dimitri

Reputation: 1966

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

<?php $email = "<a href=\"[email protected]\">[email protected] </a>"; ?> 

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

Alternatively, you can write it as such:

 <?php $email = '<a href="[email protected]">[email protected] </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.

Upvotes: 0

Jack Traynor
Jack Traynor

Reputation: 149

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

Upvotes: 2

Related Questions