Page 1 of 1

checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 6:47 am
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>
 

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 6:53 am
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.

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 8:20 am
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++;
}
?>
 

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 8:52 am
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.

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 9:40 am
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

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 9:43 am
by papa
# if(!empty($_GET))

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 11:01 am
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

Re: checkbox validation / PHP array function

Posted: Wed Oct 07, 2009 1:58 pm
by pickle
Double post. Locked.