Jump to content

Javascript getting Form Value with a twist


onlyican

Recommended Posts

Hey guys

 

I have a form, and when the user hits submit, I will check the form for certain things.

 

Basic fields I can do easily by


<input type='text' name='MyName' id='MyName' value='Jamie' />

<script type='text/javascript'>

var MyName;

MyName = document.getElementById('MyName').value;

alert("MyName is " + MyName);

</script>

That works, I have a pop up with "Jamie"

 

BUT

 

I also have some text fields set out like this

 


<input type='text' name='MyValues[]' value='1' />

<input type='text' name='MyValues[]' value='2' />

<input type='text' name='MyValues[]' value='3' />

The [] makes the Value go into an Array

 

Is there anyway I can check this in JavaScript without a Page Refresh?

 

you can write a javascript function to check the values which returns either true or false depending on the check and place this in the onSubmit function of the form

 

<form name="myform1" id="myform1" type="post" action="nextform.php" onSubmit"return checkfunction();">

--> if checkfunction() returns a "false" then the page is not posted, it stops then put up
--> an alert box saying this bit is wrong etc

--> form bits

<input type="submit" name="submit">
</form>

I mean the [] makes it an array for php

I am a php guy

so if I done this in php

 

<input type='text' name='something' value='1' />
<input type='text' name='something' value='2' />
<input type='text' name='something' value='3' />

<?php

print_r($_POST["something"]);

//Output
//3
?>

 

BUT if I did

<input type='text' name='something[]' value='1' />
<input type='text' name='something[]' value='2' />
<input type='text' name='something[]' value='3' />

<?php

print_r($_POST["something"]);

//Output
//Array ([0] => "1", [1] => "2", [2] => "3")

 

if I done in javascript the following

 


<script type='text/javascript'>

function ShowValues(){
var MyValues;

alert(document.getElementById('MyValues').value);
}
</script>
<form method='post' action=''  onClick="ShowValues();">
<input type='text' name='MyValues[]' id='MyValues' value='1' />
<input type='text' name='MyValues[]' id='MyValues' value='2' />
<input type='text' name='MyValues[]' id='MyValues' value='3' />
</form>

 

I will get an Alert saying "1";

I want the array like

Array ([0] => "1", [1] => "2", [2] => "3")

*shudder*

 

You can't use the same ID more than once -- EVER.

Second, if you gave the form a name, you could use the elements collection to get at this set of inputs... like document.forms[formName].elements['MyValues[]'] ), which is going to be an array in this case.  Not that I think text inputs are the best choice.

i know I should not use the ID more than once, which is why I was checking if JavaScript can handle [] like PHP

 

I have this then

 

<script language='text/javascript' />

function FinishedEditing(){
alert(document.forms['MyForm'].elements['MyValues[]']);
}
</script>
<form method='post' action='' name='MyForm'>
<input type='text' name='MyValue[]' value='1' />
<input type='text' name='MyValue[]' value='2' />
<input type='text' name='MyValue[]' value='3' />
<input type='hidden' name='MyOrigValue[]' value='1' />
<input type='hidden' name='MyOrigValue[]' value='2' />
<input type='hidden' name='MyOrigValue[]' value='3' />

<input type="submit" onClick="FinishedEditing();" name="submit" class="loginbutton" value="Finished" />
</form>

 

And the following pop up

 

[object NodeList]

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.