Welcome to Dream.In.Code
Become an Expert!

Join 150,199 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,992 people online right now. Registration is fast and FREE... Join Now!




can javascript remember old values?

 
Reply to this topicStart new topic

can javascript remember old values?, want to use java to return html text inputs to default values initiall

adamgram
23 Sep, 2008 - 04:14 PM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2008
Posts: 7


My Contributions
Okay, so I have a PHP/MySQL based site and I'm trying to put together an 'edit your post' page. Basically everything the user previously put in pops up as an html form with disabled fields. Currently I have a Java script activated by a link to remove the 'disable' from an individual field if the user wants to edit it, but I also want to add a 'reset' link to return to the initial values if the user changes his/her mind about one field in the midst of filling out the form.

When the form first pops up, the initial html values are dictated by PHP. By clicking 'edit' they can alter these values, and what I want them to be able to click 'reset' and return to the initial values. However, since PHP is on the server and Java with the client, if I understand things correctly Java can't re-retrieve the PHP values as I did the first time around.

Instead I am curious to find a way with Java read the initial value, remember it, and later, if the user hits 'reset', send it back to the HTML. Below is a piece of my code. Basically I want to replace the "default" in the rest_name_rest function with whatever the output of <?php echo $row[rest_name]?> was when the page was first loaded. Thanks in advance for the help!!!

CODE

<html>
<head>

<script type="text/javascript">
function rest_name_enable(){
if (document.all || document.getElementById){
document.form_name.rest_name.disabled=false}}
</script>

<script type="text/javascript">
function rest_name_reset(){
if (document.all || document.getElementById){
if (document.form_name.rest_name.disabled==false)
{var r=confirm("This will delete whatever you just typed!!" + '\n' +
"Are you sure you want to reset the Restaurant Name?");
if (r==true)  {
document.form_name.rest_name.disabled=true
document.form_name.rest_name.value="default"}
else {}}
else {}}}

</script>
</head>
<body>

<form name="form_name">
<input name="rest_name" type="text" value="<?php echo $row[rest_name]?>" disabled="disabled">
<a href = "java script:rest_name_enable()">edit</a>
<a href = "java script:rest_name_reset()">reset</a><br>

</body>
</html>


Moved to JavaScript forum

This post has been edited by pbl: 23 Sep, 2008 - 04:46 PM
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Can Javascript Remember Old Values?
23 Sep, 2008 - 04:46 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
This is the wrong forum, this is a javascript question, not a Java question.

Basically all you need to do is create a variable that holds the initial values, and if the user decides to revert the value it pulls the value from the variable and places it in the field.
User is offlineProfile CardPM
+Quote Post

adamgram
RE: Can Javascript Remember Old Values?
23 Sep, 2008 - 05:14 PM
Post #3

New D.I.C Head
*

Joined: 17 Sep, 2008
Posts: 7


My Contributions
QUOTE(BetaWar @ 23 Sep, 2008 - 05:46 PM) *


Basically all you need to do is create a variable that holds the initial values, and if the user decides to revert the value it pulls the value from the variable and places it in the field.


Yes, but how? I tried the obvious... putting

var def;
def=document.form_name.rest_name.value;

at various stages in the script, but nothing seemed to work...

User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Can Javascript Remember Old Values?
23 Sep, 2008 - 05:36 PM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
You are looking for something like this:

CODE
<script type="text/javascript">
function rest_name_enable(){
  if(document.all || document.getElementById){
    document.form_name.rest_name.disabled = false;
    document.form_name.rest_name.defVal = document.form_name.rest_name.value;
  }
}

function rest_name_reset(){
  if(document.all || document.getElementById){
    if(document.form_name.rest_name.disabled == false){
      var r = confirm("This will delete whatever you just typed!!\nAre you sure you want to reset the Restaurant Name?");
      if(r == true){
        document.form_name.rest_name.disabled = true;
        document.form_name.rest_name.value = document.form_name.rest_name.defVal;
      }
      else{
      }
    }
    else{
    }
  }
}

</script>

<form name="form_name">
<input name="rest_name" type="text" value="<?php echo $row[rest_name]?>" disabled="disabled">
<a href = "java script:rest_name_enable()">edit</a>
<a href = "java script:rest_name_reset()">reset</a><br>


I posted a snippet on something like this a while back:
http://www.dreamincode.net/code/snippet2326.htm

Hope that helps.
User is offlineProfile CardPM
+Quote Post

adamgram
RE: Can Javascript Remember Old Values?
23 Sep, 2008 - 05:47 PM
Post #5

New D.I.C Head
*

Joined: 17 Sep, 2008
Posts: 7


My Contributions
SWEET!! Thanks a lot man. I'm really new to all this and building this site as a way of learning... I read online tuts and whatnot but sometime specific questions are hard to track down. I really appreciate all the help I've got on these forums. Thanks again!

QUOTE(BetaWar @ 23 Sep, 2008 - 06:36 PM) *

You are looking for something like this:

CODE
<script type="text/javascript">
function rest_name_enable(){
  if(document.all || document.getElementById){
    document.form_name.rest_name.disabled = false;
    document.form_name.rest_name.defVal = document.form_name.rest_name.value;
  }
}

function rest_name_reset(){
  if(document.all || document.getElementById){
    if(document.form_name.rest_name.disabled == false){
      var r = confirm("This will delete whatever you just typed!!\nAre you sure you want to reset the Restaurant Name?");
      if(r == true){
        document.form_name.rest_name.disabled = true;
        document.form_name.rest_name.value = document.form_name.rest_name.defVal;
      }
      else{
      }
    }
    else{
    }
  }
}

</script>

<form name="form_name">
<input name="rest_name" type="text" value="<?php echo $row[rest_name]?>" disabled="disabled">
<a href = "java script:rest_name_enable()">edit</a>
<a href = "java script:rest_name_reset()">reset</a><br>


I posted a snippet on something like this a while back:
http://www.dreamincode.net/code/snippet2326.htm

Hope that helps.


User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Can Javascript Remember Old Values?
23 Sep, 2008 - 05:51 PM
Post #6

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
No problem, happy to have helped out smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month