JavaScript & PHP arrays ...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

JavaScript & PHP arrays ...

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;;
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post by PaTTeR »

Yes Yes it's work ;-)

Code: Select all

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