Search code examples
phpformshidden-field

send to hidden input value another visible text input in the same form in php


Is there a simple way to send to a hidden input value the value from another visible text input value in one single form? The action on my click would use different references names depending on the source of action. See my example:

<form id="floatleft" method="post">
    <label for="name">My Name:</label> 
    <input name="senderName" type="text" value="">
    <input name="first_name" type="hidden" value="">
    <input alt="P" name="submit" type="image" src="https://p.gif" value="P" onclick="this.form.action='https://p.html'" />
    <input alt="z" name="submit" type="image" src="https://www.z.png" value="z" onclick="this.form.action='https://www.z.com/';" />

Solution

  • Yeah you can do it with jQuery change function or keyup function

    HTML

    <input type="text" name="senderName" id="senderName" value="" />
    <input type="hidden" name="first_name" id="first_name" value="" />
    

    JS (Change function)

    The change event occurs when the value of an element has been changed, so with change function, once the value inside senderName input changed its value (or updated value) copied to the first_name input. More Detail Here

    $(document).ready(function(){
        $('#senderName').change(function() {
            $('#first_name').val($('#senderName').val());
        });
    });
    

    Fiddle with Change Function

    JS (Keyup function)

    With keyup function, once stop typing the value of senderName input will be copied to the first_name input. More Detail Here

    $(document).ready(function(){
        $('#senderName').keyup(function(){
            $('#first_name').val($('#senderName').val());
        });
    });
    

    Fiddle with keyup function