13 Replies - 9072 Views - Last Post: 19 April 2011 - 03:52 PM Rate Topic: -----

#1 RGarcia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 01-April 09

Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 06:45 PM

Hello everyone:

I am working with my last unit assignment, I have already found the way to include the checkboxes into each row of my table. I am still working with the php code to display the records selected in the table to be modified. I am not sure if I need to use the foreach() according to my code or if I must use any other way such using a button for each record instead checkboxes. I don't want to give up myself with this one so here is what I have until now:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<title>Modifications</title>
</head>
<body>

<?php
include("admin.php");
?>
<h2 align="center">Modifying</h2>
<body>

<?php
//Varible declared to establish a connection
$connection = mysql_connect("localhost", "user1", "ryciorycio"); 
//Veryfing if connection is established
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  exit;
	
  }
else

//Connecting to database and table
mysql_select_db("garcia", $connection);

//Declaring a variable to hold the requested values
$result = mysql_query("SELECT LastName, FirstName, AreaCode, PhoneNumber
FROM `contacts`
ORDER BY LastName ASC
LIMIT 0 , 30");
$i = 0;
$checkboxes = "<input type='checkbox' name='selected[]' value='$i++'/><br />";
//Presenting the values in a table 
echo "<table align='center' border='1' width='40%'>";
echo "<tr><th>Modify</th><th>Name</th><th>Phone Number</th></tr>";

while($row = mysql_fetch_array($result))
  {
	$FixNumber = implode('-',str_split(substr($row['PhoneNumber'] , 0,-4),3)).'-'.substr($row['PhoneNumber'],-4);
	echo "<tr><td>$checkboxes</td><td>{$row['LastName']}, {$row['FirstName']}</td><td>({$row['AreaCode']}) $FixNumber</td></tr>";	
  }
  
echo "</table><br />";

//Closing connection
mysql_close($connection);

?>

<form action="ModifySelected.php" method="get" enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="Modify" tabindex="3"/>
</form></p>

<hr/>
<p>To modify any contact select it from the list and click <strong>"Modify"</strong> button.</p><hr width="35%"/>
</body>
</html>





I have include a picture of how it looks the webpage at the momment. I am sure this way will be much better to modify the existing records that one at a time. Please do not hesitate to ask for any other information, thanks in advance.

Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Getting values from checkboxes selected in a table.

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 06:50 PM

You need to have the checkboxes inside the form with the submit button.

The easiest way to do this, I'd say, would be to have each checkbox named something like 'user[#]' where # is the id of that entry in the database. You'd also want to change the action of the form to POST for this. Then, when you submit it, you'd simply look at the $_POST['user'] array for which ones have been selected. All you'd have to do is set each checkbox's value to something like 1. It really doesn't matter what value you set, because only the boxes you check are going to come up in the array.
Was This Post Helpful? 1
  • +
  • -

#3 RGarcia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 01-April 09

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 06:58 PM

View PostValek, on 08 July 2010 - 05:50 PM, said:

You need to have the checkboxes inside the form with the submit button.

The easiest way to do this, I'd say, would be to have each checkbox named something like 'user[#]' where # is the id of that entry in the database. You'd also want to change the action of the form to POST for this. Then, when you submit it, you'd simply look at the $_POST['user'] array for which ones have been selected. All you'd have to do is set each checkbox's value to something like 1. It really doesn't matter what value you set, because only the boxes you check are going to come up in the array.


I have tried that before, I mean when you say that I need "to have the checkboxes inside the form with the submit button" but do you know anyway I can do that without being printed in the screen as a new checkbox?
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 07:03 PM

View PostRGarcia, on 08 July 2010 - 07:58 PM, said:

do you know anyway I can do that without being printed in the screen as a new checkbox?


Not entirely sure what you mean there. I was merely suggesting putting the whole table inside the form the submit button is in. Then, when you submit it to whatever page you have handling the data, the checked checkboxes will show up in your $_POST array.

Otherwise, the submit button will submit for a form which has no elements. And your script will receive nothing.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5303
  • View blog
  • Posts: 27,188
  • Joined: 10-May 07

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 07:04 PM

Can you post the html code (or is it publicly viewable?) from this output :

$checkboxes = "<input type='checkbox' name='selected[]' value='$i++'/><br />";
...
while($row = mysql_fetch_array($result))
  {
	$FixNumber = implode('-',str_split(substr($row['PhoneNumber'] , 0,-4),3)).'-'.substr($row['PhoneNumber'],-4);
	echo "<tr><td>$checkboxes</td><td>{$row['LastName']}, {$row['FirstName']}</td><td>({$row['AreaCode']}) $FixNumber</td></tr>";	
  }
  



The reason I ask is this, I have not worked with checkboxes through PHP for a while, & I believe that the name & the id need to be unique, not the value.
Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 07:21 PM

He's using an array for his checkbox name, and that's fine. There's no collision there.
Was This Post Helpful? 1
  • +
  • -

#7 RGarcia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 01-April 09

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 07:31 PM

View Postno2pencil, on 08 July 2010 - 06:04 PM, said:

Can you post the html code (or is it publicly viewable?) from this output :

$checkboxes = "<input type='checkbox' name='selected[]' value='$i++'/><br />";
...
while($row = mysql_fetch_array($result))
  {
	$FixNumber = implode('-',str_split(substr($row['PhoneNumber'] , 0,-4),3)).'-'.substr($row['PhoneNumber'],-4);
	echo "<tr><td>$checkboxes</td><td>{$row['LastName']}, {$row['FirstName']}</td><td>({$row['AreaCode']}) $FixNumber</td></tr>";	
  }
  



The reason I ask is this, I have not worked with checkboxes through PHP for a while, & I believe that the name & the id need to be unique, not the value.

I am attaching all my code here for the entire application hoping this help to guide everybody.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 07:36 PM

I still think you ought to try this to start with, so your checkboxes are making it through to your ModifySelected.php script.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<title>Modifications</title>
</head>
<body>

<?php
include("admin.php");
?>
<h2 align="center">Modifying</h2>
<body>

<?php
//Varible declared to establish a connection
$connection = mysql_connect("localhost", "user1", "ryciorycio"); 
//Veryfing if connection is established
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  exit;
	
  }
else

//Connecting to database and table
mysql_select_db("garcia", $connection);

//Declaring a variable to hold the requested values
$result = mysql_query("SELECT LastName, FirstName, AreaCode, PhoneNumber
FROM `contacts`
ORDER BY LastName ASC
LIMIT 0 , 30");
$i = 0;
$checkboxes = "<input type='checkbox' name='selected[]' value='$i++'/><br />";
//Presenting the values in a table
echo "<form action='ModifySelected.php' method='post' enctype='application/x-www-form-urlencoded'>";
echo "<table align='center' border='1' width='40%'>";
echo "<tr><th>Modify</th><th>Name</th><th>Phone Number</th></tr>";

while($row = mysql_fetch_array($result))
  {
	$FixNumber = implode('-',str_split(substr($row['PhoneNumber'] , 0,-4),3)).'-'.substr($row['PhoneNumber'],-4);
	echo "<tr><td>$checkboxes</td><td>{$row['LastName']}, {$row['FirstName']}</td><td>({$row['AreaCode']}) $FixNumber</td></tr>";	
  }
  
echo "</table><br />";

//Closing connection
mysql_close($connection);

?>

<p><input type="submit" value="Modify" tabindex="3"/>
</form></p>

<hr/>
<p>To modify any contact select it from the list and click <strong>"Modify"</strong> button.</p><hr width="35%"/>
</body>
</html>


This post has been edited by Valek: 08 July 2010 - 07:38 PM

Was This Post Helpful? 2
  • +
  • -

#9 RGarcia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 01-April 09

Re: Getting values from checkboxes selected in a table.

Posted 10 July 2010 - 09:00 AM

View PostValek, on 08 July 2010 - 06:36 PM, said:

I still think you ought to try this to start with, so your checkboxes are making it through to your ModifySelected.php script.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<title>Modifications</title>
</head>
<body>

<?php
include("admin.php");
?>
<h2 align="center">Modifying</h2>
<body>

<?php
//Varible declared to establish a connection
$connection = mysql_connect("localhost", "user1", "ryciorycio"); 
//Veryfing if connection is established
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  exit;
	
  }
else

//Connecting to database and table
mysql_select_db("garcia", $connection);

//Declaring a variable to hold the requested values
$result = mysql_query("SELECT LastName, FirstName, AreaCode, PhoneNumber
FROM `contacts`
ORDER BY LastName ASC
LIMIT 0 , 30");
$i = 0;
$checkboxes = "<input type='checkbox' name='selected[]' value='$i++'/><br />";
//Presenting the values in a table
echo "<form action='ModifySelected.php' method='post' enctype='application/x-www-form-urlencoded'>";
echo "<table align='center' border='1' width='40%'>";
echo "<tr><th>Modify</th><th>Name</th><th>Phone Number</th></tr>";

while($row = mysql_fetch_array($result))
  {
	$FixNumber = implode('-',str_split(substr($row['PhoneNumber'] , 0,-4),3)).'-'.substr($row['PhoneNumber'],-4);
	echo "<tr><td>$checkboxes</td><td>{$row['LastName']}, {$row['FirstName']}</td><td>({$row['AreaCode']}) $FixNumber</td></tr>";	
  }
  
echo "</table><br />";

//Closing connection
mysql_close($connection);

?>

<p><input type="submit" value="Modify" tabindex="3"/>
</form></p>

<hr/>
<p>To modify any contact select it from the list and click <strong>"Modify"</strong> button.</p><hr width="35%"/>
</body>
</html>



Thanks to your recommendations I am very close to finaly show what I want. But I am still having problems to show all the selected rows, its only shows the last selected row and not showing the all. Here is the code for the php that will show the selected values, before was ModifySelected.php but I have changed to ShowSelected.php. After all records are changed, the user will be redirected to the principal php where the entire list of contacts is shown.

Anyone who have the knowledge can reply to this response, please provide useful information, thanks in advance.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<title>Untitled Document</title>
</head>

<body>
<?php
include("admin.php");
?>
<h2 align="center">Making Changes</h2>

<?php
//Varible declared to establish a connection
$connection = mysql_connect("localhost", "user1", "ryciorycio"); 
//Veryfing if connection is established
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  exit;
	
  }
else

//Connecting to database and table
mysql_select_db("garcia", $connection);

//Declaring a variable that count the requested values

$size = count($_POST['selected']);
$i = 0;

while ($i < $size) 
{
// Variable to hold the Phone Number value of each selected record.
$Modify = $_POST['selected'][$i];
++$i;
}
// Declaring a variable to hold the requested values
$result = mysql_query("SELECT *
FROM `contacts` 
WHERE PhoneNumber = '$Modify'
ORDER BY LastName ASC
LIMIT 0 , 30");

if(mysql_num_rows($result)==0)
	{
	// If there is no value then echo 
	echo "<hr /><div>There were no contact chosen, please choose at least one and try again!</div>";
	}
	// In case there are one or more values in database do what is bellow
	else
	{
	echo "<table align='center' border='1' width='40%'>";
	// Defining columns
	echo "<tr><th>First Name</th><th>Last Name</th><th>Street</th><th>City</th><th>State</th><th>Zip Code</th><th>Area Code</th><th>Phone Number</th></tr>";
		// While database keeps returning values
		while($row = mysql_fetch_array($result))
		{
		// Each row will present all the existing information for the selected number. 
		echo "<tr><td><input type='text' name='first_name' size='12' value='{$row['FirstName']}'/></td><td><input type='text' name='last_name' size='12' value='{$row['LastName']}'/></td><td><input type='text' name='street' size='34' value='{$row['Street']}'/></td><td><input type='text' name='city' size='15' value='{$row['City']}'/></td><td><input type='text' name='state' size='15' value='{$row['State']}'/></td><td><input type='text' name='zip_code' size='8' value='{$row['ZipCode']}'/></td><td><input type='text' name='area_code' size='4' value='{$row['AreaCode']}'/></td><td><input type='text' name='phone_number' size='15' value='{$row['PhoneNumber']}'/></td></tr>";
++$i;
  }
  
echo "</table><br />";
	}
mysql_close($connection);
?>
</body>
</html>



I am getting bald with this, is there any other recommendation? I will appreciate it, thanks in advance.

This post has been edited by RGarcia: 10 July 2010 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 RGarcia  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 01-April 09

Re: Getting values from checkboxes selected in a table.

Posted 10 July 2010 - 11:12 AM

Can anyone else provide me with useful information about my question? Now I can show all the selected records only if they are selected from the first one in order without leaving anyone outside.

For example if I chose from the second one and above I received this error:

Notice: Undefined offset: 0 in C:\wamp\www\ShowSelected.php on line 48

Does anyone know why? What would be the best way to do this?
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Getting values from checkboxes selected in a table.

Posted 10 July 2010 - 03:32 PM

while ($i < $size)
{
     // Variable to hold the Phone Number value of each selected record.
     $Modify = $_POST['selected'][$i];
     ++$i;
}


Do you see what I see?

I see $Modify getting constantly overwritten with the next one in line, until the loop finishes. Then, when you run your query, it only runs on the last one, because that is in fact the only query you're sending.
Was This Post Helpful? 1
  • +
  • -

#12 billj4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 15-April 11

Re: Getting values from checkboxes selected in a table.

Posted 19 April 2011 - 03:23 AM

View PostValek, on 10 July 2010 - 03:32 PM, said:

while ($i < $size)
{
     // Variable to hold the Phone Number value of each selected record.
     $Modify = $_POST['selected'][$i];
     ++$i;
}


Do you see what I see?

I see $Modify getting constantly overwritten with the next one in line, until the loop finishes. Then, when you run your query, it only runs on the last one, because that is in fact the only query you're sending.



For give me for inquirying in on this because this is all new to me..but if I understand you correctly " Valek" you are saying that this


while ($i < $size)
{
     // Variable to hold the Phone Number value of each selected record.
     $Modify = $_POST['selected'][$i];
     ++$i; <<< This should not be here..this is overwritting previous command???
}


Am I right on this..if not(sorry for intrusion of post)...oh well..this is from someone who is still in process of still learning..but you guys help on this alot..

thanks..

This post has been edited by billj4u: 19 April 2011 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Getting values from checkboxes selected in a table.

Posted 19 April 2011 - 04:40 AM

nope, he meant the line above.

for demonstration run this
$i = 0;
while ($i < 10)
{
    $x = $i;
    echo $x . "|";
    $i++;
}
echo " final: " . $x;

This post has been edited by Dormilich: 19 April 2011 - 04:42 AM

Was This Post Helpful? 1
  • +
  • -

#14 billj4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 15-April 11

Re: Getting values from checkboxes selected in a table.

Posted 19 April 2011 - 03:52 PM

View PostDormilich, on 19 April 2011 - 04:40 AM, said:

nope, he meant the line above.

for demonstration run this
$i = 0;
while ($i < 10)
{
    $x = $i;
    echo $x . "|";
    $i++;
}
echo " final: " . $x;


Okay thanks..I see I have a long ways to go..so I guess the best thing to do is start all over from the beginning...

But I will be here around to see what I can learn from all here...thanks...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1