-1

I have a textbox the value load from PHP Here is the code

<input  type="text" id="fName" value="<?php echo $row['fName']; ?>" disabled/>
<input type="button" value="Edit" onclick="changeValue('fName')" />

my Javascript

function changeValue(id){  
var value = "test";
document.getElementById(id).value = value;
}

As soon as I click the function, the value change for a few second and go back to default one from database. Can anyone help me

3 Answers3

4

After clicking on the button, the page will reload so that the value of the database will be restored.

Please change

<input type="button" value="Edit" onclick="changeValue('fName')" />

to

<input type="button" value="Edit" onclick="changeValue('fName'); return false;" />

to avoid submitting the form.

For more information, please have a look at this: What's the effect of adding 'return false' to a click event listener?

Community
  • 1
  • 1
mario.van.zadel
  • 2,878
  • 13
  • 20
0

If you click on elements like links (a element) or buttons (input[type="button"] or button) page will be redirect. So at the end of onClick action you must write return false to force say to the browser I don't want to redirect this page after click!

For example:

function changeValue(id){
    var value = "test";

    document.getElementById(id).value = value;

    return false;
}
ventaquil
  • 2,661
  • 3
  • 20
  • 45
0

How can i change the PHP value in textbox through Javascript

An alternative to using document.getElementById(id).value would be:

function changeValue(id){  
      var value = "test";
      document.getElementById(id).setAttribute("value", value);
}
Charly H
  • 147
  • 9
  • Code only answers are discouraged. Please add explanation of what you code is doing and why and how it answers the OP's question. – jwpfox Sep 13 '16 at 07:59