Page 1 of 1

multiple form controls with same name

Posted: Fri Feb 23, 2007 2:25 pm
by psychotomus
I got multiple submit buttons with same name CreateCat and i seperate them by CreateCat[#]. how do i tell which button is pushed?

Posted: Fri Feb 23, 2007 2:26 pm
by RobertGonzalez
By looping the CreateCat array.

Posted: Fri Feb 23, 2007 2:32 pm
by psychotomus
by using count?

Posted: Fri Feb 23, 2007 3:10 pm
by psychotomus
example please. some of these numbers are like 1 4 9. they dont go in order or start from 0.

Posted: Fri Feb 23, 2007 3:17 pm
by hawleyjr

Code: Select all

<script language="JavaScript">
function checkMe( tVal ){
	alert( tVal );
}
</script>

Code: Select all

<form action="index.php" method="get" name="form1">
	<input name="CreateCat" type="button" value="1" onclick="checkMe( this.value );" />
	
	<input name="CreateCat" type="button" value="2" onclick="checkMe( this.value );" />
	
	<input name="CreateCat" type="button" value="3" onclick="checkMe( this.value );" />
	
	<input name="CreateCat" type="button" value="4" onclick="checkMe( this.value );" />
	
	<input name="CreateCat" type="button" value="5" onclick="checkMe( this.value );" />

</form>

Posted: Fri Feb 23, 2007 3:26 pm
by psychotomus
that does nothing on server side.

Posted: Fri Feb 23, 2007 3:53 pm
by hawleyjr
psychotomus wrote:that does nothing on server side.
You didn't mention server side. ;)

Code: Select all

echo '<HR><PRE>'; print_r($_POST); echo '</PRE>';
echo '<HR><PRE>'; print_r($_GET); echo '</PRE>';

Posted: Fri Feb 23, 2007 5:13 pm
by RobertGonzalez
psychotomus wrote:by using count?
That should be part of it. Imagine that you have an array. You want to look through that array to check a value.

I think you are going to run in to issues with regular consecutive number index looping though, if the CreateCat indexes are not sequential.

Posted: Fri Feb 23, 2007 5:17 pm
by psychotomus
ok. i managed to find some code that will get the id of the control

Code: Select all

list($cat_id) = each($_POST['CreateForum']);
	$cat_id = intval($cat_id);
	$forumname = stripslashes($_POST['textForumName'][$cat_id]);
i get an error if its not posting any data
Warning: Variable passed to each() is not an array or object in /mounted-storage/home40b/sub002/sc18478-RGIJ/mywebsite/admin/forum-manage.php on line 20

i tried adding an

Code: Select all

if(isset($_POST['CreateForum']))
but that didn't help. is there a way to tell if a form is just being posted? like if(isset($_POST)) ?

Posted: Fri Feb 23, 2007 5:42 pm
by RobertGonzalez
psychotomus wrote:i get an error if its not posting any data
Warning: Variable passed to each() is not an array or object in /mounted-storage/home40b/sub002/sc18478-RGIJ/mywebsite/admin/forum-manage.php on line 20

i tried adding an

Code: Select all

if(isset($_POST['CreateForum']))
but that didn't help. is there a way to tell if a form is just being posted? like if(isset($_POST)) ?
You can use is_array(), or you can var_dump() or print_r() before looping it to see what is in it. And if you try to use isset($_POST)... well, why not try it and tell us if it will work? :wink: