0

I've been wrestling with this problem for a couple hours now.

Essentially, what I need to do is take the following or similar HTML:

<div id="excpdivs">
<div class="excpdiv" id="excpdiv0">
      Date: <input name="excp[0][date]">
      Open:  <input name="excp[0][open]">
      Close: <input name="excp[0][close]">
</div>
<div class="excpdiv" id="expdiv1">
      Date: <input name="excp[1][date]">
      Open:  <input name="excp[1][open]">
      Close: <input name="excp[1][close]">
</div>

and get an array similar to the following to a php script via AJAX:

   Array
(
    [0] => Array
        (
            [date] => 2012-09-15
            [open] => 3:00
            [close] => 5:00
        )
   [1] => Array
        (
            [date] => 2012-09-16
            [open] => 2:00
            [close] => 5:00
        )

)

My main problem is getting the values from the input elements. My latest attempt is the following:

    var results = [];
$(".excpdiv").each(function(){
    var item = {};
    var inpts = $(this).find("input");
    item.date = $(inpts.get(0)).val();
    item.open = $(inpts.get(1)).val();
    item.close = $(inpts.get(2)).val();
    results.push(item);
});

Am I on the right track or am I hopelessly lost?

5
  • 1
    Standard form submit, or Ajax? (Unrelated to your problem, you can say inpts.eq(0).val() instead of $(inpts.get(0)).val() - saves you having to create another jQuery object yourself.) Commented Dec 5, 2012 at 5:01
  • What is your issue? The code could be improved, but it's ok as is. Commented Dec 5, 2012 at 5:07
  • nnnnnn, Ajax instead of a standard form submit Commented Dec 5, 2012 at 5:11
  • Did you solve your problem? Commented Dec 5, 2012 at 5:27
  • James, nope, still not getting any values in to item.date, item.close, or item.open. Thanks for your help though. I think I'm going to try and sleep on this one. Commented Dec 5, 2012 at 5:33

3 Answers 3

1

Maybe a simple serialize.

From: api.jquery.com/jQuery.post/

Example: Send form data using ajax requests


$.post("test.php", $("#testform").serialize());
Sign up to request clarification or add additional context in comments.

1 Comment

.serialize() is just the method I needed. Thanks!
0

i think you should do this instead

var results = [];
$(".excpdiv").each(function(){
    var item = {};
    var inpts = $(this).find("input");
    item.date = $(inpts)[0].val();
    item.open = $(inpts)[1].val();
    item.close = $(inpts)[2].val();
    results.push(item);
});

Hope it helps...

3 Comments

Thanks for the prompt response. However, I'm getting the following error with your code: Uncaught TypeError: Object #<HTMLInputElement> has no method 'val'
lol i just now tried your own code it seems to work what exactly do you want from it??
Why are you calling .val() after using things like [0]? When you use [0], you are getting the DOM element, which doesn't have jQuery methods...
0

You don't need jquery for this, the HTML markup you have will provide an array for php. Enclose the inputs in a form that posts to a php script to handle logic. Then you can access your information with:

$_POST['excp'][0]['date']

2 Comments

I should have specified that I want to send this via ajax instead of submitting the form directly to the php
Where did you specify that, its not even a tag. In any case use $.post to send a post request.api.jquery.com/jQuery.post

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.