Hector
Hector

Reputation: 668

form values updated with jquery are not submitted to php

I have a problem where some of my form fields are not being picked up in the $_GET in my php.

I have a checkbox that when checked fills some form fields with previously entered data. The problem is that when the data is entered manually it shows up and when it isn't it doesn't.

This is the jQuery that changes the values.

$("#same_as").change(function() {
  var delivery = $("input[name^=delivery_]");
  var invoice = $("input[name^=address_]");

  if ($(this).prop("checked")) {
    for (var i = 0; i < delivery.length; i++) {
      // populate the delivery fields with the invoice address values
      $(delivery[i]).val($(invoice[i]).val());
    }

    delivery.prop("disabled", true);
  }
  else {
    delivery.each(function() {
      // empty the delivery fields
      $(this).val("");
    });

    delivery.prop("disabled", false);
  }
});

This is the code that submits the form:

$("#register_form").submit(function(e) {
  e.preventDefault();

  $.get($(this).attr("action"), $(this).serialize(), function(data) {
    data = $.parseJSON(data.replace("<!DOCTYPE html>", ""));

    console.log(data);
  });
});

The php is as simple as: (there are more checks for the other fields)

if (isset($_GET["delivery_line_1"])) echo $_GET["delivery_line_1"];

When the fields are filled manually the data is sent. When the fields are filled by the checkbox function then the data isn't sent. The strange part is that when I console.log() the data before the ajax request is made it is present regardless.

Upvotes: 0

Views: 68

Answers (1)

edmondscommerce
edmondscommerce

Reputation: 2011

Your problem is that you are disabling the fields. That does stop them sending data.

Just hide the fields or something instead

Upvotes: 2

Related Questions