-1

Can anybody see where I have gone wrong? I am trying to get the value of a field in a form via POST.

The field is disabled and pre-filled.

<form method="POST" action="actions/remove-daily-recipient.php">
    <input type="text" name="recipientemail" value="email@email.com" disabled />
    <input type="submit" value="Remove"/>
 </form>

The form submits correctly and goes to my script remove-daily-recipient.php

However the first line of this script, which should grab the value of the email field, gets an error:

$email = htmlspecialchars($_POST["recipientemail"]);
echo $email;

// The error
Notice: Undefined index: recipientemail in [path/name] on line 5

What would be the cause of this?

A few things I am thinking:

  1. There are multiple forms on the page. However shouldn't the submit button within that form only fire and POST the data filled in on that particular form? It shouldn't affect any other forms?
  2. The data is pre-filled and the field is disabled. Would it not like this? I think it should be able to get data from a pre-filled and disabled field?

Thanks!

Francesca
  • 26,842
  • 28
  • 90
  • 153
  • possible duplicate of [how to POST/Submit an Input Checkbox that is disabled?](http://stackoverflow.com/questions/4727974/how-to-post-submit-an-input-checkbox-that-is-disabled) – steffen Jan 13 '15 at 12:14
  • Use if(isset($_POST['recipientemail'])){... } – Delphin Sam Jan 13 '15 at 12:20

5 Answers5

3

The problem is disabled fields are not submitted.

<input type="text" name="recipientemail" value="email@email.com" disabled />

You can use readonly property so that it'll be included in POST.

<input type="text" name="recipientemail" value="email@email.com" readonly style="color: rgb(84,84,84); background: rgb(235,235,228); border: 1px solid rgb(169,169,169); padding: 1px;" />

If you need readonly fields to look like disabled a simple CSS rule should suffice.

Kevin
  • 41,694
  • 12
  • 53
  • 70
1

You can use readonly attribute in your case, by doing this you will be able to post your field's data.

Amit Khese
  • 102
  • 11
1

The disabled attribute is a boolean attribute. When present, it specifies that the element should be disabled and it should not pass any value you can use below two option:

<input type="text" name="recipientemail" value="email@email.com" readonly />

or

<input type="text" name="recipientemail" value="email@email.com" />
Priyank
  • 3,778
  • 3
  • 29
  • 48
  • Thanks! This was exactly it. I thought it was something along those lines. Thanks for the great answer. – Francesca Jan 13 '15 at 12:19
0
if(isset($_POST['recipientemail'])){
    $email = htmlspecialchars($_POST["recipientemail"]);
    echo $email;
}

you can check if the 'recipientemail' array index exists before using it, to avoid errors.

Cimbali
  • 11,012
  • 1
  • 39
  • 68
Hải Lê
  • 1
  • 1
0

The problem is already answered, so will only add to the solution here. Since the disabled fields are not submitted with the form and are ignored all together, use can use the readonly field as already answered.

Solution 1.

<input type="text" name="recipientemail" value="email@email.com" readonly />

But if you still want to keep the styling of a disabled field(which reflects that the field is uneditable) as well as submit it, you can use a hidden field with your code:

Solution 2.

<form method="POST" action="actions/remove-daily-recipient.php">
    //this field will be shown as greyed-out
    <input type="text" name="justforshowing" value="email@email.com" disabled />
    //this field will be submitted
    <input type="hidden" name="recipientemail" value="email@email.com" />
    <input type="submit" value="Remove"/>
</form>

Hope this adds to your solution.

Anurag
  • 1,018
  • 1
  • 14
  • 36