JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2004, 12:53 PM
dan22k dan22k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 32 dan22k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 26 m 30 sec
Reputation Power: 9
passing an array via a form then validating

Hi,

I'm using php, javascript, and mysql. I have a text input type that is generated inside a for loop and it's name is an array. Depending on what is in the database is how many inputs there will be. My problem is getting the javascript to recognize the array in order to validate it.

Code:
<script language="JavaScript">
function validate_form ( )
{
        valid = true;

        var num = document.order_form.numresults.value ;
        
        for(var i=1; i <= num; i++) { 

        	 if (document.order_form.order[i].value == "")

	        	{
	                alert ( "Please fill in all of the order # boxes.  Make sure no numbers are repeated." );
	                valid = false;
	        	} 
        }  


    return valid;
}
</script>	
</head>
<body>
	
<?php	
		      
	$query = 'SELECT * '
        . ' FROM `MainGroup` '
        . ' ORDER BY List ASC ';
	$result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");
	$num_results = mysql_num_rows($result);

	echo "The number of rows are $num_results";
?>
	<table width=100% border=1 cellpadding=2 cellspacing=2>
    <form onSubmit="return validate_form ( );" name="order_form" action="grouporder.php" method="post" enctype="multipart/form-data">
		<INPUT TYPE="hidden" NAME="numresults" VALUE="<?php echo $num_results ?>">
		<tr>
			<td width=50>Order #</td>
			<td><b>GroupName</b></td>
			<td><b>GroupID</b></td>
		</tr>
<?php 
		for ($i=1; $i <= $num_results; $i++)
		  {
		     $row = mysql_fetch_array($result);
		     echo '<tr><td width=50>';
?>
		     	 <input type=text name="<?php echo "order[$i]"; ?>" value="<?php echo $row['List']; ?>" size="3" maxlength="3">
<?php
		     echo '</td>';
		     echo '<td>';
		     echo $row['GroupName'];
		     echo '</td>';
		     echo '<td>';
		     echo stripslashes($row['GroupID']);
		     echo '</td></tr>';
		  }
		
?>
			<tr>
				<td colspan=3 align=left><input type="submit" value="Submit"><INPUT TYPE=button  VALUE="Click here to display form values" ONCLICK="validate_form ( )"></td>
			</tr>
	</form>
	</table>
		
</body>
</html>


The error given is: 'document.order_form.order' is null or not an object

I've tried a few different ways to have the javascript recognize the order[] array, but have been unsuccessful.

I'd appreciate any suggestions.

Thanks,

dan

Reply With Quote
  #2  
Old June 24th, 2004, 02:52 PM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 14
Send a message via AIM to jacktasia
does it have to be an array? it would be easier to do on the javascript side if you just have them like order1,order2,order3,order4,etc...and assigned them ids instead of names and then you could just do something like:


Code:
for (var i;i<=AmountOf;i++) {
if (document.getElementById('order'+i).value=="") {
//some code
}
}


so in your php code you just do the same kind of except use the 'order'.$i ...concat method to make each object in the loop...

does that makes sense? or will that mess too much with how you are receiving the info back?

Reply With Quote
  #3  
Old June 24th, 2004, 04:18 PM
dan22k dan22k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 32 dan22k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 26 m 30 sec
Reputation Power: 9
It's definately a good idea, but I think it will mess too much with the way I get the info back. It doesn't have to be an array, but the information is dynamic and i'm using the information to update a table in my database. So at any given time, order$i can be as low as order2 or as high as order75. Plus, it's one thing to concatanate strings, but then I would have to put together the variables up to a given number on the receiving side, which I don't think is possible to my knowledge. It could be possible, but I don't know how to do it. Is there a way to or any other way to solve the original issue?

thanks,

dan

Reply With Quote
  #4  
Old June 24th, 2004, 04:34 PM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 14
Send a message via AIM to jacktasia
Quote:
Originally Posted by dan22k
It's definately a good idea, but I think it will mess too much with the way I get the info back. It doesn't have to be an array, but the information is dynamic and i'm using the information to update a table in my database. So at any given time, order$i can be as low as order2 or as high as order75. Plus, it's one thing to concatanate strings, but then I would have to put together the variables up to a given number on the receiving side, which I don't think is possible to my knowledge. It could be possible, but I don't know how to do it. Is there a way to or any other way to solve the original issue?

thanks,

dan


i hate to dwell on my first idea, but as far as getting your info back from the user..couldn't you do something like this:

PHP Code:
 1. numresults has total amount of textboxes
2. 
for loop all the way through numresults
3. body of 
for loop something like $order[$i] = _POST['order'+$i];
4. then you will have your array to use with the database
5. wouldn
't that work? 

Reply With Quote
  #5  
Old June 25th, 2004, 02:49 PM
dan22k dan22k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 32 dan22k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 26 m 30 sec
Reputation Power: 9
The JavaScript issues are completed, that works. However, I keep getting a parse error on the line

$order[$i] = _POST['order'+$i];

I've tried a few combinations, but i'm having trouble finding something that works. Basically, in a for loop I need $order[1] = _POST['order1'];, $order[2] = _POST['order2']; , upto n for $i.

Any ideas? I know this is more php than javascript, but i thought someone looking on this whole thread may have an idea.

Thanks.

Reply With Quote
  #6  
Old June 25th, 2004, 02:57 PM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 14
Send a message via AIM to jacktasia
shouldn't it be:

Code:
$order[$i] = $_POST['order'.$i];


i was mixing javascript and php when i did my pseudocode in my last post.sorry.... above code should work.

Reply With Quote
  #7  
Old June 25th, 2004, 03:19 PM
dan22k dan22k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 32 dan22k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 26 m 30 sec
Reputation Power: 9
You got it. Thanks so much.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > passing an array via a form then validating

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap