The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
passing an array via a form then validating
Discuss passing an array via a form then validating in the JavaScript Development forum on Dev Shed. passing an array via a form then validating JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 24th, 2004, 12:53 PM
|
|
Contributing User
|
|
Join Date: Jun 2004
Posts: 32
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
|

June 24th, 2004, 02:52 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559
  
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 14
|
|
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?
|

June 24th, 2004, 04:18 PM
|
|
Contributing User
|
|
Join Date: Jun 2004
Posts: 32
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
|

June 24th, 2004, 04:34 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559
  
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 14
|
|
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?
|

June 25th, 2004, 02:49 PM
|
|
Contributing User
|
|
Join Date: Jun 2004
Posts: 32
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.
|

June 25th, 2004, 02:57 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559
  
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 14
|
|
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. 
|

June 25th, 2004, 03:19 PM
|
|
Contributing User
|
|
Join Date: Jun 2004
Posts: 32
Time spent in forums: 4 h 26 m 30 sec
Reputation Power: 9
|
|
|
You got it. Thanks so much.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|