• 0

checkboxes and PHP


Question

Hi guys,

I've written a simple script in PHP that allows users to submit their ascii art to the site. Each new submission gets an 'approved' value of 0 until it is reviewed and approved by myself. I've set up a basic admin area that pulls all the records from the database that have not yet been approved and displays them on screen. Below each record is a checkbox with a dynamically generated name in the form: chk[iD], where [iD] is the auto-incremented id of the record.

I'm having trouble determining whether a checkbox has been ticked or not when submitting my form, which queries the database. I've got the following code so far, but it does not seem to work (all checkboxes are read as 'checked'!): :

$approved=0; 

if($_POST['submit']) 
{ 
    $i=0; 

    while($i<$records_size) 
    { 
        $chkBox="chk" .$records[$i]; 

        #if checkbox of current record's id is checked 
        if($chkBox!="") 
        { 
            #create query 
            $sql="UPDATE asciiart SET approved=1 WHERE id=$records[$i]"; 

            #execute query 
            $result=mysql_query($sql,$connection); 

            $approved++; 
        } 

    $i++; 
    } 
        
    $url=$PHP_SELF ."?processed"; 
    print "<html><head><meta http-equiv='Refresh' content='1; url=$url'></head></html>"; 
    die("$approved of $records_size approved"); 
}

The if($chkBox!="") part of the code is obviously what's causing the problem, but I don't know to change it. Were it a VB app I would have done if chkBox[id].checked=false

The main page is at: http://www.cyberiapc.com/asciiart.php and the admin area is at http://www.cyberiapc.com/asciiart_pending.php

I'd appreciate any pointers?

Thanks.

CoreLEx

Link to comment
https://www.neowin.net/forum/topic/87239-checkboxes-and-php/
Share on other sites

12 answers to this question

Recommended Posts

  • 0

I tried your suggestion mr magoo, but now all checkboxes are read as "unchecked":

if($_POST['submit'])
{
    $i=0;
    $approved=0;

    #loop for all check boxes
    while($i<$records_size)
    {
        $chkBox="chk" .$records[$i];

        #if checkbox of current record's id is checked 
        if($_POST['$chkBox']==true) 
        { 
            #create query
            $sql="UPDATE asciiart SET approved=1 WHERE id=$records[$i]";

            #execute query
            $result=mysql_query($sql,$connection);

            $approved++;
        }

        $i++;
    }
        
    $url=$PHP_SELF ."?processed";
    print "<html><head><meta http-equiv='Refresh' content='1; url=$url'></head></html>";
    die("$approved of $records_size approved");
}

:pinch:

Link to comment
https://www.neowin.net/forum/topic/87239-checkboxes-and-php/#findComment-978010
Share on other sites

  • 0

Here's the code for the script:

<?php

    $connection = @mysql_connect("localhost","user","pass") or die("Sorry, unable to connect to MySQL");

    $result = @mysql_select_db(asciiart,$connection) or die("Sorry, unable to to get database.");

#--------------
# retrieve data
#--------------

$sql="select id,author,title,art,approved from asciiart";
$result=mysql_query($sql,$connection);

$counter=0;

$records=array();

#write the data

echo("<form action=\"$PHP_SELF\" method=post>");
echo("<center>");
echo("<table width=200 border=0 bgcolor=#FFFFFF cellpadding=3 style=\"border: 1px solid #ccd7e0; background-color: #fff;\">");

while($currentRow = mysql_fetch_array($result))
{
    if($currentRow["approved"]==0)
    {
        echo("<tr>");
        echo("<td width=100%>");
        echo("<font size=\"1\" face=\"verdana\">");

        echo($currentRow["title"] ."<br>");
        echo("</font>");

        echo("<font size=\"2\" face=\"verdana\">");
        echo("<b>" .$currentRow["art"] ."</b><br><br>");
        echo("</font>");
        echo("<input type=checkbox name=chk" .$currentRow["id"]  ." value=off>");
        echo($currentRow["id"]);

        #store id of records awaiting approval in an array
        $records[$counter]=$currentRow["id"];

        $counter++;

        echo("</td>");
        echo("</tr>");

        #seperator cell
        echo("<tr>");
        echo("<td width=100%>");
        echo("<hr width=100% color=#ccd7e0 size=1>");
        echo("</td>");
        echo("</tr>");
    }
}

echo("</tr>");
echo("</table>");
echo("</center>");
echo("<br>");
echo("<center><input type=submit name=submit value=Submit!></center>");
echo("<br>");

$records_size=count($records);
echo("<center>Records awaiting approval: " .$records_size ."</center>");

#--------------
# submit data
#--------------


if($_POST['submit'])
{
    $i=0;
    $approved=0;

    #loop for all check boxes
    while($i<$records_size)
    {
        $chkBox="chk" .$records[$i];

        #if checkbox of current record's id is checked 
        if($_POST['chkBox']) 
        { 
            #create query
            $sql="UPDATE asciiart SET approved=1 WHERE id=$records[$i]";

            #execute query
            $result=mysql_query($sql,$connection);

            $approved++;
        }

        $i++;
    }
}

#close
mysql_close();

?>

Link to comment
https://www.neowin.net/forum/topic/87239-checkboxes-and-php/#findComment-983713
Share on other sites

  • 0
dont you have to qualifity if($_POST['submit']) ??? like if($_POST['submit'] == true) { ???

nope you don't :p

if ( $_POST['submit'] )

is the same as

if ( $_POST['submit'] == TRUE )

or

if ( !$_POST['submit'] )

is the same as

if ( !$_POST['submit'] == TRUE)

but this will happen only if it exist and there is something in the variable

Link to comment
https://www.neowin.net/forum/topic/87239-checkboxes-and-php/#findComment-983748
Share on other sites

  • 0

try changing this line:

echo("<input type=checkbox name=chk" .$currentRow["id"]  ." value=off>");

to thie:

echo("<input type=checkbox name='chk" .$currentRow["id"]  ."'>");

and then change the if statement:

       if($_POST['chkBox'])

to this:

       if($_POST['chkBox']=="on")

Link to comment
https://www.neowin.net/forum/topic/87239-checkboxes-and-php/#findComment-984179
Share on other sites

  • 0

Thanks m0fo. That makes more sense, but unfortunately also does not work as expected. See http://www.cyberiapc.com/asciiart_pending.php

I wish there was a way to debug the script line by line to see why the checkboxes' states are not recognized by the latter part of the code that performs the UPDATE query.

I've been trying a few things in the meantime, but to no avail. :pinch:

Any other suggestions?

CoreLEx

Link to comment
https://www.neowin.net/forum/topic/87239-checkboxes-and-php/#findComment-984723
Share on other sites

  • 0
Here's the code for the script:

<?php

 ? ?$connection = @mysql_connect("localhost","user","pass") or die("Sorry, unable to connect to MySQL");

 ? ?$result = @mysql_select_db(asciiart,$connection) or die("Sorry, unable to to get database.");

#--------------
# retrieve data
#--------------

$sql="select id,author,title,art,approved from asciiart";
$result=mysql_query($sql,$connection);

$counter=0;

$records=array();

#write the data

echo("<form action=\"$PHP_SELF\" method=post>");
echo("<center>");
echo("<table width=200 border=0 bgcolor=#FFFFFF cellpadding=3 style=\"border: 1px solid #ccd7e0; background-color: #fff;\">");

while($currentRow = mysql_fetch_array($result))
{
 ? ?if($currentRow["approved"]==0)
 ? ?{
 ? ? ? ?echo("<tr>");
 ? ? ? ?echo("<td width=100%>");
 ? ? ? ?echo("<font size=\"1\" face=\"verdana\">");

 ? ? ? ?echo($currentRow["title"] ."<br>");
 ? ? ? ?echo("</font>");

 ? ? ? ?echo("<font size=\"2\" face=\"verdana\">");
 ? ? ? ?echo("<b>" .$currentRow["art"] ."</b><br><br>");
 ? ? ? ?echo("</font>");
 ? ? ? ?echo("<input type=checkbox name=chk" .$currentRow["id"] ?." value=off>");
 ? ? ? ?echo($currentRow["id"]);

 ? ? ? ?#store id of records awaiting approval in an array
 ? ? ? ?$records[$counter]=$currentRow["id"];

 ? ? ? ?$counter++;

 ? ? ? ?echo("</td>");
 ? ? ? ?echo("</tr>");

 ? ? ? ?#seperator cell
 ? ? ? ?echo("<tr>");
 ? ? ? ?echo("<td width=100%>");
 ? ? ? ?echo("<hr width=100% color=#ccd7e0 size=1>");
 ? ? ? ?echo("</td>");
 ? ? ? ?echo("</tr>");
 ? ?}
}

echo("</tr>");
echo("</table>");
echo("</center>");
echo("<br>");
echo("<center><input type=submit name=submit value=Submit!></center>");
echo("<br>");

$records_size=count($records);
echo("<center>Records awaiting approval: " .$records_size ."</center>");

#--------------
# submit data
#--------------


if($_POST['submit'])
{
 ? ?$i=0;
 ? ?$approved=0;

 ? ?#loop for all check boxes
 ? ?while($i<$records_size)
 ? ?{
 ? ? ? ?$chkBox="chk" .$records[$i];

 ? ? ? ?#if checkbox of current record's id is checked 
 ? ? ? ?if($_POST['chkBox']) 
 ? ? ? ?{ 
 ? ? ? ? ? ?#create query
 ? ? ? ? ? ?$sql="UPDATE asciiart SET approved=1 WHERE id=$records[$i]";

 ? ? ? ? ? ?#execute query
 ? ? ? ? ? ?$result=mysql_query($sql,$connection);

 ? ? ? ? ? ?$approved++;
 ? ? ? ?}

 ? ? ? ?$i++;
 ? ?}
}

#close
:)ysql_close();

?>

Just change if($_POST['chkBox']) to if($_POST[ $chkBox ])

:)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.