Results 1 to 18 of 18

Thread: Having trouble with Array of Checkboxes and FileUpload

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Having trouble with Array of Checkboxes and FileUpload

    I created a page that contains list of checkboxes in a loop like,

    <input type=checkbox name='CHK[]' value=LoopValue>


    I am posting this page to an another PHP page
    where I have written
    $chk = $_POST['CHK'];
    print_r($chk);

    and as a reasult it is showing me nothing.. while I have selected some checkboxes as well.
    Same problem with <input type=file name=file>

    This is all working in PHP 4 while it is not working in PHP 5.2.6.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Having trouble with Array of Checkboxes and FileUpload

    well, you didn't really post any code, but I made this script:
    PHP Code:
    <pre>
    <?php
      
    echo "php=" phpversion() . "\n\n";
      
    print_r($_POST);
    ?>
    </pre>
    <hr />
    <form method="post">
    <?php for($i 0$i 5$i++): ?>
      <input type="checkbox" name="check[]" />
      <input type="file" name="file[]" />
      <br />
    <?php endfor; ?>
      <input type="submit" value="submit" />
    </form>
    that script returns this example output for me:
    Code:
    php=5.2.9
    
    Array
    (
        [file] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
                [4] => 
            )
    
        [check] => Array
            (
                [0] => on
                [1] => on
            )
    
    )
    remember, check boxes are either on or off; if they are on, they are set. if not, they aren't set (like in my example, I checked the first two checkboxes and not the last three). file inputs, however, are always set when present in a form.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    Thanks..

    OK I copied ur code and select some checkboxes and select some file... and thats what am I getting as a result

    Array ( [campaign] => 1 [check] => [Submit] => Update )

    [check] is blanked while i have selected some checkboxes
    Its not even showing there is a file or somthing. and not showing selected checkboxes as well.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Having trouble with Array of Checkboxes and FileUpload

    I don't see how that can be output from kows' code; there's no "campaign" field, and "Submit" should have a value of "submit" if this were the result of kows' example...

    Either post what you're using, or use kows' example exactly as-is and tell what happens.

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Having trouble with Array of Checkboxes and FileUpload

    yeah, I'm not sure how you modified my code, but you definitely changed a bunch of it. you should now A) post whatever code you're actually using (we can't help you if we don't know what you're doing), and B) running the code that I originally posted without any modifications.

    and just as a side note, there is no need to name your submit buttons unless you have multiple submit buttons for the same form. like in my code, all you need are the following attributes:
    Code:
    <input type="submit" value="submit" />
    I'm not sure why, it's just a pet peeve of mine.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    I dint change Kow's Code too much... his code is simple and i just put it as it is.. well you named a button or not dosent matter.. Campaign field was a combobox which dosent matter either... I am sorry i couldnt post the actuall code.. anyway
    Thanks Guys...well.. I found the solution

    I used GET instead of POST and all my checkboxes were available to me as an Array... i dont knw the reason.. but it worked that way

    I found that the GOLBAL_VARIABLE is set to OFF in PHP 5 for security reason thats why I can not upload file and I can not see file box result on other page...
    Well GLOBAL_VARIABLE is set to OFF in PHP 4 as well.. but Uploading a file worked in PHP 4 regardless the ON n OFF...

    As soon as I will be able to turn it ON I will let you guys know... I hope my File Upload will work this way...

    Many Thanks...

  7. #7
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Having trouble with Array of Checkboxes and FileUpload

    I used GET instead of POST and all my checkboxes were available to me as an Array... i dont knw the reason.. but it worked that way
    This means your form was probably set up as:
    Code:
    <form method="get">
    ...or just "<form>". In either case, the GET method is used to send and retrieve data. To use POST, do as kows did:
    Code:
    <form method="post">
    As soon as I will be able to turn it ON I will let you guys know... I hope my File Upload will work this way...
    Ugh, do you mean the "register_globals" setting? That's turned off by default in PHP 5 for good reason, and yours is not an appropriate reason to turn it on. Post your code and perhaps we can tell you how to fix your file upload without opening up a security hole.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Having trouble with Array of Checkboxes and FileUpload

    Quote Originally Posted by mfaisalnice View Post
    I dint change Kow's Code too much... his code is simple and i just put it as it is.. well you named a button or not dosent matter.. Campaign field was a combobox which dosent matter either... I am sorry i couldnt post the actuall code.. anyway
    Thanks Guys...well.. I found the solution

    I used GET instead of POST and all my checkboxes were available to me as an Array... i dont knw the reason.. but it worked that way

    I found that the GOLBAL_VARIABLE is set to OFF in PHP 5 for security reason thats why I can not upload file and I can not see file box result on other page...
    Well GLOBAL_VARIABLE is set to OFF in PHP 4 as well.. but Uploading a file worked in PHP 4 regardless the ON n OFF...

    As soon as I will be able to turn it ON I will let you guys know... I hope my File Upload will work this way...

    Many Thanks...
    That is because you have the method attribute of the form tag in you HTML either missing or set to "get". I recommend you change it to "post".

    It occurs to me that you are changing things that you do not understand. This is one of the best ways to create a page with security flaws. I am also willing to bet that you are not properly validating your variables, which could also lead to security flaws.

    Why is it that you cannot post your code? I am pretty sure there is nothing in it that we want to steal. At the very list, send it in a private message to kows or myself.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Having trouble with Array of Checkboxes and FileUpload

    It occurred to me, I bet I know what's wrong with your file upload. Your form tag needs to include this:
    Code:
    <form method="post" enctype="multipart/form-data">

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    OK here is my Coding Files... all your guidance is appreciated guys.

    it is the first File that contains FORM

    ========================================================
    manufacturer.php
    =================
    <form name="Manufacturer" method="POST" action="addmanufacturer.php" onSubmit="return formSubmit;" enctype="multipart/form-data"> <? include "admin_header.php"; ?>
    <tr>
    <td>
    <br />
    <table width="80%" bgcolor="#CCCCCC" border="1" cellspacing="0" cellpadding="0" align="center">
    <tr>
    <td>

    <table align="center">
    <tr>
    <TD class="error" id="ErrorMessage">
    <? echo $ErrorMessage; ?>
    </TD>
    </tr>
    </table>
    <br />

    <table width="55%" border="1" align="center" cellpadding="5" borderColor="#000000" bgcolor="999999" style="BORDER-COLLAPSE: collapse; bordercolor: #000000">
    <tr align="center" bgColor="#333333">
    <td height="30" colspan="2" class="normal"> <p>Add New
    Manufacturer</p></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Manufacturer Name</td>

    <td width="77%" class="normal">
    <input name="txtName" id="txtName" type="text" size="60" maxlength="255"><font color="#FF0000">*</font></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Description</td>

    <td valign="top">
    <textarea name="txtDescription" id="txtDescription" cols="45" rows="7"></textarea>
    <font color="#FF0000">*</font></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">URL</td>

    <td width="77%" class="normal">
    <input name="txtURL" id="txtURL" type="text" size="60" maxlength="255"><font color="#FF0000">*</font></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Image Upload</td>

    <td width="77%" class="normal">
    <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="1000000">
    <input type="file" id="imagedata" name="imagedata" size="40">
    </td>

    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Status</td>

    <td width="77%" class="normal">
    <select name="cboStatus" id="cboStatus">
    <option value="Inactive" selected>Inactive</option>
    <option value="Active">Active</option>
    <option value="Unknown">Unknown</option>
    </select>
    </td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Featured</td>
    <td><input type="checkbox" name="chkFeature"></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Lesco Green LG</td>
    <td><input type="checkbox" name="chkLescoGreenLG"></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">LG URL</td>

    <td width="77%" class="normal">
    <input name="txtLGURL" id="txtLGURL" type="text" size="60" maxlength="255"></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">Quick Ship QS</td>
    <td><input type="checkbox" name="chkQShipQS"></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">QS URL</td>

    <td width="77%" class="normal">
    <input name="txtQSURL" id="txtQSURL" type="text" size="60" maxlength="255"></td>
    </tr>


    <tr align="center" class="normal" bgcolor="#CCCCCC">
    <td colspan="2">
    <input type="submit" name="Submit" value=" Save " onClick="javascript:Validate();">
    <input type="button" name="Submit2" value=" Back " onClick="javascript:GoBack();">
    <input type="button" name="ExporttoPDF" value=" Export to PDF " onClick="javascript:ExportPDF();">
    </td>
    </tr>
    </table>
    </form>

    ==========================================================
    ==========================================================

    Here is my PHP File which will get data from POST and save it in DB...
    ==========================================================
    addmanufacturer.php
    ================

    <?

    include "../include/connect.php";

    $Name = $_REQUEST["txtName"];
    $Description = $_REQUEST["txtDescription"];
    $URL = $_REQUEST["txtURL"];
    $QuickShip = $_REQUEST["chkQShipQS"];
    $Status = $_REQUEST["cboStatus"];
    $Featured = $_REQUEST["chkFeature"];
    $LescoGreen = $_REQUEST["chkLescoGreenLG"];
    $LGURL = $_REQUEST["txtLGURL"];

    $QSURL = $_REQUEST["txtQSURL"];


    if (isset($_FILES['imagedata']) && $_FILES['imagedata']['size'] > 0)
    {

    // Temporary file name stored on the server
    $tmpName = $_FILES['imagedata']['tmp_name'];
    $data_name = $_FILES['imagedata']['name'];
    $data_size = $_FILES['imagedata']['size'];
    $data_type = $_FILES['imagedata']['type'];
    // Read the file
    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    $data = addslashes($data);
    fclose($fp);



    $sql = "insert into manufacturer (Name,Description,URL,image,filename,filesize,filetype,MStatus,Featured,LescoGreenLG,LGURL,QuickShip ,QSURL) values('$Name','$Description','$URL','$data','$data_name','$data_size','$data_type','$Status','$Feat ured','$LescoGreen','$LGURL','$QuickShip','$QSURL')";
    mysql_query($sql) or die(mysql_error());
    // Print results


    }
    echo "<script>location.href='../admin/manufacturerpage.php?msg=added';</script>";
    ?>

    ========================================================
    =========================================================

    THIS CODE IS WORKING IN PHP 4 AT MY HOME WHILE IT IS NOT WORKING IN PHP 5 at Linux SERVER, PHP 5...

    I hope you guys can help me now.. it is furstating...
    Sorry for not posting code earlier... I was not in office when i was replying to you and I thought my question was clear enough..
    Actually I used already all the attributes and tricks u mentioned me... but its not working for me.. I dont knw why.. please help soon
    I bolded the main TEXT as well...
    ONLY FILE upload issue is left.. I solved the checkboxex issue

    Thanks

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    Another form with Radio Buttons and FileBOX

    =======================================================
    importcontactpage.php
    =======================================================
    <form name="Contact" method="post" action="../admin/importcontacttoDB.php" onSubmit="return formSubmit;" enctype="multipart/form-data"> <? include "admin_header.php"; ?>
    <tr>
    <td>
    <table width="80%" bgcolor="#CCCCCC" border="1" cellspacing="0" cellpadding="0" align="center">
    <tr>
    <td>

    <table align="center">
    <tr>
    <TD class="error" id="ErrorMessage">
    <? echo $ErrorMessage; ?>
    </TD>
    </tr>
    </table>
    <br />

    <table width="55%" border="1" align="center" cellpadding="5" borderColor="#000000" bgcolor="999999" style="BORDER-COLLAPSE: collapse; bordercolor: #000000">
    <tr align="center" bgColor="#333333">
    <td height="30" colspan="2" class="normal"> <p>Import
    Contact</p></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">File Type<font color="#FF0000">*</font></td>

    <td width="77%" class="normal" style="Color:black;">
    <table>
    <tr>
    <td class="normal" style="Color:black;">CSV File
    </td>
    <td><input type="radio" name="rdoFileType" value="CSVFile">
    </td>
    </tr>
    <td class="normal" style="Color:black;">Tab Delimited
    </td>
    <td><input type="radio" name="rdoFileType" value="TabDel">
    </td>
    </tr>
    <td class="normal" style="Color:black;">Excel File
    </td>
    <td><input type="radio" name="rdoFileType" value="Excel">
    </td>
    </tr>
    </table>

    </td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td width="23%" class="normal" style="Color:black;">File<font color="#FF0000">*</font></td>
    <td>
    <input type="file" name="filedata" size="40"> </td>
    </tr>

    <tr align="center" class="normal" bgcolor="#CCCCCC">
    <td colspan="2">
    <input type="submit" name="Submit" value=" Save " onClick="javascript:Validate();">
    <input type="button" name="Submit2" value=" Back " onClick="javascript:GoBack();">
    </td>
    </tr>
    </table>
    </form>

    =========================================================
    =========================================================
    =========================================================
    PHP file where it must process the uploaded file
    importcontacttoDB.php
    ==========================================================
    <?

    include "../include/connect.php";

    $Name = $_REQUEST["rdoFileType"];
    print_r($_POST);


    ?>
    =======================================================
    =======================================================
    THE RESULT I See is this
    ======================================================
    ======================================================
    Array ( [rdoFileType] => TabDel [Submit] => Save )
    ======================================================

    Now again it is working at my office and showing me FILE as well... I am using Windows XP, PHP 4

    IT is not working on my client page and showing me only RADIO BUTTON ... CLIENT is using LINUX, PHP 5....

    I think it has to do somthing with the Global Variable is set to OFF...in PHP 5

    Please help soon guys

    Thanks in Advance

  12. #12
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Having trouble with Array of Checkboxes and FileUpload

    OK here is my Coding Files... all your guidance is appreciated guys.

    it is the first File that contains FORM
    So what result are you getting from this, if not the one you expect?... I tried this on my (PHP 5.2.6) server and it worked as expected - all form data (including file data) was received by the addmanufacturer.php script.

    I believe there was also a recent thread concerning the storing of image data in a db... and why it's a bad idea.

    As for your second example, print_r($_POST) will not show file data, because it's stored in a different array: $_FILES. So, add print_r($_FILES);
    Last edited by SambaNeko; Aug 28th, 2009 at 11:11 AM.

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    Quote Originally Posted by SambaNeko View Post
    So what result are you getting from this, if not the one you expect?... I tried this on my (PHP 5.2.6) server and it worked as expected - all form data (including file data) was received by the addmanufacturer.php script.

    I believe there was also a recent thread concerning the storing of image data in a db... and why it's a bad idea.

    As for your second example, print_r($_POST) will not show file data, because it's stored in a different array: $_FILES. So, add print_r($_FILES);
    Are you Using LINUX OS???... well $_POST at least shows the FileName because I tried it with GET <form action='GET'> and $_GET were showing me the file name?

  14. #14
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Having trouble with Array of Checkboxes and FileUpload

    Yes Linux. I'd still like to know what result you're actually getting; could you replace the entire contents of addmanufacturer.php with this code, and tell me what you get when you submit the form to it?

    Code:
    <?php
    echo "php=" . phpversion() . "\n\n";
    print_r($_REQUEST) . "\n\n";
    print_r($_FILES) . "\n\n";
    ?>
    Last edited by SambaNeko; Aug 28th, 2009 at 04:08 PM. Reason: Blatant Stupidity - see kows' correction below.

  15. #15
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Having trouble with Array of Checkboxes and FileUpload

    oop, remember that print_r() is a function that doesn't return output (but rather outputs itself), so make sure it's:

    PHP Code:
    <?php
    echo "php=" phpversion() . "\n\n";
    print_r($_REQUEST);
    print_r($_FILES);
    ?>
    like previously mentioned in this thread, though, if you're talking about register_globals when you say "global variable," that setting would have nothing to do with any of this. it would not affect anything you're doing, and it's a deprecated setting for good reason.

  16. #16

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    Kows,

    Here is the result i got when I use your code..

    CODE
    ==============
    <?php
    echo "php=" . phpversion() . "\n\n";
    print_r($_REQUEST);
    print_r($_FILES);
    ?>
    ============

    RESULT
    ==================
    php=5.2.6 Array ( [rdoFileType] => TabDel [Submit] => Save ) Array ( )
    ================



    Hope you can help me soon.

    Many Thanks

  17. #17
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Having trouble with Array of Checkboxes and FileUpload

    Some thoughts for you:

    - make a file called phpinfo.php, and put this code in it:
    Code:
    <?php
    phpinfo();
    ?>
    This will generate a page with your server's PHP configuration settings. Do a Ctrl+F for "file_uploads" - is that set to "on"?

    - also on the phpinfo file, look for "upload_max_filesize" and "post_max_size" and make sure the file you're trying to upload isn't bigger than those

    - does your file name have any non-alphanumeric characters in it?

    - your form has some Javascript that's running onclick of the submit buttons, and onsubmit of the forms - what are these scripts doing? Could you post them as well?

  18. #18

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: Having trouble with Array of Checkboxes and FileUpload

    Hi SambaNeko

    I tried the code u mentioned me..

    file_uploads is SET to ON

    Upload_max_filesize = 2M

    post_max_size = 8M

    my file does not contain any non-alphanumeric characters.

    JAVA SCRIPT( I tried to remove every thing..and just the file box were there with FORM and it still dint work out)

    JS was simple as below
    function onSubmit()
    {
    if(form.textbox.value == "")
    {
    alert("You Must fill this text box");
    }
    }

    RIGHT NOW... I have a simple page with a Filebox n submit button and the FORM Tag in it... and on the other page I see nothing in FILES array

    Please help
    Last edited by mfaisalnice; Sep 1st, 2009 at 07:25 AM. Reason: spelling mistakes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width