1

I am wondering why my onclick function doesn't work in this particular case. My function is a pretty straightforward alert:

function confirmEditProduct(editProductID){
      alert("Worked");
  }

This is my HTML code echoed in PHP:

    echo "
      <img src='$directory/$productImage' width='200px;'></img><br><br>
      <p>Product Name: <p><input type='text' id='editName' name='editName' style='width:400px;' value='$productName'/>
      <p>Category:
          <select class='form-control' id='editCat' style='width:200px;' disabled>
            <option value='Cacti'"; if ($productCategory == '1') echo " selected='selected'"; echo ">Cacti</option>
            <option value='Succulents'"; if ($productCategory == '2') echo " selected='selected'"; echo ">Succulents</option>
          </select>

      <p>Description: <p><textarea rows='4' cols='50' id='editDesc' name='editDesc'>$productDesc</textarea>
      <p>Price: <input type='text' id='editPrice' name='editPrice' value='$productPrice'/>
      <p>Image: <input class='input-group' type='file' id='editImage' name='editImage' accept='image/*' />
      <p>Stock: <input type='text' id='editStock' name='editStock' value='$productStock'/>
      <p>
      <input type='button' class='btn btn-success' id='$productID' onClick='confirmEditProduct($productID);' value='Edit Product' />


  ";

This snippet is part of a modal btw.

Thank you to everyone who can help me out

13
  • 1
    Is your js function global? Commented Dec 5, 2016 at 0:03
  • how do i find that out? it's on my main.js file. Commented Dec 5, 2016 at 0:04
  • Can you show that code? Commented Dec 5, 2016 at 0:06
  • do you have any errors in the developer tools console? if $productID is not a numeric value, then you need to put quotes around it Commented Dec 5, 2016 at 0:06
  • i think i should mention that this code results to a generation of a couple of modals which contains the edit product button. Commented Dec 5, 2016 at 0:06

1 Answer 1

2

This seems like it would be quite simple. Are you also echoing the Javascript? If so, take an extra precaution and move it before the HTML code. Some other things you may want to consider include making the input button into a straight up button and omitting the semicolon at the end of the onclick attribute. As for styling, you may want to put the value attribute after the type attribute. Hope this helps!

Edit: When I mean echoing the Javascript, make sure you have a <script src="main.js"> or the entire script within your echoed code.

Sign up to request clarification or add additional context in comments.

7 Comments

What does it mean to "take an extra precaution and move it before the HTML code"? Yes I am also echoing the javascript. If there's a better way to do it please let me know :)
My personal preference is to use a <button type="button">, if it's not nested within a form, and you want to use inline JS. I am not sure if this makes a difference...
I have new info: on the console there is an error that says: VM4004 admin.php:1 Uncaught ReferenceError: confirmEditProduct is not defined at HTMLInputElement.onclick
As others said too, your function should be global, not nested within another function. Try this code: <button type='button' class='btn btn-success' id='$productID' onclick='confirmEditProduct($productID);' >Edit Product</button>
You may want to put the function within the echoed text in the echoed text, echo " <script> function confirmEditProduct(editProductID){ alert("Worked"); } </script> ... <input type='button' class='btn btn-success' id='$productID' onClick='confirmEditProduct($productID);' value='Edit Product' /> ";
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.