Jump to content

[SOLVED] Checkbox validation script error


KevinM1

Recommended Posts

This is a continuation of my thread 'Form logic issue' on the PHP Help section of the board.

 

I keep getting an error on line 35, character 2, and for the life of me I can't see it.  I'm wondering if it's stemming from a logic error, as only one of the events arrays will be available for the script to grab.  If one tries to execute an asignment to elements that aren't on the page, will JavaScript return null/false?  Or will it spit out an error?

 

Of course, my error is probably the result of a stupid typo I've made somewhere.

 

Regardless, anyone mind taking a look?

 

As always, here's the code in question.  I'll post the PHP script that I use to populate the form's checkboxes, too, so you can see how I'm trying to do things.  My JavaScript is part of subheader.php.

 

registration_controller.php:

<?php

require_once "maincore.php";
require_once "subheader.php";
require_once "side_left.php";

if(!iMEMBER){
fallback();
}

if(isset($_POST['edit'])){ //for editing registrations.  only 1 registration can be edited at a time
if(isset($_POST['events'])){ //MEMBER
	$event = explode(", ", $_POST['events'][0]);

	$user_id = $event[0];
	$ev_id = $event[1];
	$login_timestamp = $event[2];

	$query = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
	$result = dbquery($query);
	$row = mysql_fetch_assoc($result);

	header("Location: ". BASEDIR ."registration.php?evid={$row['ev_id']}&regAgent={$row['registering_agent']}&agentWritingNum={$row['agent_writing_number']}&phone={$row['phone']}&email={$row['email']}&regSales={$row['regional_sales_coordinator']}&disSales={$row['district_sales_coordinator']}");
} else if(isset($_POST['personalEvents'])){ //ADMIN
	$event = explode(", ", $_POST['personalEvents'][0]);

	$user_id = $event[0];
	$ev_id = $event[1];
	$login_timestamp = $event[2];

	$query = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
	$result = dbquery($query);
	$row = mysql_fetch_assoc($result);

	header("Location: ". BASEDIR ."registration.php?evid={$row['ev_id']}&regAgent={$row['registering_agent']}&agentWritingNum={$row['agent_writing_number']}&phone={$row['phone']}&email={$row['email']}&regSales={$row['regional_sales_coordinator']}&disSales={$row['district_sales_coordinator']}");
}
} else if(isset($_POST['delete'])){ //for deleting events.  multiple events can be deleted
if(isset($_POST['events'])){ //MEMBER's personal registrations
	foreach($_POST['events'] as $value){
		$event = explode(", ", $value);

		$user_id = $event[0];
		$ev_id = $event[1];
		$login_timestamp = $event[2];

		$deleteTextQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
		$deleteTextResults[] = dbquery($deleteTextQuery);

		$deleteAflacQuery = "DELETE FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
		$deleteLoginsQuery = "DELETE FROM ". DB_PREFIX ."aw_ec_logins WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";

		$deleteAflacResult = dbquery($deleteAflacQuery);
		$deleteLoginsResult = dbquery($deleteLoginsQuery);
	}

	if($deleteAflacResult && $deleteLoginsResult){
		$deleteText = "The following registration(s) have been deleted:\n<br />\n<br />\n";
		$deleteText .= "<table cellspacing='0'>\n\t<tr>\n\t\t<th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

		foreach($deleteTextResults as $queryResult){
			while($row = mysql_fetch_assoc($queryResult)){
				$deleteText .= "\n\t<tr class='regtbl1'>\n\t\t<td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
			}
		}

		$deleteText .= "\n</table>\n";

		echo $deleteText;
	}					
} else if(isset($_POST['personalEvents'])){ //ADMIN's personal registrations
	foreach($_POST['personalEvents'] as $value){
		$event = explode(", ", $value);

		$user_id = $event[0];
		$ev_id = $event[1];
		$login_timestamp = $event[2];

		$deleteTextQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
		$deleteTextResults[] = dbquery($deleteTextQuery);

		$deleteAflacQuery = "DELETE FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
		$deleteLoginsQuery = "DELETE FROM ". DB_PREFIX ."aw_ec_logins WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";

		$deleteAflacResult = dbquery($deleteAflacQuery);
		$deleteLoginsResult = dbquery($deleteLoginsQuery);
	}

	if($deleteAflacResult && $deleteLoginsResult){
		$deleteText = "The following registration(s) have been deleted:\n<br />\n<br />\n";
		$deleteText .= "<table cellspacing='0'>\n\t<tr>\n\t\t<th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

		foreach($deleteTextResults as $queryResult){
			while($row = mysql_fetch_assoc($queryResult)){
				$deleteText .= "\n\t<tr class='regtbl1'>\n\t\t<td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
			}
		}

		$deleteText .= "\n</table>\n";

		echo $deleteText;
	}					
} else if(isset($_POST['adminEvents'])){ //ADMIN: ALL possible registrations
	foreach($_POST['adminEvents'] as $value){
		$event = explode(", ", $value);

		$user_id = $event[0];
		$ev_id = $event[1];
		$login_timestamp = $event[2];

		$deleteTextQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
		$deleteTextResults[] = dbquery($deleteTextQuery);

		$deleteAflacQuery = "DELETE FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
		$deleteLoginsQuery = "DELETE FROM ". DB_PREFIX ."aw_ec_logins WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";

		$deleteAflacResult = dbquery($deleteAflacQuery);
		$deleteLoginsResult = dbquery($deleteLoginsQuery);
	}

	if($deleteAflacResult && $deleteLoginsResult){
		$deleteText = "The following registration(s) have been deleted:\n<br />\n<br />\n";
		$deleteText .= "<table cellspacing='0'>\n\t<tr>\n\t\t<th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

		foreach($deleteTextResults as $queryResult){
			while($row = mysql_fetch_assoc($queryResult)){
				$deleteText .= "\n\t<tr class='regtbl1'>\n\t\t<td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
			}
		}

		$deleteText .= "\n</table>\n";

		echo $deleteText;
	}					
}
}

if(isset($_POST['personalSubmit'])){ //ADMIN's personal registrations
if(isset($_POST['personalEvents']) && $_POST['personalEvents'][0] == '*'){ //if ALL personal registrations are selected
	$personalQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '". $userdata['user_id'] ."'";
	$personalResult = dbquery($personalQuery);

	echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>\n";
	echo "<table cellspacing='0'>\n\t<tr>\n\t\t<th></th><th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

	$count = 0;

	while($row = mysql_fetch_assoc($personalResult)){
		if($count % 2 == 0){
			echo "\n\t<tr class='regtbl1'>\n\t\t<td><input type='checkbox' name='personalEvents[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
		} else{
			echo "\n\t<tr class='regtbl2'>\n\t\t<td><input type='checkbox' name='personalEvents[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
		}

		$count++;
	}		
	echo "</table>\n<input type='submit' name='edit' value='Edit' /><input type='submit' name='delete' value='Delete' />\n</form>";
} else if(isset($_POST['personalEvents']) && $_POST['personalEvents'][0] != ''){ //for singular personal registrations
	$event = explode(", ", $_POST['personalEvents'][0]);

	$user_id = $event[0];
	$ev_id = $event[1];
	$login_timestamp = $event[2];

	$personalQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
	$personalResult = dbquery($personalQuery);

	echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>\n";
	echo "<table cellspacing='0'>\n\t<tr>\n\t\t<th></th><th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

	while($row = mysql_fetch_assoc($personalResult)){
		echo "\n\t<tr class='regtbl1'>\n\t\t<td><input type='checkbox' name='personalEvents[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
	}

	echo "</table>\n<input type='submit' name='edit' value='Edit' /><input type='submit' name='delete' value='Delete' />\n</form>";
}
}

if(isset($_POST['submit'])){ //non-ADMIN's personal registrations
if(isset($_POST['events']) && $_POST['events'][0] == '*'){ //if ALL personal registrations are selected
	$personalQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '". $userdata['user_id'] ."'";
	$personalResult = dbquery($personalQuery);

	echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>\n";
	echo "<table cellspacing='0'>\n\t<tr>\n\t\t<th></th><th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

	$count = 0;

	while($row = mysql_fetch_assoc($personalResult)){
		if($count % 2 == 0){
			echo "\n\t<tr class='regtbl1'>\n\t\t<td><input type='checkbox' name='events[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
		} else{
			echo "\n\t<tr class='regtbl2'>\n\t\t<td><input type='checkbox' name='events[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
		}

		$count++;
	}		
	echo "</table>\n<input type='submit' name='edit' value='Edit' /><input type='submit' name='delete' value='Delete' />\n</form>";
} else if(isset($_POST['events']) && $_POST['events'][0] != ''){ //for singular personal registrations
	$event = explode(", ", $_POST['events'][0]);

	$user_id = $event[0];
	$ev_id = $event[1];
	$login_timestamp = $event[2];

	$personalQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE user_id = '$user_id' AND ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
	$personalResult = dbquery($personalQuery);

	echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>\n";
	echo "<table cellspacing='0'>\n\t<tr>\n\t\t<th></th><th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

	while($row = mysql_fetch_assoc($personalResult)){
		echo "\n\t<tr class='regtbl1'>\n\t\t<td><input type='checkbox' name='events[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
	}

	echo "</table>\n<input type='submit' name='edit' value='Edit' /><input type='submit' name='delete' value='Delete' />\n</form>";
}
}

if(isset($_POST['adminSubmit'])){ //ADMIN-only: ALL possible event registrations
if(isset($_POST['adminEvents']) && $_POST['adminEvents'][0] == '*'){ //if ALL registrations are selected
	$adminQuery = "SELECT * FROM ". DB_PREFIX ."aflac";
	$adminResult = dbquery($adminQuery);

	echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>\n";
	echo "<table cellspacing='0'>\n\t<tr>\n\t\t<th></th><th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

	$count = 0;

	while($row = mysql_fetch_assoc($adminResult)){
		if($count % 2 == 0){
			echo "\n\t<tr class='regtbl1'>\n\t\t<td><input type='checkbox' name='adminEvents[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
		} else{
			echo "\n\t<tr class='regtbl2'>\n\t\t<td><input type='checkbox' name='adminEvents[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
		}

		$count++;
	}

	echo "</table>\n<input type='submit' name='delete' value='Delete' />\n</form>";
} else if(isset($_POST['adminEvents']) && $_POST['adminEvents'][0] != ''){ //for singular registrations
	$event = explode(", ", $_POST['adminEvents'][0]);

	$user_id = $event[0];
	$ev_id = $event[1];
	$login_timestamp = $event[2];

	$adminQuery = "SELECT * FROM ". DB_PREFIX ."aflac WHERE ev_id = '$ev_id' AND login_timestamp = '$login_timestamp'";
	$adminResult = dbquery($adminQuery);

	echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>\n";
	echo "<table cellspacing='0'>\n\t<tr>\n\t\t<th></th><th>Registering Agent</th><th>Agent Writing Number</th><th>Phone Number</th><th>E-mail Address</th><th>Regional Sales Coordinator</th><th>District Sales Coordinator</th>\n\t</tr>";

	while($row = mysql_fetch_assoc($adminResult)){
		echo "\n\t<tr class='regtbl1'>\n\t\t<td><input type='checkbox' name='adminEvents[]' value='{$row['user_id']}, {$row['ev_id']}, {$row['login_timestamp']}' /></td><td>{$row['registering_agent']}</td><td>{$row['agent_writing_number']}</td><td>{$row['phone']}</td><td>{$row['email']}</td><td>{$row['regional_sales_coordinator']}</td><td>{$row['district_sales_coordinator']}</td>\n\t</tr>";
	}

	echo "</table>\n<input type='submit' name='delete' value='Delete' />\n</form>";
}
}

require_once "side_right.php";
require_once "footer.php";

?>

 

regValidate.js (I put in a comment labeling which line is line 35):

var W3CDOM = (document.createElement && document.getElementsByTagName);

function init(){
if(!W3CDOM) return;
var registrationForm = document.getElementById('registrationForm');
registrationForm.onsubmit = validate;
}

function validate(){
var events = registrationForm['events[]'];
var personalEvents = registrationForm['personalEvents[]'];
var adminEvents = registrationForm['adminEvents[]'];

var eventsResult = false;
var personalEventsResult = false;
var adminEventsResult = false;

var i;

if(events){
	for(i = 0; i < events.length; i++){
		if(events[i].checked){
			eventsResult = true;
		}
	}

	if(!eventsResult){
		alert('Please select a registration to edit or delete!');
		return false;
	}
	else{
		return true;
}
else if(personalEvents){
	for(i = 0; i < personalEvents.length; i++){ // <-- line 35
		if(personalEvents[i].checked){
			personalEventsResult = true;
		}
	}

	if(!personalEventsResult){
		alert('Please select a registration to edit or delete!');
		return false;
	}
	else{
		return true;
}
else if(adminEvents){
	for(i = 0; i < adminEvents.length; i++){
		if(adminEvents[i].checked){
			adminEventsResult = true;
		}
	}

	if(!adminEventsResult){
		alert('Please select a registration to delete!');
		return false;
	}
	else{
		return true;
	}
}
else{
	alert('Unforseen error!');
	return false;
}
}

window.onload = init;

 

Thanks in advance! :)

Link to comment
Share on other sites

if(events){
    for(i = 0; i < events.length; i++){
        if(events[i].checked){
            eventsResult = true;
        }
    }

    if(!eventsResult){
        alert('Please select a registration to edit or delete!');
        return false;
    }
    else{
        return true;
}

-missing closing '}' on the final else

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.