Results 1 to 3 of 3

Thread: I think I have an odd issue

  1. #1
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I think I have an odd issue

    For the life of me, I can't figure out what's going on here. Every bit of documentation I can find says I'm doing it wrong, but if I do it right it doesn't work the way I need it to. I have a php photo gallery script that allows clients to choose photos they wish to have edited. I want them to be able to choose up to 20 photos per order, but no more than 20. I originally made it so they could choose as many as they wanted, but only 20 get processed but that was confusing for some people. Now I'm adding in a check before the submit to see how many are selected using check boxes and either submit or let them know they picked too many photos.

    Under each photo is the check box using the following php code..

    if(!in_array($file_names[$count],$check_orders)) $content.="<input type=\"checkbox\" name=\"Edit[]\" value=\"$count\"><font size=\"4\"><b> EDIT PHOTO</b></font><br>";

    ...and the rest of the code iterates through the photos. The in_array() just checks to see if that photo was already ordered previously. $count is the iteration variable for the photos. That all works. If you haven't noticed what the problem is, it's the name of the check boxes. Edit[]. If the [] brackets are not included, the $_REQUEST['Edit'] variable only returns a single number and not an array of numbers. When I include the [] brackets, $_REQUEST['Edit'] returns the full array of all the numbers associated with the checked photos.

    That would be fine, but now as I said, I'm trying to add in a pre-submit check to see if there are more than 20 photos selected. The problem is, JavaScript doesn't recognize Edit[] as an element but it does recognize Edit. No matter what combination I use, I can not seem to get both the pre-submit JavaScript AND the post-submit php to work with the array of check boxes.

    All documentation I can find says to name the inputs without the [] brackets and it will automatically be turned into an array. The only place I find a reference to using [] brackets in the name of a form variable is on php.net for a multiple enabled dropdown list selection.

    Recap: Everything works on the php side when I use the [] brackets but not on the JavaScript side. Everything works on the JavaScript side when I do NOT use the [] brackets but doesn't work on the php side. Any ideas how to get both working? I'm thinking if nothing else, I'll add another input variable to the form, hidden, and as I do the check to count how many are selected, fill up that variable as an array and pass that on with the form.

  2. #2
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Upon further research, I found that html asks for the [] brackets in the name of the form element to indicate it will be an array of check boxes. So now I'm guessing this should be a JavaScript question instead of php.

    Sorry for the misunderstanding.

  3. #3
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I found a solution and will post here just in case anyone else is having this issue.

    It seems html likes the [] brackets to indicate an array when passing form elements, but JavaScript doesn't. JavaScript treats it as just a string of text. I'll show an example to illustrate.

    <form name="myform">
    <input type="checkbox" name="Edit[]" value="1">
    <input type="checkbox" name="Edit[]" value="2">
    <input type="checkbox" name="Edit[]" value="3">
    <input type="button" name="submit" onClick="checkForm();">
    </form>
    <script language="JavaScript">
    function checkForm() {
    el=document.myform.elements;
    for (i=0;i<el['Edit[]'].length;i++) {
    if (el['Edit[]'][i].checked==true) alert(el['Edit[]'][i].value+' is selected');
    }
    }

    So basically JavaScript treats the Edit[] in the above example as a text string and uses that as the name of an element within the form then sets up an array with that element which you can then access as shown. I hope this makes sense as this is all new to me so I may not be explaining very well.

Similar Threads

  1. Replies: 3
    Last Post: 02-15-2011, 02:37 AM
  2. AnyLink Drop Down Menu - border issue *and* FF alignment issue
    By trjonas in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 12-18-2007, 05:58 AM
  3. Email issue -- Carbon Copy Issue
    By Humper in forum HTML
    Replies: 0
    Last Post: 09-01-2006, 10:01 PM
  4. Email issue -- Carbon Copy Issue
    By Humper in forum PHP
    Replies: 1
    Last Post: 09-01-2006, 09:47 PM
  5. Email issue -- Carbon Copy Issue
    By Humper in forum JavaScript
    Replies: 0
    Last Post: 09-01-2006, 08:26 PM

Bookmarks

Posting Permissions

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