Jump to content

Easy question for a newbie


didster

Recommended Posts

Hello, I am very new to PHP so appologies if the code is awful. I am sure that someone will give me a simple answer!

 

I have also used a few lines to test for myself. I have an HTML form that passes the variable chkno[] through to the seperate PHP file. Chkno[] is an array of 3 checkbox buttons.

 

<input type="checkbox" id="chkno" name="chkno[]" value="(Go)"/>

<input type="checkbox" id="chkno" name="chkno[]" value="(No GO Sector)"/>

<input type="checkbox" id="chkno" name="chkno[]" value="(No GO Full Form)"/>

 

When I do not click any of the buttons, my little test at the bottom returns not empty!? Please can anyone help?

 

 

$chknotest0 = $_POST['chkno'][0];

$chknotest1 = $_POST['chkno'][1];

$chknotest2 = $_POST['chkno'][2];

 

$chknotest=array("$chknotest0", "$chknotest1", "$chknotest2");

 

 

print_r($chknotest);

 

if (empty($chknotest))

print_r($chknotest);

else echo "not empty";

Link to comment
https://forums.phpfreaks.com/topic/99354-easy-question-for-a-newbie/
Share on other sites

Thankyou,

 

The return that I get is...

 

Array ( [0] => [1] => [2] => ) not empty

 

... so even though there is nothing typed in the tick boxes, my test still returns not empty! My test is to determine that at least one of the tick boxes is pressed. It doesnt matter which one of the three. As I say, I am only a beginner so I am probably way off the mark,

 

Thankyou for your help so far!

Before using user input variables ($_GET, $_POST, $_COOKIE etc) you should always check to see if they exists first and apply some form of validation:

if(isset($_POST['chkno']) && is_array($_POST['chkno']))
{
    $chknotest = $_POST['chkno'];

    print_r($chknotest);
}
else
{
    echo '$_POST[\'chkno\'] empty';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.