Jump to content

Sending dynamic content with jQuery


GuitarGod
Go to solution Solved by kicken,

Recommended Posts

Hi all,

 

I'm currently using jQuery's load function to send/load data into a page element, but I'm having trouble figuring out how to send dynamic data.

var option_count = $( '#opt_count' ).val(); // This number is subject to change on every page
var opts = '';
 
for ( i = 0; i < option_count; i++ )
{
  opts = opts+'&option_'+i+'='+$( '#option_'+i ).val();
}
 
// So the data above could be &option_0=7&option_1=4&option_2=9 etc
// but I can't seem to send option as an individual parameter
 
// I've tried
 
$( '#contain' ).load(
'receiver.php',
{
page_no : "2"+opts,
});
 
// I've also tried
 
 
$( '#contain' ).load(
'receiver.php',
{
page_no : "2",
opts
});
 
// What I'm trying to convey is
 
$( '#contain' ).load(
'receiver.php',
{
page_no : "2",
option_0 : 7,
option_1 : 4
// etc etc..
});

However, the number of options and their values are dynamic and subject to change. Any ideas?

Link to comment
Share on other sites

  • Solution

var option_count = $( '#opt_count' ).val(); // This number is subject to change on every page
var opts = {page_no:2};
for ( i = 0; i < option_count; i++ )
{
  opts['option_'+i] = $('#option_'+i).val();
}

$( '#contain' ).load('receiver.php', opts);
 

Create the opts object with the page_no option to start, then add each option_x property in the loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.