0

I call a javascript function in my button onclick.

JS Code:

function getusername(){
    document.getElementById("strPwd").value="<?php echo get_user_name(); ?>";
}

Button Code:

<button class="btn btn-success" onclick="getusername()">Generate</button>

It works fine in the first run. But the problem is if I click again, it is not working. If I refresh the page it runs again.

What should I do ?

Raja
  • 772
  • 1
  • 15
  • 38
  • 2
    may be it works on second click, the value of '' is same always, so not noticeable..? Btw, you didn't show getpassword() function …? – Sudhir Bastakoti Feb 13 '15 at 10:19
  • try jquery like $("idofbutton").click(function(){ //your corresponding code }); add id field to button – Arunprasanth K V Feb 13 '15 at 10:20
  • i think button click event already done a postback here, so no need of refreshing , just refer DemoUser's comment too – Arunprasanth K V Feb 13 '15 at 10:22
  • @DemoUser sorry......its my typing mistake. I changed my post. – Raja Feb 13 '15 at 10:25
  • @DemoUser it is not same always. I checked it in page view source. it is different in `JS function` but not working in `button onclick` so not displaying in `strPwd` `textbox`. – Raja Feb 13 '15 at 10:36
  • @Raj — It is the same until you run the PHP again, and PHP is server side code which runs when the page is loaded, not when the button is clicked. – Quentin Feb 13 '15 at 10:41
  • @Quentin Thank you. I understood. I am trying to do it as per [this](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). – Raja Feb 13 '15 at 10:48
  • @Quentin I would like to tell you that, [this](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) is a process. But I need the way. Could you please give me a demo of ajax http request implementing my `php` code ? – Raja Feb 13 '15 at 11:22

0 Answers0