0

I've got two disabled input fields (city and state) that are calculated automatically when a third field is filled out (zip code) in a form, the relevant parts of which are:

<form method="post" name="myemailform" action="submit.php">
    <input type="text" name="home_address_city" id="home_address_city" disabled>
    <input type="text" name="home_address_state" id="home_address_state" disabled>
    <input type="submit" name="Submit" id="submit_button">
</form>

I'm then taking the form data and submitting it via ajax to a PHP script that emails it:

$(function(){
    $('form').on('submit', function(e){
        e.preventDefault();
        $.ajax({
            type: $(this).attr('method'),
            url: $(this).attr('action'),
            data: $(this).serialize()
        });
    });
});

The PHP then grabs everything as follows:

extract($_POST, EXTR_PREFIX_SAME, "extr");

And should be outputting it in the email body as follows:

$email_body = "City:\t$home_address_city\n"."State:\t$home_address_state\n";

These disabled fields were previously filled out manually and they were being sent in the email without problem. Since disabling them, they are not being sent in the email but everything else is otherwise working. The values do get sent if I remove disabled or replace it with readonly. (Using readonly instead was considered and rejected because it allows you to focus on the field and select the text, and the client doesn't want the user interacting with the fields at all.)

So how do I pass the values of the disabled input fields to the PHP script?

Vincent
  • 2,689
  • 3
  • 24
  • 40
  • 1
    Enable them right before your `$.ajax` call. – jeroen Nov 22 '14 at 23:54
  • 1
    possible duplicate of [How to make $.serialize() take into account those disabled :input elements?](http://stackoverflow.com/questions/4748655/how-to-make-serialize-take-into-account-those-disabled-input-elements) – skobaljic Nov 23 '14 at 00:05
  • @jeroen That's was all the push I needed to figure it out. – Vincent Nov 23 '14 at 00:12
  • @skobaljic Had I found that, I probably wouldn't have needed to post a question, so there's a decent argument that this question is a duplicate. For some reason that didn't turn up when I was searching, probably because I didn't zero in on `$.serialize()` in my searches or in the question above. – Vincent Nov 23 '14 at 00:15
  • What I searched for is `jquery disabled fields serialize`. – skobaljic Nov 23 '14 at 00:30

1 Answers1

1

As suggested in the comments above, you can use JQuery to enable the disabled input elements before the $.ajax call. This post has the details.

Community
  • 1
  • 1
Vincent
  • 2,689
  • 3
  • 24
  • 40