StevenOliver Posted April 25, 2020 Share Posted April 25, 2020 This hardcoded "999" works fine:function changeAmount(a){ document.form1.sku999.value="44"; } But since the number is a php variable, I need this to work. It does not work:function changeAmount(a){ document.form1.'sku'+a.value="44"; } In the html below, I "hardcoded" the php variable below to "999" just to try and make the simple javascript above to work.... but it doesn't. <input onClick="changeAmount('999');" type="button"> <input type="text" name="sku999"> I've spent days trying to learn javascript concatenation, but even this doesn't work: function changeAmount(a){var conc = 'sku'+a; document.form1.conc.value="44"; } If you would please let me know what I've done wrong. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/310641-is-this-a-concat-error/ Share on other sites More sharing options...
requinix Posted April 25, 2020 Share Posted April 25, 2020 I don't see any PHP in there. I do see Javascript though. Probably means this question will do better in the Javascript forum. document.form1.sku999.value="44"; sku999 is a name. It is not a variable. You cannot stick a variable in there because Javascript is only going to look at its name. document.form1.'sku'+a.value="44"; I don't know why you think you can just stick a string in there. There are rules to how this kind of stuff works. Follow the rules. document.form1.conc.value="44"; Better except that "conc" is a variable, and like I said earlier, Javascript will only look at the name. And form1 doesn't have a "conc". Fortunately there is a way to access an object member using the value of a variable or expression. Square brackets. Just like arrays. document.form1['sku'+a].value="44"; Quote Link to comment https://forums.phpfreaks.com/topic/310641-is-this-a-concat-error/#findComment-1577203 Share on other sites More sharing options...
Barand Posted April 25, 2020 Share Posted April 25, 2020 Try accessing via id <!DOCTYPE> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <script type="text/javascript"> function changeAmount(a) { var sku = document.getElementById("sku"+a) sku.value = 44 } </script> </head> <body> SKU 999 : <input name='sku999' id='sku999' value = ''><br> <button id='mybtn' onclick='changeAmount(999)'>Set value</button> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/310641-is-this-a-concat-error/#findComment-1577204 Share on other sites More sharing options...
StevenOliver Posted April 25, 2020 Author Share Posted April 25, 2020 requinix, thank you! In days of searching, I never found the brackets suggestion. Also, I'll give more attention to the correct forum topic (or, at least I should have put <?php echo..." on top LOL ) Barand, thank you! The html is a result of a PHP loop through 100+ mySQL records, and since I already have "<textarea name=sku<?=$number?>...." at least 100+ times, I thought it would "bog down the html page size" if I additionally added id="sku<?=$number?>" to all 100+ textareas. (I still think in terms of the 1990's browsers where you had to keep an eye on each extra unnecessary byte... Quote Link to comment https://forums.phpfreaks.com/topic/310641-is-this-a-concat-error/#findComment-1577206 Share on other sites More sharing options...
Barand Posted April 25, 2020 Share Posted April 25, 2020 10 minutes ago, StevenOliver said: since I already have "<textarea name=sku<?=$number?>...." at least 100+ times That form is going to be one major headache when it's posted. Use array syntax when naming your fields then processing them is just a loop, <textarea name='sku[$number]' > Quote Link to comment https://forums.phpfreaks.com/topic/310641-is-this-a-concat-error/#findComment-1577207 Share on other sites More sharing options...
StevenOliver Posted April 25, 2020 Author Share Posted April 25, 2020 It's a loop like this: while ($sku = $query->fetch_assoc()) { // a hundred results echo '<textarea name="'.$sku["product_name"].' ..... etc. And that produces a huge amount of html. I meant that if I added an id tag to that, it would almost double the amount of html sent to the browser: echo '<textarea name="'.$sku["product_name"].' id="'.$sku["product_name"].'"... etc. // Quote Link to comment https://forums.phpfreaks.com/topic/310641-is-this-a-concat-error/#findComment-1577219 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.