0

Possibly very easy to do but I cannot work it out!

I'm trying to make a button which inserts row value pulled out from the database into a textfield.

<?php while ($row = mysql_fetch_array($query)) { ?> <tr> 
<td><?php echo $row['mobile_number']; ?></td>
<td><input type="button" value="select" onclick="clickMe()" /></td>
</tr>
<?php } ?>

<input type="text" id="textfield" />

so when the button "select" is clicked, the textfield would get populated with the mobile number.

Many thanks for your help in advance.

3
  • Unless I'm missing something: <input type="text" id="textfield" value="<?php echo $row['mobile_number']; ?>" /> ? Commented Nov 4, 2013 at 12:45
  • @AmalMurali That would be instant instead of on click Commented Nov 4, 2013 at 12:46
  • Use jquery using .on method Commented Nov 4, 2013 at 12:56

1 Answer 1

1

Try this:

<script>
function clickMe(number) {
    document.getElementById("textfield").value = number;
    // or jQuery
    $("#textfield").val(number);
}
</script>

<?php while ($row = mysql_fetch_array($query)) { ?> <tr> 
<td><?php echo $row['mobile_number']; ?></td>
<td><input type="button" value="select" onclick="clickMe(<?php echo $row['mobile_number']; ?>)" /></td>
</tr>
<?php } ?>

<input type="text" id="textfield" />
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much! document.getElementById("textfield").text = number; didn't work but the jQuery one did, any idea why?
Yeah I think it should be .value Happy to help.
thanks, yes, .value worked! i've just tried this with another column which the values hold a string, e.g. "AB214" and it's not pulling the information now, does .val only pull integers?
YOu could try converting it to a string before you set it. number.toString()
Like this?: function clickMe2(text) { text.toString().$("#txtfield").val(text); } -- it's not working :(
|

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.