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
https://forums.phpfreaks.com/topic/186475-php-javascript/
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
https://forums.phpfreaks.com/topic/186475-php-javascript/#findComment-984689
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
https://forums.phpfreaks.com/topic/186475-php-javascript/#findComment-984707
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
https://forums.phpfreaks.com/topic/186475-php-javascript/#findComment-985134
Share on other sites

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.