0

I'm calling a javascript function in php. it works when i call alert() method directly on click. here is my code

echo "<form action='' method='post' enctype='multipart/form-data' >
          <div class='fileupload fileupload-new' style='width: 250px;' data-provides='fileupload'><input type='hidden' value='' name=''>
          <div class='fileupload-preview thumbnail' style='width: 390px; height: 250px; '></div>
          <div>
         <span class='btn btn-file btn-success'><span class='fileupload-new'>Select image</span>
         <span class='fileupload-exists'>Change</span><input type='file' name=''></span>
      <button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"alert('hi');\">Save</button>
       </div>
       </div>
       </form>";

this is the line which causing problem. it works for direct alert() method

<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"alert('hi');\">Save</button>

but when i call it through other javascript function, then this won't work.

<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"changeSitepic();\">Save</button>

here is the javascript code.

<script type="text/javascript">

function changeSitepic(){

     alert('hi');

        }

</script>

I unable to find where i'm doing wrong. please help.

3
  • Post your PHP code instead of HTML. Commented Nov 2, 2016 at 13:38
  • @KarolGasienica done Commented Nov 2, 2016 at 13:40
  • What if you inspect your code with f.ex. F12 in a browser? How does it look there in source? Maybe are there any errors in log? Commented Nov 2, 2016 at 13:43

3 Answers 3

3

I think your attribute 'id' and function name are same. Please change the 'ID' and try. Change your button like this and try. It may help you

 <button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic_id' onclick="return changeSitepic()">Save</button>
Sign up to request clarification or add additional context in comments.

Comments

2

also there is no need to use double quotes and escape them. just use single quotes.

onclick='javascript:changeSitepic()'

Comments

0

try this in your onclick

onclick=\"javascript:alert('hi');\"

2 Comments

He already tried that, and I think in alert() will be some function he wants to run onclick.
It is not calling changeSitepic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.