When the button is pressed in the following string, the number variables work fine, but the string ($title1) doesn't. Can't get it. foreach ($dbo->query($q1) as $row) { $title1 = $row['ProductName']; $string1 = " <button id = \"\" onclick = \"SaveProductItems( $title1, $id, $cost,$quantity)\">Submit</button> "; PHP: function SaveProductItems(title, id,cost,quantity){ let t = title; Code (JavaScript):
Is there any way to do this: $titleID = "titleID" . $counter; $string1 = " div class=\"col\"> <h4><center><p id =\"\" >Title</p></center></h4> <center> <p > <input id = \"$titleID\" type=\"text\" name=\"title\" value = \"$title1\"placeholder=\"title1\"></p></center> </div> <button id = \"\" onclick = \"SaveProductItems( $titleID)\">Submit</button> "; PHP: function SaveProductItems(titleID){ var val1 = document.getElementById(titleID).value; Code (JavaScript): thanks...
Well I did this...both, here: foreach ($dbo->query($q1) as $row) { $counter = $counter + 1; $titleID = "titleID" . $counter; $descID = "descID" . $counter; $costID = "costID" . $counter; $quantityID = "quantityID" . $counter; string1 = " <div class=\"container\"> <div class=\"row\" > <div class=\"col\"> <h4><center><p id =\"\" >Title</p></center></h4> <center> <p > <input id = \"$titleID\" type=\"text\" name=\"title\" value = \"$title1\"placeholder=\"$title1\"></p></center> </div> <div class=\"col\"> <h4><center><p id = \"\">Desc</p></center></h4> <textarea wrap id = \"$descID\" name=\"text\" rows=\"5\" cols=\"34\">$description</textarea> </div> <button id = \"\" onclick = \"SaveProductItems( $titleID, $descID, $costID,$quantityID)\">Submit</button> PHP: function SaveProductItems(title, desc,cost,quantity){ var val1 = document.getElementById(title.id).value; var val2 = document.getElementById(desc.id).value; var val3 = document.getElementById(cost.id).value; var val4 = document.getElementById(quantity.id).value; Code (JavaScript):
If I understand this correctly, you are trying to render those php variables in the HTML button. In this case, this should be this: $string1 = "<button id = \"\" onclick = \"SaveProductItems( ".$title1.", $id, $cost,$quantity)\">Submit</button> "; PHP: Notice this (".$title1."). If I am right about your intentions. then this should work.
Let's leap forward a decade and do something like this, but I'd probably change counter to something more exact like $row['product_id']. By standardising the names of the input variables you avoid having inputs that change all over the place and you're passing the simplest info to javascript. Your onclick event should also be a listener but we'll save that for another day. Onclick is fine in this instance. {} curly brackets tell the server the exact name of a variable. '' single quotes mean you don't have to escape the string and makes it easier to read. The only place I left doubles in was in the title because it could contain something like "O'Keefes Elixir" that will be buggy with single quotes. <?php foreach ($dbo->query($q1) as $row) { $counter = $counter + 1; // the H4 should be styled with css, shouldn't have a <p> tag inside it $string1 = " <div class='container'\"'> <div class='row' > <div class='col'> <h4 id=''>Title</h4> <p style='text-align: center;' ><input id = '{$titleID}' type='text' name='title' value = \"{$title1}\" placeholder=\"{$title1}\"></p> </div> <div class='col'> <h4 id=''>Desc</h4> <textarea wrap id='desc{$counter}' name='text' rows='5' cols='34'>{$description}</textarea> </div> <button id='' onclick='SaveProductItems({$counter})'>Submit</button> </div> </div>"; ?> <script> function SaveProductItems(counter){ var val1 = document.getElementById('title'+counter).value; var val2 = document.getElementById('desc'+counter).value; var val3 = document.getElementById('cost'+counter).value; var val4 = document.getElementById('quantity'+counter).value; } </script> Code (markup):
Use the single quote (') sign around $title1 when passing it via javascript. Like this: $string1 = "<button id = \"\" onclick = \"SaveProductItems('{$title1}', $id, $cost, $quantity)\">Submit</button>
Thank you everyone. I tested and Jeet got it right. I think it is so neat now that I can pass strings! Enjoy your new year, Joshua