#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    377
    Rep Power
    76

    Holding a value of select box on form


    I am trying to get my form to hold the value of the select box which it does. But the value I get back is "1" or "0".

    I need to have the value be "Rent" or "Own". How can I do this?

    $rent_own = $_POST['rent_own']
    PHP Code:
    function createOptionFromArray($myArray,$selected) {

            if(!
    is_array($myArray)) {

                return 
    false;

            }

            
    $returned $select '';

            foreach(
    $myArray as $key => $value) {

                if(
    $selected == $key) {

                    
    $select ' selected';

                }

                
    $returned .= "<option value=\"$key\"$select>$value</option>";

                
    $select '';

            }

            return 
    $returned;

        }  
    $option = array(=> 'Rent'=> 'Own');

    $selected = (isset($_POST['rent_own']) && intval($_POST['rent_own']) < 2) ? $_POST['rent_own'] : '';

    print 
    "<select name=\"rent_own\">\n";

    print 
    createOptionFromArray($option,$selected);

    print 
    "</select>\n"
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    Originally Posted by mallen
    But the value I get back is "1" or "0".
    Because that's the value you define for the option elements:

    PHP Code:
    $returned .= "<option value=\"$key\"$select>$value</option>"
    If you want $value instead of $key to be the value, well, then use $value instead of $key

    Apart from that: You must learn to escape your stuff.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    377
    Rep Power
    76
    Thanks that was simple.
    What is the best approach for calling the createOptionFromArray function many times? My form has about 10 select boxes and it wasn't working with more than one createOptionFromArray. so I had to rename each one createOptionFromArray1, 2, 3 and so on.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    Show us the code.

    The function can be called as often as you want, so if there's something wrong, it's due to a bug in your code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    377
    Rep Power
    76
    This is what I have for each select box I need. But of course each select box has its own list of options. The HTML form has over 600 lines of code so I don't want to post that. It just duplicates text boxes, select boxes and text areas. If I try to include another the form doesn't show any fields from that point downward. If I rename createOptionFromArray() it works.
    PHP Code:
     <?php 
      
    function createOptionFromArray($myArray,$selected) {

            if(!
    is_array($myArray)) {

                return 
    false;

            }

            
    $returned $select '';

            foreach(
    $myArray as $key => $value) {

                if(
    $selected == $value) {

                    
    $select ' selected';

                }

                
    $returned .= "<option value=\"$value\"$select>$value</option>";

                
    $select '';

            }

            return 
    $returned;

        }  
    $option = array(=> 'Rent'=> 'Own');

    $selected = (isset($_POST['rent_own']) && intval($_POST['rent_own']) < 2) ? $_POST['rent_own'] : '';

    print 
    "<select name=\"rent_own\">\n";

    print 
    createOptionFromArray($option,$selected);

    print 
    "</select>\n"

    ?>
    Last edited by mallen; August 18th, 2013 at 09:15 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    if you are re-creating a function multiple times, then you are doing it wrong. A function by definition should be used multiple times!

    if the options for each check box differ then you pass them as parameter, which is what you are doing.

    as for function re-use. maybe move this function to another file and include that file in this script at the TOP. I think some other code may prevent function code from being executed e.g.

    if something {
    my function.....
    } else {
    use my function = error!
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    377
    Rep Power
    76
    The page the long form is on is a mixture of HTML and PHP. Would I need to include the function every time there is a block of PHP code?

    Pseudo code:

    HTML here....

    PHP here require("function code here.php");

    Rest of the PHP code

    HTML here

    PHP here require("function code here.php");

    Rest of the PHP code
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    No. You need the function definition exactly once. Not twice or three times or once for every function call. Once for the whole script and all scripts you may include.

    I think there are some fundamental misunderstandings regarding PHP basics (or programming basics in general), so it might be a good idea to get clear about how that works.

    A function is a static entity. You introduce it once to the interpreter, and then you call it as often as you want using the name you specified in the definition. You don't refined it. In fact, you cannot. If you try, PHP will complain that the name is already in use.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    377
    Rep Power
    76
    I have to rework it somehow. Because this line cannot be called and printed on the page more than once.:

    PHP Code:
    print createOptionFromArray($option,$selected); 
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    We're running in circles.

    As paulh1983 and I already told you several times, a function can be called as often as you want. That's the whole purpose of a function (amongst other things).

    A huge part of programming with PHP consists of calling internal functions like intval() or htmlspecialchars() or whatever. Have you ever had PHP complain about a function being called too often? "Sorry, but you've already called intval() three times. Please wait 24 hours."

    If you're having some bug, it has nothing to do with repeating this line. I've actually run the code just to make sure I'm not overlooking something, and it worked exactly as expected.

    So the bug is somewhere in your script, but not on this line. If you want us to help you, we need the full code (excluding HTML stuff, maybe) and an exact description of what's wrong.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    377
    Rep Power
    76
    Ok I changed it and I put it in a separate function at the top of the page and it works. I can put multiple select boxes on the page.

    PHP Code:
    function form_select($name$options$selected) {
        
    $html sprintf('<select name="%s">'$name);
        foreach(
    $options as $key => $value) {
            
    $html .= sprintf('<option value="%s"'$value);
            if (
    $selected == $value)//changed from $key
                
    $html .= ' selected="selected"';
            
    $html .= sprintf('>%s</option>'$value);
        }
        
    $html .= '</select>';
        return 
    $html;

    And on the page I have this:

    PHP Code:
    $options = array(=> 'Rent'=> 'Own');
    $selected = (isset($_POST['rent_own']) && intval($_POST['rent_own']) < 2) ? $_POST['rent_own'] : '';
    echo 
    form_select('rent_own'$options$selected); 
    Last edited by mallen; August 20th, 2013 at 09:00 PM.

IMN logo majestic logo threadwatch logo seochat tools logo