Eka Leonardo Eka Leonardo - 2 months ago 8
Javascript Question

How can i change the PHP value in textbox through Javascript

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

Answer

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 an click event listener?