...

View Full Version : Value to a hidden field



antler
08-10-2004, 01:05 PM
Does anyone know how to pass a value from the marquee tag to a hidden field?
i.e


<?php
$message_content="wibble"
?>

<body>


<marquee behavior=scroll direction="left"
bgcolor="white" id="wibble" style="font-size:28px">
</marquee>

<div align="left">

<script language="JavaScript" type="text/javascript">

function message(){

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");

obj.innerHTML=message;
}
</script>

<input name="HiddenMessage" type="hidden" id="HiddenMessage"
value="<?php echo $_GET['$message_content']; ?>" />
<input type="hidden" name="MM_insert" value="scroll_frm">
</form>
</body>

SpiritualStorms
08-10-2004, 02:04 PM
Could you elaborate a little? I mean, do you want to have a button that passes the value from one field, to another? Or do you want it with in the same function?

Logic dictates that if you create a path to a specific form element, that you must also create a path to another form element.


function message(){

var hidEle=document.form['whatever name']['whatever name'];

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");

obj.innerHTML=message;
hidEle.value=message;
}


I would presume thats how it works.

glenngv
08-10-2004, 02:59 PM
Could you elaborate a little? I mean, do you want to have a button that passes the value from one field, to another? Or do you want it with in the same function?

Logic dictates that if you create a path to a specific form element, that you must also create a path to another form element.


function message(){

var hidEle=document.form['whatever name']['whatever name'];

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");

obj.innerHTML=message;
hidEle.value=message;
}


I would presume thats how it works.
Should have the s in form

var hidEle=document.forms['whatever name']['whatever name'];

antler
08-11-2004, 10:56 AM
Could you elaborate a little? I mean, do you want to have a button that passes the value from one field, to another? Or do you want it with in the same function?

Logic dictates that if you create a path to a specific form element, that you must also create a path to another form element.


function message(){

var hidEle=document.form['whatever name']['whatever name'];

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");

obj.innerHTML=message;
hidEle.value=message;
}


I would presume thats how it works.

Ok, What I had in mind was related to the whole message and formatting thing from earlier posts.

A user writes a message (via the function above "message()") but how could that text be saved to a database when the user clicks a button called submit?

I thought about putting a hidden field into the form named for example, HiddenMessage, and then somehow directing the text written via the message() function to that hidden field.

In theory I could then insert the contents of the hidden field "Hidden Message" into the database like this:


f ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "text_frm")) {
$insertSQL = sprintf("INSERT INTO Text (Message) VALUES (%s,
GetSQLValueString($_POST['HiddenMessage'], "text"),


The problem is that I do not know how to specify the path from message() to HiddenMessage.

I will take a look at the examples you have posted.

any further points in the right direction appreciated!

antler.

antler
08-11-2004, 11:16 AM
function message()

var hidEle=document.form['HiddenMessage']['hiddenMessage'];

hidEle.value=message;


So, is this saying that into the local variable "hidEle" goes the "document" object and the property "form" ['the_document']['HiddenMessage'];

Is that a path to the hidden field in the form "HiddenMessage", so when the user starts the message() function the value of hidEle is sent to HiddenMessage?

jamescover
08-11-2004, 03:56 PM
var message=prompt("Please type a message (200 character max)","");


You are invoking the function from your form, right? If so, the value of message is already set to whatever is typed into the prompt. You don't need a hidden field. Just grab the value in php as $message when it is passed via the form submission.


-james

SpiritualStorms
08-11-2004, 04:06 PM
Dude. You use variables to to store data. If the goal is to pass data from one location, to another, then you will use a variable. When you create a path, all you are doing is giving the variable a direction to go to for the collection of data. Thus, you use the dot syntax as a means of passing data to a variable:



var hidEle=document.form['whatever name']['whatever name'];


With the above, you have basically created a representation. Your variable will now represent an object. The object of course is a form element. First part, is simply connecting to the document object since obviously thats where you form element will be. Now because your document can contain many things, like an image, links, text, and the what not, the next step is to specify the form object. Now, since the form object can contain more than one form element, you will need to specify which of those form elements you are interested in, hence, the second part in the brackets.

Now, if you have a variable that represents a form element, and you want to assign, or store a value in the value property of that form element, then you will do something like this:



var hidEle=document.form['whatever name']['whatever name'];

hidEle.value=message;


If we assume that your hidEle actually represents a specific form element, then you can now access any of that form elements properties:



var hidEle=document.form['whatever name']['whatever name'];

hidEle.value;
hidEle.name;
hidEle.type;


And so on, and on.

Since you goal is to store something inside a hidden field, it would make sense to think then that you would ultimately do something like this:



function message(){

var hidEle=document.form['whatever name']['whatever name'];

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");

obj.innerHTML=message;

hidEle.value=message;
}


By having a variable, you can cut down on the number of times you have to basically create a path to the very property you are interested in. Since you want store data in a hidden field, you will need to do this:


hidEle.value=message;

If we assume that message is a variable, and hidEle is another variable, then we will use one variable to access the value property, and then assign to that property the contents of another variable, such a variable by the name of message.

I thought i went through this with you? I thought i specifically said something about the accessing of a particular property of a particular tag? Why arent you getting this part?

In the above, hidEle is the variable, and the value property is representefed by, hidEle.value. Since we want to store something inside it, we use the assignement operator, thus its hidEle.value=. Once, we have the assignment operator, then its a matter of knowing what will assign. Since you want what is typed in a prompt() method, we will basically attach a variable at the end of the assignment operator, bring our previous to this final statement:

hidEle.value=message.

Whatever is stored in message, will now be relocated to the hidden field property that is responsible for the holding of data, which is generally the value property.

antler
08-11-2004, 04:50 PM
It didn't work for me.

What I do not understand is what goes in ['whatever name']['whatever name'] obviously.

The name of what? the value of what? The id of what?

That is what I did not understand.

SpiritualStorms
08-11-2004, 05:01 PM
Did i not say that when you create a form tag, that you could give it a name? Did i not also say that you can give a form element a name?



<form name="forest">

<input type="hidden" name="tree">

</form>


Well, if i have something like that, i think i would probably do something like,



var hidEle=document.forms['forest']['tree'];


Any questions?

antler
08-11-2004, 05:19 PM
Yeah, I was kind of afraid you would say that:



function message(){
var hidEle=document.form['scroll_frm']['HiddenMessage'];

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");


obj.innerHTML=message;
hidEle.value=message;
}

My form is called scroll_frm and the hidden field is called HiddenMessage.

I just get error on page.

SpiritualStorms
08-11-2004, 05:24 PM
What is the nature of your error? You will need to provide details if one is to know what exactly seems to be the problem.

antler
08-11-2004, 05:40 PM
Error on page is displayed in left hand corner of the status bar when I click the button to initiate message().



<BODY>

<form action="<?php echo $editFormAction; ?>
" name="scroll_frm" id="scroll_frm" method="POST">
<table width="280" border="1" bordercolor="#996666">
<tr bordercolor="#FFFFFF">
<td height="60" colspan="2">

<marquee behavior=scroll direction="left"
bgcolor="white" id="wibble" style="font-size:28px">
</marquee>

<div align="left">

<script language="JavaScript" type="text/javascript">

function message(){
var hidEle=document.form['scroll_frm']['HiddenMessage'];

var obj=document.getElementById("wibble");
var message=prompt("Please type a message (200 character max)","");


obj.innerHTML=message;
hidEle.value=message;
}
</script>

<input name="write " type="button" id="write "
onClick="message(); " value="Clcik to type text">
<input name="HiddenMessage" type="hidden" id="HiddenMessage" />
<input type="hidden" name="MM_insert" value="scroll_frm">
</form>

SpiritualStorms
08-11-2004, 05:45 PM
Did you click on the yellow triangle by the status bar? Generally, if you click on it, it will pop up a debugger screen. In the debugger screen, it will tell you the line at which the code is erroring, and the generally the charactor position at which it errors. What does it say?

antler
08-11-2004, 06:12 PM
'document.form.scroll_frm' is null or not an object.

dumpfi
08-11-2004, 06:25 PM
As stated in the 3rd post:

var hidEle=document.forms['scroll_frm']['HiddenMessage'];

dumpfi

SpiritualStorms
08-12-2004, 03:38 AM
Out of curiosity, why are you placing your function inside the body section of your document? As a general rule, you store/place most of your functions in either an external file, or you put them in the head section of your document.

Did you resolve your prob?

antler
08-12-2004, 01:17 PM
I did not know it was wrong to place functions in the body.

I just placed the code mostly in the order it would run. I often do the same with php.

Yes Thanks I solved my problem.

Thanks for all your help guys! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum