0

I have an input in a contact form to record the users IP address:

<input type="text" class="form-control" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" disabled>

This works ok. It shows the users IP address when they are completing the contact form.

The problem is including this in the e-mail.

I am including it in the e-mail using:

  $ip = $_POST['ip'];

but it generates this error message on send:

Notice: Undefined index: ip in /var/www/contact.php on line 51

I don't get this error message with any other inputs so I assume it is to do with using disabled in the form?

  • Take a look at [Disabled form inputs do not appear in request](http://stackoverflow.com/q/7357256/908174). Voting to close as duplicate. – Luís Cruz Apr 18 '15 at 20:39

3 Answers3

1

disabled doesn't cause this error, two things you should do: 1. check your website is validated using the W3C validator. 2. Try using 'readonly' instead of 'disabled' see what happens EDIT: 1. check for meta charset, use utf-8 is it's alright.

EDIT 2: Please note that $_SERVER['REMOTE_ADDR']; does not always resolve the client's ip address.

odedta
  • 2,430
  • 4
  • 24
  • 51
  • `readonly` makes it work. Why do all online references say to use this `disabled`? Thanks I'll accept when it allows. –  Apr 18 '15 at 19:34
  • You can find more info @ http://www.w3.org/TR/html401/interact/forms.html#disabled – odedta Apr 18 '15 at 19:38
0

Don't use disabled.

Do this instead:

<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">

Because disabled means the value of that field isn't sent when the form is submitted.

manlikeangus
  • 421
  • 9
  • 28
0

From MDN:

... a disabled control's value isn't submitted with the form.

You can solve this using readonly instead.

Marcio Mazzucato
  • 8,841
  • 9
  • 64
  • 79