Page 1 of 1

javascript incompatible with php ?

Posted: Thu Nov 09, 2006 10:01 pm
by winsonlee
The following script works for javascript but the problem is my php script can only capture a value from one reason text box

Code: Select all

<script language="JavaScript">

<!--

   for(i=0; i< document.ptform.reason.length; i++){
     total = total + parseInt(document.ptform.reason[i].value);
   }
	 if(total != parseInt(document.ptform.rejected.value)){
		   str = str + "Reject Qty and Fault Entered does not match.\n";
	 }

//-->

</script>

<form name='ptform'  action="process.php" method="POST" onSubmit="return validate_form(this);">
<input type='text' class='textc' value='' name='reason'>
<input type='text' class='textc' value='' name='reason'>
<input type='text' class='textc' value='' name='reason'>
<input type='text' class='textc' value='' name='reason'>
</form>




The following script works fine for php as i am able to captured all the value in the text field but my javascript refuse to work.

Code: Select all

<script language="JavaScript">

<!--

   for(i=0; i< document.ptform.reason.length; i++){
     total = total + parseInt(document.ptform.reason[i].value);
   }
	 if(total != parseInt(document.ptform.rejected.value)){
		   str = str + "Reject Qty and Fault Entered does not match.\n";
	 }

//-->

</script>

<form name='ptform'  action="process.php" method="POST" onSubmit="return validate_form(this);">
<input type='text' class='textc' value='' name='reason[]'>
<input type='text' class='textc' value='' name='reason[]'>
<input type='text' class='textc' value='' name='reason[]'>
<input type='text' class='textc' value='' name='reason[]'>
</form>


Any solution that I can use to use both at the same time ??

Posted: Thu Nov 09, 2006 10:03 pm
by Burrito
try using this instead:

Code: Select all

document.getElementsByName('reason[]')

Posted: Thu Nov 09, 2006 11:44 pm
by Zoxive
Yes try what Burrito said, but its doing this because they are all named the same, so they just are overwriting each other. Another way to try is naming them "reason[]". Theres 2 basic ways to do this, one is easier for php to recognize it, and one thats easier for javascript.

Posted: Fri Nov 10, 2006 12:56 am
by timvw
Use reasonX as name instead... Then you can access them with both JavaScript and PHP...

var reason = document.getElementByName("reason" + x);
$reason = $_POST['reason' . $x];