LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2007, 10:44 AM   #1
orfiyus
Member
 
Registered: May 2006
Posts: 42

Rep: Reputation: 15
Need help converting a link into a text field html


Yea my bad for the awful thread title.

Im trying to make a link that when it is clicked on turns itself into a text-field once the user types in the text and clicks somewhere else wutever the user typed in the previously mentioned text field turns back into a link.

The problem Im having is I dont know what to actually search for in google (which why this thread has such a bad title). Anyone have any links to tutorials on how I could do this? Also even telling me the name of this action could help cuz I could just search it myself.
 
Old 11-27-2007, 06:58 PM   #2
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
You could use javascript. Off the top of my head without testing or error checking (think simplified possible solution)... maybe something along these lines:

Code:
<script type="text/javascript">
function editLink()
{
  var myLink = document.getElementById('my_link');
  var myInput = document.getElementById('my_input');

  myInput.value = myLink.innerHTML;
  myLink.style.display = 'none';
  myInput.style.display = 'inline';
  
}

function doneEdit()
{
  var myLink = document.getElementById('my_link');
  var myInput = document.getElementById('my_input');

  myLink.innerHTML = myInput.value;
  myLink.style.display = 'inline';
  myInput.style.display = 'none';
}
</script>
<a id="my_link" 
  href="javascript:editLink()">Click Me</a>
<input type="text" 
  style="display: none;" 
  id="my_input"
  onblur="doneEdit()"/>
 
Old 11-29-2007, 08:17 AM   #3
orfiyus
Member
 
Registered: May 2006
Posts: 42

Original Poster
Rep: Reputation: 15
Yo Micah Thanks alot this is exactly wut I am looking for. Wut exactly is this kind of process called? And can you tell me where you found this code or if you wrote it yourself?
 
Old 11-29-2007, 08:34 AM   #4
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
I just wrote it off the top of my head. You'd want to test it in any browser you intend to support (IE6+, FF2+, Safari, Opera, etc.)

To be able to do this type of thing, you'll want to Google up on "DOM", "CSS", and "JavaScript" in addition to regular (x)HTML knowledge.

A lot of things can be made simpler by using some type of framework such as Prototype (google prototype.js).
 
Old 11-30-2007, 09:36 AM   #5
orfiyus
Member
 
Registered: May 2006
Posts: 42

Original Poster
Rep: Reputation: 15
I shouldve said this before but I needed this code for a php script. So I placed it in the php and it dont work. Can someone take a look and see wuts wrong cuz I get a blank page when I try to run it as php script. Heres my code:

Code:
<?php
echo("<html><body>");
echo("<TITLE>COMBO2.PHP</TITLE>");
echo("<BR><B><U>THIS Is the test to combine php javascript and html<br></U>");
echo("<BR><BR>");


echo("<script type="text/javascript"><!--\n");
echo("function editLink() { ");
echo("var myLink = document.getElementById('my_link'); ");
echo("var myInput = document.getElementById('my_input'); ");

echo("myInput.value = myLink.innerHTML; ");
echo("myLink.style.display= 'none'; ");
echo("myInput.style.display = 'inline'; ");
echo("} ");

echo("function doneEdit() { ");
echo("var myLink = document.getElementById('my_link'); ");
echo("var myInput = document.getElementById('my_input'); ");

echo("myLink.innerHTML = myInput.value; ");
echo("myLink.style.display = 'inline'; ");
echo("myInput.style.display = 'none'; ");
echo("} ");

echo("//--></script>");
echo( "<a id="my_link" href="javascript:editLink()">Click Me</a> ");
echo( " <input type="text"  style="display: none;" ");
echo( "id="my_input" ");
echo( "onblur="doneEdit()"/> ");
echo("<br>");
echo(" </body></html>");
?>
 
Old 11-30-2007, 10:32 AM   #6
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Wow, you are relatively new to this yes?

The short answer is that you didn't escape your quotes:
Code:
echo( "<a id="my_link" href="javascript:editLink()">Click Me</a> ");
should be:
Code:
echo( "<a id=\"my_link\" href=\"javascript:editLink()\">Click Me</a> ");
Also, you should look into configuring your php.ini to display errors during development (turn it off on a production server) so that you would get an error instead of a blank page. In this case, it would have been a parse error.

Next, just some friendly advice, work on writing cleaner code. Start by running the HTML through a validator (Google search "HTML Validator") and used the "cleaned up" version. Especially when using DOM and Javascript functions as we are in this case.

Next, rather than putting all that code into echo functions, why not just use a PHP block where needed. As an example, both of these work the same:

Code:
<?php 
  $hello = "Hello World";
  echo "<p>$hello</p>"; 
?>

<?php $hello = "Hello World" ?>
<p><? echo $hello ?></p>
The goal is to keep the PHP and output as separate as possible so it's easy to read and maintain. A lot of professionals use templates for this reason, but if you're new, you should start by trying to keep the PHP that process things in one part of the page, such as the top, and regular html on the bottom with PHP variables placed where needed.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>PHP, Javascript, and HTML</title>
<script type="text/javascript">
function editLink()
{
  var myLink = document.getElementById('my_link');
  var myInput = document.getElementById('my_input');

  myInput.value = myLink.innerHTML;
  myLink.style.display = 'none';
  myInput.style.display = 'inline';
  
}

function doneEdit()
{
  var myLink = document.getElementById('my_link');
  var myInput = document.getElementById('my_input');

  myLink.innerHTML = myInput.value;
  myLink.style.display = 'inline';
  myInput.style.display = 'none';
}
</script>
</head>
<body>
<?php
  // here is a php block
  echo "This is a test of PHP, Javascript, and HTML";
?>
<br/>
<p><?= "I could have just used a PHP echo short cut" ?></p>
<br/>
<a id="my_link" 
  href="javascript:editLink()">Click Me</a>
<input type="text" 
  style="display: none;" 
  id="my_input"
  onblur="doneEdit()"/>
</body>
</html>
 
Old 11-30-2007, 11:34 AM   #7
orfiyus
Member
 
Registered: May 2006
Posts: 42

Original Poster
Rep: Reputation: 15
Yea Im gonna do that. I posted this problem on another forum and got the same answer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
html suppress input field history Tischbein Programming 2 10-10-2007 03:35 AM
one charater is missed from html form field value simon_qwl Programming 2 04-24-2007 08:19 AM
converting Windows text to Linux text joshknape Linux - Software 3 09-11-2005 12:52 PM
form field in html to have permanent value meluser Programming 3 04-19-2003 02:53 PM
Converting Text To HTML Glock Shooter Programming 6 07-03-2002 06:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration