0

I'm posting dynamically added form elements to PHP via AJAX.

I can see that the serialised form data is posted to the php, but when I try to access the data within it, some of the fields come up NULL i.e. var_dump in the PHP below shows NULL.

this is the Jquery that adds the dynamic elements:

$(function(){
var count=0;
    $('#more_edu').click(function(){
    count ++;
    $('#education_add').append('<br><br><label>University/Institution: </label><input type="text"  class="searchbox" id="edu_inst'+count+'" name="edu_inst[]" maxlength="200" value="">);
    event.preventDefault();
    });

});

and the Jquery posting to php:

function profileSub(){
var myform;
        event.preventDefault();
        myform = $('form').serialize();

     $.ajax({
  type: 'POST',
  url: 'tutorprofileinput.php',
  data: {"form": myform},

success:function(data, response, xhr){
 console.log(response); 
   console.log(data); 
   console.log(xhr);

  },
  error:function(){
    // failed request; give feedback to user
    $('#ajax-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
  }
});
}

This is the original form:

<form id="tutor_profile_input" onsubmit="return false;">
<label>University/Institution: </label>
<input type="text"  class="searchbox" id="edu_inst" name="edu_inst[]" maxlength="200" value=""> </br></br>
<label>Subject:</label> 
<input type="text" class="searchbox" id="edu_subj" name="edu_subject[]" maxlength="200" value=""></br></br>
<label> Level  </label>
<select id="edu_level" name="edu_level[]">

and the PHP itself:

<?php
if (isset($_POST['form'])){
$form = $_POST['form'];
var_dump($_POST["edu_inst"]);?>

This is the var dump of the whole $_POST:

location=&price=&tutorname=&edu_inst%5B%5D=Uni1&edu_subject%5B%5D=subje1&edu_level%5B%5D=BA&edu_inst%5B%5D=uni2&edu_subject%5B%5D=subj2&edu_level%5B%5D=BA&bio=%09&exper

4

2 回答 2

0

The form you've posted has an ID of #tutor_profile_input, where as the one you're appending to in the jQuery function is #education_add - Unless I've misunderstood?

Otherwise I'd look at specifying a more specific target in the AJAX request - You're just targetting $('form') at the moment which could be any form on the page..

于 2013-05-21T23:20:19.890 回答
0

Have discovered the answer so thought I would share - The Jquery serialize() function encodes the data into a string, which is then posted to PHP as an array with the key of "form".

In order to deal with this in php I had to first use the urldecode function in PHP to convert the string encoded elements (%5B%5D) from the name attributes. This was because there might be multiple values in these so they were declared in the form as an array ("name="edu_insts[]"). Then use parse_str to split the string into an array.

<?php
$querystring = $_POST['form'];
$querystring = urldecode($querystring);
parse_str($querystring, $params);
$insts = $params['edu_inst'];
echo $insts[0]."<br>";
echo $insts[1]."<br>";
?>

This will create an array named $params with keys corresponding to the form name attributes.

Note that if you have multiple values within the same name, then each one will be placed within an array itself, so with the above text you will have $insts[0] = University 1 and $insts[1] = University 2 etc.

Hope this helps anyone with the same problem.

于 2013-05-22T17:05:47.987 回答