Title: Need help with complex Form/PHP/Custom Field query
Last modified: August 19, 2016

---

# Need help with complex Form/PHP/Custom Field query

 *  [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/)
 * I’ve scoured the internet (php forums mostly) to find a solution and have not
   turned up anything that actually works – hoping someone here can help……
 * I have a form that creates a dropdown list from specific pages and populates 
   the value of the options based on info in a custom field. The dropdown works 
   perfectly.
 * Example:
    ~ <select id=”products” name=”products”> <option selected value=”null”
   >Please Select Your Product</option>
 * <?php global $post;
    $myposts = get_posts(‘orderby=title&order=ASC&post_type=
   page&post_parent=22&numberposts=-1’);
 * foreach($myposts as $post) : ?>
    <option value=”<?php $sku = get_post_meta($post-
   >ID, productsku, true); echo $sku ?>”><?php the_title(); ?></option> <?php endforeach;?
   > </select> ~
 * I have another hidden input on the same form that needs to have it’s value populated
   based on the site visitor’s selection from the dropdown list, but it’s not as
   simple as passing the value from the selected option to the hidden input value,
   it actually needs to check for a second custom field from the same page as the
   selection.
 * Example:
    ~ <input type=”hidden” name=”location” id=”location” value=”<?php echo
   $othercustomfield ?>” /> ~
 * So if the visitor selects a product from the dropdown list, I need to find what
   the value of the custom field (meta_key “location”) is for that specific selection,
   and put it into the hidden input field.
 * I even tried a few javascript suggestions I found, but that didn’t work either….

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [gerbilk](https://wordpress.org/support/users/gerbilk/)
 * (@gerbilk)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485459)
 * something like this? [http://api.jquery.com/val/](http://api.jquery.com/val/)
 *  Thread Starter [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485540)
 * I don’t understand fully how to utilize that, but it looks like it would get 
   me closer, by identifying what was selected into a variable, that in theory I
   could then use in some sort of lookup fashion to get the correct 2nd value to
   put into the hidden input field….
 * In the example I’m using, the site visitor would select the product option that
   they want, which has the value of the product’s ID # (sku) – then in the hidden
   field I have to put the location of that particular product, which is the location(
   warehouse A or warehouse B), which right now is in a second custom field (meta_key
   =”location” meta_value=”warehouseA” or “warehouseB”…..that way the form data 
   gets sent to the correct location for packaging & shipping….
 * BUT in theory if it’s easier to not use the second custom field but just hard
   code a lookup function, that would work too (at least until they decide to add
   another location)….
 * In that case I think I could make use of that “val()” code you sent me to
 * Unless there is a better way to do what I’m trying to do?
 *  [gerbilk](https://wordpress.org/support/users/gerbilk/)
 * (@gerbilk)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485543)
 * how about this [http://api.jquery.com/text/](http://api.jquery.com/text/)
 * echo the second custom field into a div which is set to display:none within the
   loop, so each menu item carries the additional data with it, then target it when
   the user clicks the menu item, so (logic rather than code) onclick .. then ..
   jQuery(“.class-of-second-custom-field”, this); .. then … text() use to populate
   the hidden field, targeted with css selectors.
 * If you figure this out let me know how you did it!
 *  Thread Starter [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485548)
 * Wow – that’s creative! I like that idea. I’ll mess around with it and post back
   my results by tomorrow, if not this evening.
 * 🙂
 *  Thread Starter [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485578)
 * I’m still struggling with this problem…..I’m sure it’s because I’m not a coder
   and just guessing, and trying solutions that aren’t working….it also occurred
   to me that what I really need is a PHP solution, not javascript, because with
   javascript turned off it breaks…
 * What I’m thinking might work instead is to use something that takes the value
   of the selected option, and compares it to an array, then if it matches, put 
   another value into a variable that can be echoed as the value of the hidden input…..
   this could be a good short-term solution because there are a very limited number
   of options…..so something like this:
    ~<?php $getsku = $_POST[‘products’]; if
   $getsku is_in(array(‘product1′,’product2′,’product3’)) { $location = “warehouse
   a”; } else if $getsku is_in(array(‘product4′,’product5′,’product6’)) { $location
   = “warehouse b”; } ?> <input type=”hidden” value=”<?php echo $location ?>” > 
   ~
 * BUT I tried that and it didn’t work ….so I think it must be close to this, but
   I just am doing some small thing wrong…
 * Any ideas?
 *  [gerbilk](https://wordpress.org/support/users/gerbilk/)
 * (@gerbilk)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485582)
 * without javascript you would have to refresh the page to get anything to happen,
   php is server side. Unless I’ve miss-understood what you are trying to do.
 *  Thread Starter [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485588)
 * Hi gerbilk
 * No, you are correct – I didn’t realize (or if I did I forgot) that I’d need javascript
   to perform an action based on what someone selects from a dropdown list.
 * But I also realized that there is other javascript processing going on in this
   page, so I’m not going to worry about someone who may have js turned off – if
   they do none of the page and much of the site won’t work anyway, so I’m back 
   to using a javascript solution.
 * Unfortunately, since I’m not a coder I don’t really know how to write what is
   needed – the few things I tried based on what I read on those two pages linked
   to above (using their demos as examples) didn’t work, so I will probably have
   to go find a javascript forum to post my question in….
 * I did get the dropdown to echo the second custom field that I need (thanks for
   that creative tip!) but I just couldn’t seem to use it properly.
 * But thanks for trying to help anyway, I really do appreciate it. 🙂
 *  [gerbilk](https://wordpress.org/support/users/gerbilk/)
 * (@gerbilk)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485590)
 * I’m pretty bad at jquery as well, I’ve had a quick play and google and found 
   this
 *     ```
       <head>
         <script src="http://code.jquery.com/jquery-latest.js"></script>
       </head>
       <body>
           <a href="#" title="new value">link</a>
   
         <input id="update" type="hidden" value="" />
       <script>
           $('a').click(function(e) {
            e.preventDefault(); // <-- don't follow the link
            $('#update').val($(this).attr('title'));
       });
   
       </script>
       </body>
       ```
   
 * what you would need to do here is echo the hidden value into the title attribute
   of each link and obviously be more specific with the a selector in the jquery,
   then change #update to the id of your hidden field. you need firebug to see this
   working in DOM rather than the html panel. This code works as it is, it should
   just be a question of echoing out your data accordingly. You could adjust this
   slightly and get the text() thing working
 *  Thread Starter [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485595)
 * Thanks gerbilk – I think I understand this, but does the link need to actually
   be clicked on to initiate the script? If not, then I think this may solve the
   problem, as I could put it in the form but hide it.
 * If it needs to be clicked on to work, then I’m guessing it would have to go into
   the option? Like this?
 * ~
    <option value=”<?php echo $sku ?>”>[“><?php the_title(); ?>](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/?output_format=md#)
   </option> ~
 *  [gerbilk](https://wordpress.org/support/users/gerbilk/)
 * (@gerbilk)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485596)
 * i have an almost finalised version here that should do what you need
 *     ```
       <head>
         <script src="http://code.jquery.com/jquery-latest.js"></script>
       </head>
       <body>
       <form>
         <select id="selectbox">
        <option name="test" value="one" title="title" selected="selected">one</option>
        <option name="test2" value="two" title="title2">two</option>
       </select>
   
       <input id="update" type="hidden" value="defaultold" />
         </form>
   
       <script>
   
        $(document).ready(function() {
           $('#update').val('default');
           $('#selectbox').change(function() {
                $('#update').val($(this).find("option:selected").attr("title"));
           });
       });
   
       </script>
       </body>
       ```
   
 * for this you need to echo the normal field for the select options into the value
   =”<?php echo stuff here?>” then the additional field into title=”<?php echo stuff
   here?>” . I’m really going to take some time to learn jquery properly, ultra 
   useful stuff!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Need help with complex Form/PHP/Custom Field query’ is closed to new 
replies.

 * 10 replies
 * 2 participants
 * Last reply from: [gerbilk](https://wordpress.org/support/users/gerbilk/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-complex-formphpcustom-field-query/#post-1485596)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
