How do I concat variables to change the javascript document object

  • Thread starter Miles Davenport
  • Start date
M

Miles Davenport

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.
 
B

Brian Genisio

Miles said:
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
 
M

Michael Winter

On 1 Apr 2004 08:07:04 -0800, Miles Davenport <[email protected]>
wrote:

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

Read the FAQ, specifically:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top