Member Avatar for diafol

Hi folks, getting a problem adding name/values to make an array to pass to php.
I've got a number of inputs named week_start_x (where x is an integer ranging from 1 to a dynamic number [generated through php] - the max no. is determined by another field called 'yearblocks').

I want to pass these name/value pairs to a php script. However, I get an 'undefined' when I fish out the $_POST variables for the array. Anybody help me out? I assume it's to do with the fact that the 'str' variable is printing out a string as opposed to a list of name/values, but I have no idea of how to go about this.

var cnt = 1;
str = "";
blocks = $("#yearblocks").val(); 
while (cnt <= blocks){
   str = str + ',{name: "week_start[\'' + cnt + '\']", value: "' +  $('#week_start_' + cnt).val() + '"}';
   cnt++;
}	
var data = [
    { name: "step", value: "2"} , str
];

$.post("includes/receive.php",  data , function(msg){ ...(etc)...
});

Recommended Answers

All 3 Replies

Hi Ardav,

How's it going?

This may work:

var cnt = 1;
var blocks = $("#yearblocks").val(); 
var week_starts = [];
while (cnt <= Number(blocks)) {
    eek_starts.push($('#week_start_' + cnt).val());
    cnt++;
}	
var data = {
    step : "2",
    weeks : blocks,
    week_starts : week_starts
};

$.post("includes/receive.php",  data , function(msg){ ...(etc)...
});

though I'm not sure how the "week_starts" array appears in php. Presumably it needs to be decoced.

Alternatively, you could try this :

var cnt = 1;
var blocks = $("#yearblocks").val(); 
var data = {
    step : "2",
    weeks : blocks
};
while (cnt <= Number(blocks)) {
    data['week_start_'+i] = $('#week_start_' + cnt).val();
    cnt++;
}	
$.post("includes/receive.php",  data , function(msg){ ...(etc)...
});

which should be more straightforward to extract from $_POST.

  • $_POST
  • $_POST//if needed
  • $_POST
  • $_POST
  • etc.

Airshow

commented: Big hand +7
Member Avatar for diafol

Ho ho ho! I'm a real n00b at js - I keep treating it like php. Airshow - thanks a million, I will try it out pronto.

Long time, me old.

//EDIT

FYI: the first method worked a treat! I've been at it all day - I'd offer you a big beer if you weren't flying!

I will give you an attribution on the site/app once it's completed.

Ardav,

You solved it even dispite my eek_starts typo!!

I'll have a beer later anyway. What a good idea.

A

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.