Hi there i m having some problems in creating and handling multiple buttons. The idea is that i have two buttons "Accept" and "Reject" and i want to give them differnt names using the variable $i from the for loop. Below i give you the code which shows what i am trying to do. The code has wrong syntax on purpose, in order to depict what i am trying to do!!

for ($i = 0; $i < count_req($getuser[0]['email']); $i++){
			echo $req_array[$i]['s_fname']." ".$req_array[$i]['s_lname'];
			echo "<form><input type='submit$i' name='accept' value='Accept'>
				<input type='submit' name='reject$i' value='Reject'><br></form>";

				echo "Request".count($_REQUEST);
				if($_REQUEST['accept$i'] == 'Accept')
				{
					echo "Accept";
  					accept($getuser[0]['email'],$req_array[$i]['s_email']);		
				}
				else if($_REQUEST['reject$i'] == 'Reject')
				{
					echo "Reject";
  					reject($getuser[0]['email'],$req_array[$i]['s_email']);
				}
		}

I just put $i to the name of the button and in the if statement. I want each button to have it's own name, otherwise all the accept and reject buttons work simultaneously. I'm open to any suggestion. Thanks in advance....

1) Do you want to update database in the process form. (you did not mention form action and form method)

2) You can also keep hidden value before button element may be id of table or email id or user id which ever is primary key of table, so that hidden value is also posted with submit

3) no need of $i name in button name/id as you are using sepearte form element for each button pair (accept/reject). You can keep same name (that will make your work easy in action page.

1)I am using _REQUEST[] which includes _GET and _POST. In the IF statement i use the functions accept and reject repsectively in order to update my database. I think it works fine.

2)The email is the primary key, but in which way can i keep it as a hidden value so as every time i push a button only one action is performed and not all of them.

3)Using id in the form what name should i give?

echo "<form id =".$i."> some code here... <form>";

I am new to php and web devopment so excuse me for my unawareness...

for ($i = 0; $i < count_req($getuser[0]['email']); $i++){
	echo "<form method='post'>
	<input type='hidden' name='id' value='{$i}'/>
	<input type='submit' name='action' value='Accept'/>
	<input type='submit' name='action' value='Reject'/>
	<br/>
	</form>";
}
if(ISSET($_POST['id']) && ctype_digit($_POST['id']) && ISSET($_POST['action'])){
	$id = $_POST['id'];
	$action = $_POST['action'];
	if($action == 'Accept'){
		//Do whatever accept does with $id
	}elseif($action == 'Reject'){
		//Do whatever reject does with $id
	}else{
		//$action has a different value
		var_dump($action);
	}
}else{
	//vars didnt pass
	var_dump($_POST['id']);
	var_dump($_POST['action']);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.