Cannot store selected form row items in variables

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
BenjaminDisco
Forum Newbie
Posts: 1
Joined: Sat Aug 02, 2014 8:10 am

Cannot store selected form row items in variables

Post by BenjaminDisco »

Hi guys,

Sorry im a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.

I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.

My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?

Thanks

//function.php

Code: Select all

function getRequestors($tableName, $conn)
{
	try {
		$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)

			? $result
			: false;

	} catch(Exception $e) {
		return false;
	}
}
//form.php

Code: Select all

<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
	<tr>
			<th>Name</th>
			<th>Number Entered</th>
	<tr/>
	<tr>

<?php foreach($users as $user) : ?>

	  <td width="30%" name="FullName"><?php echo $user['FullName']; ?></td>
	  <td width="30%"><input type="int" name="NumberedEntered"></td>
	</tr>
<?php endforeach; ?>
</table>
	  <input type="submit" value="submit"></td>
</form>
//index.php

Code: Select all

if ( $_REQUEST['NumberedEntered']) {
	echo $_REQUEST['NumberedEntered'];
	echo $_REQUEST['FullName'];
}
Last edited by Celauran on Sat Aug 02, 2014 8:20 am, edited 1 time in total.
Reason: Please wrap your code in syntax tags.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cannot store selected form row items in variables

Post by Celauran »

You're reusing names inside your form, each overwriting the previous. You want arrays instead. Something like:

Code: Select all

<?php foreach ($users as $user): ?>
	<input type="text" name="FullName[]" value="<?= $user['FullName']; ?>">
	<input type="text" name="NumberEntered[]">
<?php endforeach; ?>
You could also key it by user ID or something.

You will need to change your index up a little, also, as it's going to get the whole table returned. If you're only interested in those with NumberEntered specified, iterate over $_POST['NumberEntered'] and display only those with a value.
Post Reply