ray ray - 4 months ago 30
HTML Question

Javascript - Add users ip address to form textbox

The script below collects the users external IP and writes it to the page, I want to be able to put this in to a form textbox and write the value to a database. everything I have tried results in a null value.

Thanks

<!-- START: Here's the IP stuff -->
<script type="application/javascript">
function getIP(json) {

document.write(json.ip)

}
</script>

<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<!-- END: IP Stuff -->



<form>
<input type="text" id="ipadderss">
</form>

Answer

Isn't this what you're trying to do? Using pure javascript you can change the input value with document.getElementById("ipadderss").value = json.ip;. Also moved the form to the top.

<form>
<input type="text" id="ipadderss">
</form>
<!-- START: Here's the IP stuff -->
<script type="application/javascript">
function getIP(json) {
document.getElementById("ipadderss").value =json.ip;
}
</script>

<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
 <!-- END: IP Stuff -->

Comments