vinaysnarayan Posted April 12, 2009 Share Posted April 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/ Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 No. PHP is server side. Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808116 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 Hi Jackpf, Thanks for your reply. But how to get this done through php? Any code for this? Thanks, Vinay Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808119 Share on other sites More sharing options...
.josh Posted April 12, 2009 Share Posted April 12, 2009 You can't. php is a server-side language. It cannot dynamically change elements in a browser on a client. That's what client-side languages are for. Like javascript. Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808123 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808124 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 Oh ok. But if that is the case, how to get this done? Can this be done in Java/html ? any other method to get this done? Thanks for your time and post in advance. -Vinay Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808125 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 What's wrong with the code you had in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808126 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808139 Share on other sites More sharing options...
.josh Posted April 12, 2009 Share Posted April 12, 2009 So you want to take values from other elements, click a button, and have another element get populated with those values? Yes we heard you the first time. And the 2nd. What part of "php cannot do this" do you not understand? Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808142 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808147 Share on other sites More sharing options...
nankoweap Posted April 12, 2009 Share Posted April 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808153 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808155 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808157 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 jackpf, Indeed i'm new to php, as mentioned by my initial email. I've downloaded some GPL scripts and trying to modify them according to my needs. Regards, Vinay Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808158 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 Why not just insert the data into both tables then? I'm not sure I understand... Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808161 Share on other sites More sharing options...
vinaysnarayan Posted April 12, 2009 Author Share Posted April 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808167 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 function populate_something_else(element1, element2) { document.getElementById(element2).value = document.getElementById(element1).value; } Why not use that to migrate the data across from the first form to the second? Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808179 Share on other sites More sharing options...
vinaysnarayan Posted April 13, 2009 Author Share Posted April 13, 2009 Oh ok. Let me try this and get back to you. thank you for your advice. Regards, Vinay Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808369 Share on other sites More sharing options...
vinaysnarayan Posted April 13, 2009 Author Share Posted April 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808672 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 What's wrong with the code I sent you? Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808676 Share on other sites More sharing options...
vinaysnarayan Posted April 13, 2009 Author Share Posted April 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808682 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808713 Share on other sites More sharing options...
vinaysnarayan Posted April 13, 2009 Author Share Posted April 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808723 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 I'm not entirely sure what you mean. If this code is so complicated for a simple register script, maybe you should consider rewriting it. A register script shouldn't be that hard. Quote Link to comment https://forums.phpfreaks.com/topic/153764-populate-a-text-box-from-drop-down/#findComment-808725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.