Steven
Steven

Reputation: 19425

Need help with associative arrays

My brain is fried after 10 hours of coding, so I need some help.

I have used the following function to retrieve data from a form submission (before I process the data, verifying input etc):

  // All form fields are identified by '[id]_[name]', where 'id' is the 
  // identifier of the form type. Eg. brand, store etc.
  // The field identifier we want to return is just the name and not the id.
  public function getFormData() {
    $formData = array();
    foreach ($_POST as $key => $value) {
      $name = preg_replace('!.*_!', '', $key);
      if (is_array($value)) {
        $formData[$name] = implode(',', $value);
      } else {
        $formData[$name] = $value;
      }
    }
    return $formData;
  }    

Now I'm submitting the form using AJAX, so I'm not able to use this function any more.

My $_POST['formData'] string looks like this (short version):

"store_name=My+new+store&store_street1=Some+address&store_zipcode=1188"

My goal is to be able to execute the following code:

echo $formData['name'] //Displays 'Some address'

My jQuery code looks like this:

  function processRegistration()
  {

    var formData = jQuery('#registerStore form').serialize();

    jQuery.post("mypath/jquery_bll.php", { instance: 'processRegistration', formData : formData },
      function(data)
      {
          alert('some test data here');
      },"json");

How can I change my function to handle data from an Ajax call?

Upvotes: 0

Views: 254

Answers (4)

andho
andho

Reputation: 1172

I notice that your using:

jQuery.post("mypath/jquery_bll.php", { instance: 'processRegistration', formData : formData },

In your code which will most probably output:

instance=processRegistration&formData=field1=value1&field2=value2

So what the php script will get is:

$_POST = array(
  'instance'=>'processRegistration',
  'formData'=>'field1=value1',
  'field2'=>'value2
);

Edit: This is because the serialized object will create a query string that is ready to be sent and then you are putting it inside an object for the data parameter.
The data parameter either accepts an key/value object or query string from the likes of jquery.fn.serialize.
http://docs.jquery.com/Ajax/jQuery.post
So maybe if you change this line:

jQuery.post("mypath/jquery_bll.php", { instance: 'processRegistration', formData : formData },

..to ..

jQuery.post("mypath/jquery_bll.php", formData + '&instance=processRegistration',

that would work

Upvotes: 1

andho
andho

Reputation: 1172

If you are open to using plugins you could use this little plugin although it is not available in the jquery plugin repo

$.params2json = function(d) {
    if (d.constructor != Array) {
        return d;
    }
    var data={};
    for(var i=0;i<d.length;i++) {
        if (typeof data[d[i].name] != 'undefined') {
            if (data[d[i].name].constructor!= Array) {
                data[d[i].name]=[data[d[i].name],d[i].value];
            } else {
                data[d[i].name].push(d[i].value);
            }
        } else {
            data[d[i].name]=d[i].value;
        }
    }
    return data;
};

You can use the following code with the plugin:

function processRegistration()
{
  var formData = $.params2json($('#registerStore form').serializeArray());
  formData.instance = 'processRegistration';
  $.post('mypath/jquery_bll.php', formData,
    function(data) {
      alert('some test data here');
    }, "json");
  });
}

Upvotes: 0

enobrev
enobrev

Reputation: 22532

There's no functional difference between an ajax call and a regular call from a browser.

So, to answer...

$formData = getFormData();
echo $formData['name'];

Upvotes: 0

cletus
cletus

Reputation: 625037

If you're using $.post() there is no difference. It's just a POST request.

Upvotes: 3

Related Questions