checkbox validation / PHP array function

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

Locked
User avatar
nehrav
Forum Commoner
Posts: 38
Joined: Sun Sep 20, 2009 6:55 am
Location: New Delhi
Contact:

checkbox validation / PHP array function

Post by nehrav »

Hi guyz,
I am back with a new problem.... :roll:

Problem : When I check one checkbox & click UPDATE, page move to Update.php with one field (to be updated).
But even after checking 2 checkboxes, page is posting 2 values to update.php but showing one field only.....
http://localhost/test/update.php?id=48&id=49

posting the values but action is not done :banghead: :banghead:

I want the same number of fields to be shown, which are checkboxed on edit page.


Hope I clear the issue...

Please check attach images also....
I have 2 pages edit.php and update.php :!:

Edit page contain the value from database repeated with checkbox, update delete button using while loop..

Code for Edit page

Code: Select all

 
<?php
    $con = mysql_connect("xxxxx","xxx","xxxxxxxxx");
    $db = mysql_select_db("test",$con); 
 
    $select = mysql_query("select * from test_tbl");    
?>
<form name="editForm" action="delete.php" method="GET">
<table border='0' cellpadding='4' cellspacing='0' align='center' width='100%'>
            <tr bgcolor="#24a1b6">
                <td>&nbsp;</td>
                <td colspan="3">Location</td>
            </tr>
            
    <?php   
        while($result = mysql_fetch_array($select))
            {
                $id = $result['id'];
                $location = $result['location'];
            
            echo "<tr>
                <td><input type='checkbox' name='id' value='$id'></td>
                <td>$location</td>
                <td align='center'><input type='submit' value='Delete'></td>
                <td align='right'><input type='Submit' value='Update' onclick=\"document.editForm.action='update.php'; return true;\"></td>
                
              </tr>";
              }
    ?>
    </table>
</form>
 
and Update page have input field with passed value and update button
Code for update page :

Code: Select all

 
<?php
    $con = mysql_connect("xxxxx","xxx","xxxxxxxxx");
    $db = mysql_select_db("test",$con);
    
    $new_id = $_GET['id'];
    $update = mysql_query("select * from test_tbl WHERE id='$new_id'"); 
?>
 
<html>
<head>
<title>Update Page</title>
</head>
<body>
<form action="updateaction.php" method="get" name="EditAddForm">
        <table border="0" cellpadding="4" cellspacing="0" align="center" width="100%">
          <tr bgcolor="#24a1b6" class="whitetext">
                <td width="28%">Location</td>
            </tr>
            <tr>
            <td height="5" colspan="8"></td>
          </tr>       
    <?php
            
            while($uresult = mysql_fetch_array($update))
            {
                $id = $uresult['id'];
                $location = $uresult['location'];
                
          echo "<tr>
          <input type='hidden' name='id' value='$id'>
            <td><input type='text' name='new_location' value='$location' size='45' maxlength='50'></td>
            <td align='center'><input type='submit' value='Update'></td>
            
            </tr>";
          }
            ?>
        </table>
        </form>
</body>
</html>
 
Attachments
update.gif
update.gif (7.16 KiB) Viewed 492 times
edit.gif
edit.gif (8.04 KiB) Viewed 492 times
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: checkbox validation / PHP array function

Post by papa »

Took a quick look at the code and I would guess you need to change your GET id var to an array.

<input type='checkbox' name='id[]' value='$id'>

Then on the submit page you loop through the $_GET var and see which id has a value.
User avatar
nehrav
Forum Commoner
Posts: 38
Joined: Sun Sep 20, 2009 6:55 am
Location: New Delhi
Contact:

Re: checkbox validation / PHP array function

Post by nehrav »

papa wrote:Took a quick look at the code and I would guess you need to change your GET id var to an array.

<input type='checkbox' name='id[]' value='$id'>

Then on the submit page you loop through the $_GET var and see which id has a value.

Can you help me in write down the proper code.........
I add

Code: Select all

 
<?php
$idnum=0;
while($result = mysql_fetch_array($select))
    {
echo "<td><input type='checkbox' name='id[$idnum]' value='$id'></td>";
 
$idnum++;
}
?>
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: checkbox validation / PHP array function

Post by papa »

Code: Select all

 
 <?php
 
 if(!empty($_GET)
 {
    foreach($_GET['id'] as $id)
    {
        if(!empty($id)) 
        {
            $update = mysql_query("select id, location from test_tbl WHERE id='$id'"); 
            $uresult = mysql_fetch_assoc($update);
            echo "<input type='hidden' name='id' value='".$uresult['id']."'>";
            echo "<td><input type='text' name='new_location' value='".$uresult['location']."' size='45' maxlength='50'></td>";
            echo "<td align='center'><input type='submit' value='Update'></td>";
        }
    }
}
I don't know if this code works though, but might help a bit.


edit:

Code: Select all

 
<?php
$idnum=0;
while($result = mysql_fetch_array($select))
{
 echo "<td><input type='checkbox' name='id[$idnum]' value='$id'></td>";
  
$idnum++;
}
?>
You don't need the $idnum as the array gets assigned a key automatically.
So:
id[] will do just fine.
User avatar
nehrav
Forum Commoner
Posts: 38
Joined: Sun Sep 20, 2009 6:55 am
Location: New Delhi
Contact:

Re: checkbox validation / PHP array function

Post by nehrav »

papa wrote:

Code: Select all

 
 <?php
 
 if(!empty($_GET)
 {
    foreach($_GET['id'] as $id)
    {
        if(!empty($id)) 
        {
            $update = mysql_query("select id, location from test_tbl WHERE id='$id'"); 
            $uresult = mysql_fetch_assoc($update);
            echo "<input type='hidden' name='id' value='".$uresult['id']."'>";
            echo "<td><input type='text' name='new_location' value='".$uresult['location']."' size='45' maxlength='50'></td>";
            echo "<td align='center'><input type='submit' value='Update'></td>";
        }
    }
}
I don't know if this code works though, but might help a bit.


edit:

Code: Select all

 
<?php
$idnum=0;
while($result = mysql_fetch_array($select))
{
 echo "<td><input type='checkbox' name='id[$idnum]' value='$id'></td>";
  
$idnum++;
}
?>
You don't need the $idnum as the array gets assigned a key automatically.
So:
id[] will do just fine.

Code is giving me Parse error: on 2nd line only

Code: Select all

 
if(!empty($_GET)
  {
 
maybe not getting $_GET
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: checkbox validation / PHP array function

Post by papa »

# if(!empty($_GET))
User avatar
nehrav
Forum Commoner
Posts: 38
Joined: Sun Sep 20, 2009 6:55 am
Location: New Delhi
Contact:

Re: checkbox validation / PHP array function

Post by nehrav »

papa wrote:# if(!empty($_GET))
oooooo, Fields are getting placed correctly now

same checkbox and same records......done now

thanks buddy.........u r a genius

can u explain me your code that which statement fulfill which purpose....
I mean the code functionality........plz
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: checkbox validation / PHP array function

Post by pickle »

Double post. Locked.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Locked