multiple form controls with same name

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

multiple form controls with same name

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

By looping the CreateCat array.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

by using count?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

example please. some of these numbers are like 1 4 9. they dont go in order or start from 0.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

that does nothing on server side.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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)) ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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:
Post Reply