Jump to content

Autofill a textbox without refreshing the page


adige72

Recommended Posts

Hi.

 

I would like to automatically fill in the textbox labeled "Amount 2" after choosing the value of Amount without refreshing the page. I guess it requires javascript or AJAX but i don't know how to do it. Any help please?

 

http://i55.tinypic.com/29crpdz.jpg

 

$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");

$metabox = array();

$metabox[] = array(   "name" => "amount",
                            "label" => "Amount",
                            "desc" => "Choose the amount.",
                            "std" => "0",
                            "type" => "select",
                            "options" => $options_amount);

$metabox[] = array(   "name" => "amount2",
                            "label" => "Amount 2",
                            "desc" => "No need to enter it.",
                            "std" => "",
                            "type" => "text"); 

OK, the code you posted doesn't show anything about how the user selects an amount or how you would determine the value to populate in the text field.

 

Here is a quick example script

<html>
<head>

  <script type="text/JavaScript">
    function updateField(fieldID, newValue)
    {
        var fieldObj = document.getElementById(fieldID);
        fieldObj.value = newValue;
    }
  </script>

</head>
<body>

Amount:
<select name="amount" id="amount" onchange="updateField('amount2', this.options[this.selectedIndex].value);">
<option value=""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br>
Amount2: <input type="text" name="amount2" id="amount2" />
</body>
</html>

So how can i embed this code in a php file?

 

The same way you output anything to the browser with PHP, echo ,print, etc. I only provided some sample code because you failed to show any of the relevant code for me to provide a modification.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.