0

I am trying to send a form field array through my form but am unsuccessfull :-/

I have a hidden field, generated from jQuery, looking like this:

$(".imghidden").html('<input type="hidden" name="pimage[]"  value="'+data.imgname+'">');

This is generated for each file uploaded to this post. When I then submit the form I do not get anything through the "pimage" form submission. All other fields return a value?!? Below is the jQuery Ajax I am trying to use:

var $form = $( this ),
    category = $form.find( "select[name='category']" ).val(),
    newcategory = $form.find( "input[name='newcategory']" ).val(),
    title = $form.find( "input[name='title']" ).val(),
    subtitle = $form.find( "input[name='subtitle']" ).val(),
    content = $form.find( "textarea[name='content']" ).val(),
    pimage = $form.find( "input[name='pimage']" ).val()

// Send the data using post
var posting = $.post( "data/mod/projects.php", { createnew: true, cat: category, newcat: newcategory, ti: title, sti: subtitle, con: content, pimg: pimage  });

What am I doing wrong. Any help is appreciated.

Thanks in advance :-)

3
  • Is the .imghidden element that you append the hidden element to within the $form? If you do console.log($form.find("input[name='pimage']").length) what value do you get? Commented Mar 7, 2015 at 11:27
  • try $form.find( "input[name^='pimage']" ).val() Commented Mar 7, 2015 at 11:29
  • 2
    You would have better to serialize the FORM instead of building FORM data manually: api.jquery.com/serialize Commented Mar 7, 2015 at 11:30

2 Answers 2

5

Your jQuery selector is looking for an input with name pimage... which doesn't exist. I haven't tested it, but it looks like your jQuery selector should be looking for pimage[] instead.

e.g.

pimage = $form.find( "input[name='pimage[]']" ).val()
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, thank you. I also found helpful to use the :checked pseudo-selector to get the checked values. E.g. pimage = $form.find( "input[name='pimage[]']:checked" ); To get an array of checked values: var pimages = $('input[name="pimage[]"]:checked').map(function(){ return this.value; }).get();
0

You are trying to find a selector that doesn't exist. Try

$form.find( "input[name^='pimage']" ).val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.