Tashen Jazbi
Tashen Jazbi

Reputation: 1068

Button onclick event is not calling javascript function in php

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.

Upvotes: 0

Views: 2138

Answers (3)

Harun Eggleton
Harun Eggleton

Reputation: 43

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

onclick='javascript:changeSitepic()'

Upvotes: 2

Azeez Kallayi
Azeez Kallayi

Reputation: 2642

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>

Upvotes: 3

Neo
Neo

Reputation: 3399

try this in your onclick

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

Upvotes: 0

Related Questions