• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

Copy dynamic select field value to a text field in another form on the same page

I have a select field that is created on the fly and there are 2 forms on the page.  I don't want to have the select field on the page twice in both forms but I need that value for both forms, whichever the user selects to submit.

So I would like to have the select field display in the first form, then whatever is selected, copies to a hidden text field in the 2nd form.

I am attaching thee code that displays the drop down/select field
<?php // select drop-downs?
if($attr_option == 1){
echo get_attribute_dropdown($post->ID);
}
elseif($attr_option == 2){
echo get_attribute_dropdown($post->ID,2,$basis_price);						else{}													
?>

Open in new window

0
JuniorBee
Asked:
JuniorBee
  • 4
  • 3
1 Solution
 
asafadisCommented:
You can take a JavaScript approach, or (my preferred choice) a PHP approach:

/*************************************
 * JavaScript Approach
 *************************************/

// JavaScript
<script type="text/javascript">
    window.onload = function() {
        var select = document.getElementById('mySelect');
        var hidden = document.getElementById('myHiddenInput');
        select.onchange = function() {
            hidden.value = this.value;
        };
    };
</script>

// HTML
<select id="mySelect">
    <option value="world">hello</option>
    <option value="bar">foo</option>
</select>

<input type="text" id="myHiddenInput" />


/*************************************
 * PHP Approach
 *************************************/

// HTML
<form method="post" action"page_to_post.php">
   <div id="form1">
      <select name="form1[mySelect]">
          <option value="world">hello</option>
          <option value="bar">foo</option>
      </select>
      <input type="text" name="form1[myText]" value="lazy fox" />
      <button name="action" value="process_form1">Submit</button>
   </div>
   <hr />
   <div id="form2">
      <select name="form2[mySelect]">
          <option value="world">hello</option>
          <option value="bar">foo</option>
      </select>
      <input type="text" name="form2[myText] value="jumps fence"" />
      <button name="action" value="process_form2">Submit</button>
   </div>
</form>

// PHP
/*
The $_POST array will look something like:
Array
(
    [form1] => Array
        (
            [mySelect] => world
            [myText] => lazy fox
        )

    [action] => process_form1
    [form2] => Array
        (
            [mySelect] => bar
            [myText] => jumps fence
        )

)

Notice that while all the fields are submitted, they are neatly grouped in an inner array (form1 and form2).
Also, the 'action' field is unique.  So, you can process the form based on the value of 'action'.
*/

Open in new window

0
 
JuniorBeeAuthor Commented:
I prefer the javascript method since it's faster to implement but it doesn't seem to work.
0
 
asafadisCommented:
Can we see what you're trying to do?
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
JuniorBeeAuthor Commented:
I seem to have gotten it to work, thanks!
0
 
JuniorBeeAuthor Commented:
oh is there a way to get it to load when the page loads also?  So that the hidden form has the same thing as the first selection in the dropdown?
0
 
asafadisCommented:
Try:
window.onload = function() {
        var select = document.getElementById('mySelect');
        var hidden = document.getElementById('myHiddenInput');
        select.onchange = function() {
            hidden.value = this.value;
        };

        hidden.value = select.value;
    };

Open in new window

0
 
JuniorBeeAuthor Commented:
thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now