Jump to content

populate a text box from drop down.


vinaysnarayan

Recommended Posts

Hi,

 

I'm new to php, but however, I can manage small changes.

 

I've a small html/java script. I want to achieve the same through php.

Please help, how can I do this.

 

create a new html file, copy the below script, save the file and run it in a browser. I want to get the same results using a php file. Please help me how to do this. Thanks in advance.

 

Note: The below code outputs data on a textarea box. However, is it possible to get the output on a texbox instead of a textarea box?

 

 

<script type="text/javascript">

function addItem(itemValue,outputDestination){

formElement = document.forms['myForm'].elements[outputDestination]

tempVar = formElement.value

if (tempVar.lastIndexOf("\n") != tempVar.length-1){

}

 

formElement.value = itemValue;

 

}

</script>

 

<form name="myForm" id="myForm">

<select onchange="addItem(this.value,'textOutput2')">

<option value="A1">A1</option>

<option value="A2">A2</option>

<option value="A3">A3</option>

<option value="B1">B1</option>

<option value="B2">B2</option>

<option value="B3">B3</option>

<option value="C1">C1</option>

<option value="C2">C2</option>

<option value="C3">C3</option>

</select>

 

 

<textarea style="displaynone;" name="textOutput2" cols="10" rows="1"></textarea>

<br />

 

</form>

 

Regards,

Vinay

Link to comment
Share on other sites

No...I mean...PHP is server side. It cannot be done through php. That's probably why it's written in Javascript.

 

You could do something like if you select an item and submit the form, PHP can do something like this:

 

<?php
echo '<form action="" method="post">
<select name="select">
<option>value1</option>
<option>value2</option>
</select>';
$select= $_POST['select'];
echo '<input type="text" name="something" value="'.$select.'" />
<input type="submit" />
</form>';
?>

 

Try it out and you'll see what I mean. But javascript is much more suited to the task. Why do you want to change it anyway?

Link to comment
Share on other sites

Ok. Let me give you more details what I want to do.

 

Please see the attached file - mqform1.php.  When I run this in a file in a browser, I get a form, where I can submit information to the database.

The fields - Region - Environment - Server are 3 level drop down menu. This will be populated only when the corresponding parent is selected. However, after I click the 'Save Data' button, I want the data contained in each drop down (after selection) to be populated to the corresponding text box next to it. Can any one please help me on this?

 

Thank you very much in advance.

 

-Vinay

 

[attachment deleted by admin]

Link to comment
Share on other sites

Sorry for any inconvenience caused. Please dont be angry on me as I'm very new to php.

 

jackpf has posted a code, above in this thread. It just works perfectly fine. If i select a value from a drop down and click the button, the text box gets populated. I want to do the same for the mqform1.php i sent you.

 

I'm stuck up and dont know how to do it. :(

 

Regards,

Vinay

Link to comment
Share on other sites

let me ask a question... why? i mean, what value-add to the application/user is there by doing this? seems like each parent list will display the chosen value already. duplicating the value in a textbox is redundant.

 

and btw... you can't do this in php. ;) that is, unless the form's submitted to the server each time a parent's selected item changes. if so, then it's just a matter of determining the selected value and setting the default value of a text field accordingly.

 

jason

Link to comment
Share on other sites

Well, you're script appears to be copied, as i don't really believe you know OOP if you're a beginner. I would recommend writing your own script as you're not going to learn PHP by copying.

 

And javascript could do it like this

function populate_something_else(element1, element2)
{
document.getElementById(element2).value = document.getElementById(element1).value;
}

 

But I am unsure what benifits this will incur, as you already have the value in element1. Why not just post that instead?

Link to comment
Share on other sites

I have two different tables - A and B in the mysql database. The values for Region, Environment, Server are stored permanently in table A and will not be changed. When I select these values, fill up the form and click save data, I want the complete values (including Region, Environment, Server) to be stored in the corresponding columns in table B.

 

This is what i'm trying to achieve.

 

Regards,

Vinay

Link to comment
Share on other sites

When the user clicks 'Save Data', a new form should pop up with the values selected in the mqform1.php.

At the bottom of this page, I have a 'Confirm' Button. This form - lets say form2 - values in this will be read only and just a 'Confirm' button. Once clicked, it should update the columns in table B.

 

In this form2, all the field should be read only text boxes, with the values selected in the previous editable form.

 

Also, I'm not inserting both the tables. I'm inserting only to table B depending on the values selected from table A for region, env, and server and the remaining values from mqform1.php.

 

Please let me know if you need any further information.

Thanks in advance,

-Vinay

Link to comment
Share on other sites

Hi jackpf / anyone,

 

I'm not a coding guy. I'm from system administration. Can any one help me to get this done either through java / dhtml / php any thing.

I just need to submit this by end of this week. Thanks in advance.

 

If this cannot be done, thank you every one for your kind support, time and replies you sent me.

 

Warm Regards,

Vinay S

Link to comment
Share on other sites

Hi Jackpf,

 

I dont know how to use the code you gave in the attached file (mqform.php) i sent you. I searched for a variable, where the value of each drop down selected would be stored. But I could not identify where exactly I can get that value. :(

 

Also, I dont know where to insert the function code snippet you gave and where to write the function call.

I lose the challenge, jackpf.

 

I thank you very very much for your kind help you gave me.

 

Regards,

Vinay

 

 

Link to comment
Share on other sites

Ok, say you have this:

 

<input type="text" id="element1" />
<input type="text" id="element2" />

 

And you want to put the value of element 1 into element 2.

 

You can then use this:

 

function populate_something_else(element1, element2)
{
document.getElementById(element2).value = document.getElementById(element1).value;
}

 

Like this:

 

<input type="text" id="element1" onkeydown="populate_something_else('element1', 'element2');" />
<input type="text" id="element2" />

 

With that, whatever you type in the first input should appear in the second input as you type it.

Link to comment
Share on other sites

Hi Jackpf,

 

Thank you again. I understood the code. I can implement the code you gave. But I dont know where in the mqform.php, I can get the value of element1 (first drop down in my case).

 

The code is so complicated that, I cannot identify the variable where exactly the value will be stored, after I do the selection in the drop down.

 

-Vinay

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.