Jump to content

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';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.