javascript incompatible with php ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

javascript incompatible with php ?

Post 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 ??
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try using this instead:

Code: Select all

document.getElementsByName('reason[]')
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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];
Post Reply