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
https://forums.phpfreaks.com/topic/121407-solved-dynamic-forms-and-javascript/
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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.