user5377415
user5377415

Reputation:

Passing value from one field to another

I want to pass the value 9112232453 of one textfield to another.

I know I need Javascript for this but I don't know how to do it.

HTML

<form method = "post" action="">
  <input type="checkbox" name="phone" value="9112232453" onclick='some_func();' >
  <input type="text" name="phone" value="" id="phone">
  <input type="submit" name="Go">
</form> 

Then later, I want to use the value in my php.

Upvotes: 5

Views: 3543

Answers (4)

Buzinas
Buzinas

Reputation: 11725

The best way is to not obtrude the HTML code with Javascript event handlers.

So, you can add a DOMContentLoaded event listener to the document, and as soon as DOM is loaded:

  1. You add a change event listener to the input[type=checkbox], and then:

    1.1. If the checkbox is checked, then you change the input#phone's value to its value

    1.2. If not, then you empty the input#phone's value.

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('cbphone').addEventListener('change', function(e) {
    var phone = document.getElementById('phone');
    if (this.checked) {
      phone.value = this.value;
      // you can even enable/disable the input#phone field, if you want to, e.g:
      // phone.disabled = false; 
    }
    else {
      phone.value = '';
      // phone.disabled = true; 
    }
  });
});
<form method="post" action="">
  <input type="checkbox" name="cbphone" id="cbphone" value="9112232453">
  <input type="text" name="phone" id="phone">
  <input type="submit" name="Go" value="Go">
</form>

Upvotes: 1

amklose
amklose

Reputation: 1213

Try this: http://jsfiddle.net/yhuxy4e1/

HTML:

<form method = "post" action="">
    <input type="checkbox" name="phone" value="9112232453" onclick='some_func();' id="chk_phone">
    <input type="text" name="phone" value="" id="txt_phone">
    <input type="submit" name="Go">
</form>

JavaScript:

some_func = function() {
    var checkBox = document.getElementById('chk_phone');
    var textBox = document.getElementById('txt_phone');
    textBox.value = checkBox.value;
}

Upvotes: 0

shansingodiya
shansingodiya

Reputation: 29

before submit form use validation and check whether the field value is filled up or not. if yes get value of the field.

if(document.getElementBy("fieldIdfirst").value!="")
{
   document.getElementBy("fieldIdSecond").value=document.getElementElementById("fieldIdfirst");
}

Thanks it..

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16782

You could use a JS. function to take param (this.value) like:

<script>       
 var some_func = function  (val){
            var input = document.getElementById("phone");
            input.value = val;
        }
</script>

<form method = "post" action="">
    <input type="checkbox" name="phone" value="9112232453" onclick='some_func(this.value);' >
    <input type="text" name="phone" value="" id="phone">
<input type="submit" name="Go">
    </form> 

Upvotes: 3

Related Questions