Jump to content

[SOLVED] dynamic forms and javascript


RIRedinPA

Recommended Posts

Hi

 

I am building some forms using PHP based on a MySQL query, when I build them the I need to determine the value of the checkboxes in each form, so for example:

 

Database query is made and returns 2 results, so I will have two forms, named group_1 and group_2

 

Each form will have several checkbox inputs in them so the form would like something like this:

 

<form name=group_1>

<input type='checkbox' name='copydrop_1' id='copydrop_id_1' onChange="somejavascript(this.name, this.id);">

</form>

 

<form name=group_2>

<input type='checkbox' name='copydrop_2' id='copydrop_id_2' onChange="somejavascript(this.name, this.id);">

</form>

 

when the user changes the checkbox a javascript function is called (which runs an AJAX call to update the database with the change in value and returns the new value to the page).

 

how do I determine which form and checkbox was selected if I am, in Javascript, passing variables for those items. For example:

 

function somejavascript(fieldname, fieldid) { 
//get which form
var fieldidarray = fieldid.split("_");
var formname = "group_" + fieldidarray[2];

//determine if checkbox is on or off
if (document.formname.fieldname.checked == true) { 
var dbvalue = 1; 
} else { 
var dbvalue = 0;
}

//-->AJAX Stuff
}

 

I keep getting an error document.formname is undefined. Any help would be appreciated.

Link to comment
Share on other sites

I figured it out, it's been a while since I've worked with Javascript...

 

function somejavascript(fieldname, fieldid) { 
//get which form
var fieldidarray = fieldid.split("_");
var formname = "group_" + fieldidarray[2];

//determine if checkbox is on or off
if (document.forms[formname].elements[fieldname].checked == true) { 
var dbvalue = 1; 
} else { 
var dbvalue = 0;
}

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.