Jump to content

PHP & Javascript


bacarudaguy

Recommended Posts

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... :shrug: 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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... :o

 

[attachment deleted by admin]

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.