469,600 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

How do I concat variables to change the javascript document object

My Javascript is rather rusty :( ... and I need to do change some form
values, in the folowing way:

(1).
I have the following a href (wrapped in PHP), which calls processForm.

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

href="javascript:void(0)" onClick="processForm(\'' . $form_name
.. '\', \'move\')
1
2 function processForm(formName,hidVal) {
3 document.formName.myHiddenValue.value = hidVal;
4 document.formName.submit();
5 }
====

Can someone please tell me how to incorporate the formName variable,
so I can reference the "document.****formName****.myHiddenValue.value "
correctly.

I then need call form.submit(); in a similar way.

(2)

I originally had the following code (which didn't work with later
version of apache):
====
echo ('&nbsp;&nbsp;<A class=NavBarFont href="#" onClick="document.' .
$form_name . '.myHiddenValue.value=\'delete\';document.' . $form_name
.. '.submit();">delete</a>' . "\n");
====

Can someone please tell me if there is a better way of writing this
bit of Javascript. I am open to suggestions. I cannot believe, I
cannot remember how to mix variables and ojb references, but I have
tried eval, and lots of other things.

Any help is greatly appreciated. :)

thanks

Miles.
Jul 20 '05 #1
2 5926
Miles Davenport wrote:
My Javascript is rather rusty :( ... and I need to do change some form
values, in the folowing way:

(1).
I have the following a href (wrapped in PHP), which calls processForm.

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

href="javascript:void(0)" onClick="processForm(\'' . $form_name
. '\', \'move\')
1
2 function processForm(formName,hidVal) {
3 document.formName.myHiddenValue.value = hidVal;
4 document.formName.submit();
5 }
====

Can someone please tell me how to incorporate the formName variable,
so I can reference the "document.****formName****.myHiddenValue.value "
correctly.

I then need call form.submit(); in a similar way.

(2)

I originally had the following code (which didn't work with later
version of apache):
====
echo ('&nbsp;&nbsp;<A class=NavBarFont href="#" onClick="document.' .
$form_name . '.myHiddenValue.value=\'delete\';document.' . $form_name
. '.submit();">delete</a>' . "\n");
====

Can someone please tell me if there is a better way of writing this
bit of Javascript. I am open to suggestions. I cannot believe, I
cannot remember how to mix variables and ojb references, but I have
tried eval, and lots of other things.

Any help is greatly appreciated. :)

thanks

Miles.

Miles,

This group only deals with Javascript, and cannot comment on your PHP.
Please either give us a URL where we can find the code, or give us the
PHP output (from the browser, view source) in pure HTML/Javascript. If
your server is not putting the code out correctly, there is a problem
with your PHP (or server), and we cannot help you. You might want to
ask a PHP group.

Thanks,
Brian
Jul 23 '05 #2
On 1 Apr 2004 08:07:04 -0800, Miles Davenport <mi****@chocksaway.com>
wrote:

[snip]
href="javascript:void(0)" onClick="processForm(\'' . $form_name
. '\', \'move\')
Read the FAQ, specifically:

<URL:http://jibbering.com/faq/#FAQ4_24>
1
2 function processForm(formName,hidVal) {
3 document.formName.myHiddenValue.value = hidVal;
4 document.formName.submit();
5 }
document.forms[ formName ].myHiddenValue.value = hidVal;
document.forms[ formName ].submit();

However, as you refer the object more than once, it would be more
efficient to perform the lookup once and store the reference:

var form = document.forms[ formName ];
form.myHiddenValue.value = hidVal;
form.submit();

[snip]
echo ('&nbsp;&nbsp;<A class=NavBarFont href="#" onClick="document.' .
$form_name . '.myHiddenValue.value=\'delete\';document.' . $form_name
. '.submit();">delete</a>' . "\n");

Can someone please tell me if there is a better way of writing this
bit of Javascript.
A better way would be to use:

<button type="submit" name="myHiddenValue"
value="delete">Delete</button>

inside the relevant form, with "myHiddenValue" changed to "operation" or
something similarly descriptive.

This presents a consistent UI (links for links, buttons for actions) and
avoids unnecessary use of JavaScript. If you must use a link, aim for:

function delete( form ) {
form.myHiddenValue.value = 'delete';
form.submit();
}
...
<a href="" onclick="delete(document.formName);return false;"
class="NavBarFont">delete</a>

replacing "formName" using PHP as appropriate. As Brian said in his post,
you shouldn't include PHP in posts to this group. If document content is
dynamically generated, you can state that, but show actual values that the
client (and JavaScript) sees, not the code that produces it. Very few
people may understand your flavour of server-side language, limiting the
number of people that can help you.
I am open to suggestions. I cannot believe, I cannot remember how to
mix variables and ojb references, but I have tried eval, and lots of
other things.


You should never use eval() for things like this. In fact, there is almost
always a better way than using eval().

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Judy M | last post: by
3 posts views Thread by Chris | last post: by
3 posts views Thread by jimmygoogle | last post: by
2 posts views Thread by justplain.kzn | last post: by
1 post views Thread by es_ | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.