Page 1 of 1

JavaScript & PHP arrays ...

Posted: Sat Nov 02, 2002 6:27 am
by PaTTeR
Hi there,
i have a form

Code: Select all

<form action="blabla.php" action="POST">
  <input name="sub_form&#1111;name]" />
  <input name="sub_form&#1111;surname]" />
..............
</form>
With this form in php i have an array $_POST['form'] with elements name and surname. This is O.K.

I can't validate this form via JavaScript

Code: Select all

....
if (inForm.sub_form&#1111;name].value =="") &#123;alert("Please fill name"); return false;&#125;;
....
becouse an error "name is not defined"

When change JavaScript whit this:

Code: Select all

...
if (inForm.elements&#1111;1].value =="" ) &#123;alert("Please fill name"); return false;&#125;; 
...

everything works fine.

sub_form[name] is the name of input element, but JavaScript looks for array 'sub_form' with 'name' element.

Any idea how to solve this ? I need to tell JavaScript that 'sub_form[name]' is the name io element, not variable.

Posted: Sun Nov 03, 2002 4:09 am
by volka
you may use getElementById() to retrieve a reference to the input-field

Code: Select all

<form action="blabla.php" action="POST">
  <input name="sub_form&#1111;name]" id="sub_form_name"/>
  <input name="sub_form&#1111;surname]" id="sub_form_surname"/>
..............
</form>

Code: Select all

oInput = getElementById("sub_form_name");
if (oInput.value =="") &#123;alert("Please fill name"); return false;&#125;;

Posted: Sun Nov 03, 2002 6:31 am
by PaTTeR
Yes Yes it's work ;-)

Code: Select all

oInput = document.getElementById("name");
10x volka ;-)