bacarudaguy Posted December 28, 2009 Share Posted December 28, 2009 I'm wondering if it's possible for JS to create and fill a variable that PHP can read? My ultimate goal is to take a value from a <select> <option> drop-down and use it to dynamically create a certain number of input fields. Example: The <select> drop-down has 1, 10 and 100 listed. The user selects 10, and PHP dynamically creates 10 text inputs on the page. (NOTE: the <select> drop-down is being filled from a populated table in the database) Is something like this possible or is JS better off being used alone... I know VERY little JS (although I'm noticing lots of similarities to PHP) and I'm really not sure how to go about something like this... Thanks! Quote Link to comment Share on other sites More sharing options...
premiso Posted December 28, 2009 Share Posted December 28, 2009 You want to look into AJAX. Quote Link to comment Share on other sites More sharing options...
bacarudaguy Posted December 28, 2009 Author Share Posted December 28, 2009 You want to look into AJAX. I had a feeling it would come down to AJAX. Yet something else I have no experience in. Time for some sleepless nights... Quote Link to comment Share on other sites More sharing options...
Alex Posted December 28, 2009 Share Posted December 28, 2009 If you're only wanting to dynamically create input elements there's no need for PHP at all. Here's an example: <script type="text/javascript"> function makeInputs(number) { document.getElementById("inputs").innerHTML = ''; for(var i = 0;i < number;i++) document.getElementById("inputs").innerHTML += '<input type="text" name="name" /><br />'; } </script> <form method="" action="" onsubmit="return false"> <select id="number" name="number" onchange="makeInputs(this.value)"> <option value="1">1</option> <option value="2">2</option> </select> </form> <div id="inputs"> </div> Quote Link to comment Share on other sites More sharing options...
bacarudaguy Posted December 28, 2009 Author Share Posted December 28, 2009 Alex - I think I can implement to make it work! That's perfect! Is there a way I can use the JS number variable in the <input>? I need to have a name & value (eg. 1-10) assigned to each input. Thanks! Quote Link to comment Share on other sites More sharing options...
Alex Posted December 28, 2009 Share Posted December 28, 2009 You can change this line: document.getElementById("inputs").innerHTML += '<input type="text" name="name" /><br />'; To something like this: document.getElementById("inputs").innerHTML += '<input type="text" name="name_' + i + '" value="' . i + '" /><br />'; To make the names like: name_0, name_1, and the value would be the number. Quote Link to comment Share on other sites More sharing options...
bacarudaguy Posted December 28, 2009 Author Share Posted December 28, 2009 PERFECT! I'll plug away at this tomorrow and see if I can make everything happen! Props to you man! Quote Link to comment Share on other sites More sharing options...
bacarudaguy Posted December 28, 2009 Author Share Posted December 28, 2009 Alex - I've got it implemented and working. Made a few changes to fit my application which wasn't terribly hard. My next question may be a bit trickier... I am using the drop down <select> that is dynamically generated from a table in my db. It has 4 columns... $query = "SELECT p_id, p_name, p_places, p_payout FROM points"; The p_places is the column that populates the value of <option value="">. The p_payout column is an array of the point values assigned to each payout place. I've attached a pic to help explain. My question is in the JS you provided, is there a way to loop through the entire array from the DB to populate a text input field with the array? Hope that makes sense... [attachment deleted by admin] Quote Link to comment 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.