Link to home
Start Free TrialLog in
Avatar of JakeD14
JakeD14

asked on

PHP Survey with Javascript Form Validation problem

having a problem getting my form validation to work. I have a sql db generating a survey list of questions but am having trouble getting the data from the second question to javascript. Just trying to get the answers to show in an alert box at the moment but cant even manage to do that...

<?PHP
$vocID = $_GET['id'];
$count = 0;
?>
<script>
function formValidation(){
var checkA;
var checkB;
var checkC;
var checkD;
var checkE;
var checkF;

var questionA = new Array;
var questionB = new Array;
var questionC = new Array;
var questionD = new Array;
var questionE = new Array;
var questionF = new Array;

var i = 0;
var j = 1;
var qNum = document.getElementById('qNum').value;  

while (i <= qNum){
	var qID = 'q'+j;

	checkA = qID+'A';
	checkB = qID+'B';
	checkC = qID+'C';
	checkD = qID+'D';
	checkE = qID+'E';
	checkF = qID+'F';
	
	questionA[i] = document.getElementById(checkA).checked;
	questionB[i] = document.getElementById(checkB).checked;
	questionC[i] = document.getElementById(checkC).checked;
	questionD[i] = document.getElementById(checkD).checked;
	questionE[i] = document.getElementById(checkE).checked;
	questionF[i] = document.getElementById(checkF).checked;
		
        alert (checkA+': '+questionA[i]);
	alert (checkB+': '+questionB[i]);
	alert (checkC+': '+questionC[i]);
	alert (checkD+': '+questionD[i]);
	alert (checkE+': '+questionE[i]);
	alert (checkF+': '+questionF[i]);

        j++;
	i++;
}
}
</script>
</head>

<body>
<form name='myForm' onSubmit="return formValidation();" action='VOC.php' method='post'>
    <?PHP
   	$userQuery = "SELECT * FROM `voc` WHERE `vocID` = '$vocID'";
	$userResult = mysql_query($userQuery) or die('Query failed: ' . mysql_error());
					
	while($summary = mysql_fetch_assoc($userResult))
	{
		$count++;
		$qID = $summary['qID'];
		$question = $summary['question'];
		$q1 = $summary['q1'];
		$q2 = $summary['q2'];
		$q3 = $summary['q3'];
		$q4 = $summary['q4'];
		$q5 = $summary['q5'];
		$q6 = $summary['q6'];
		$labelA = "q".$qID."A";
		$labelB = "q".$qID."B";
		$labelC = "q".$qID."C";
		$labelD = "q".$qID."D";
		$labelE = "q".$qID."E";
		$labelF = "q".$qID."F";
		$answer = $summary['answer'];
		
		echo "$qID.". ".$question."<br />";
		if($q1 != ""){echo "$q1 <input name='".$qID."' id='$labelA' type='radio' /><br />";}
		if($q2 != ""){echo "$q2 <input name='".$qID."' id='$labelB' type='radio' /><br />";}
		if($q3 != ""){echo "$q3 <input name='".$qID."' id='$labelC' type='radio' /><br />";}
		if($q4 != ""){echo "$q4 <input name='".$qID."' id='$labelD' type='radio' /><br />";}
		if($q5 != ""){echo "$q5 <input name='".$qID."' id='$labelE' type='radio' /><br />";}
		if($q6 != ""){echo "$q6 <input name='".$qID."' id='$labelF' type='radio' /><br />";}
		echo "<input name='answer".$qID."' id='answer' value='".$answer."' type='hidden' />";
	}
	?>
	<input type='hidden' value='<?=$count;?>' name="qNum" id="qNum" /><input type='submit' value='Next->' /><br />

Open in new window

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Php runs on the server including any functions and calculations to finally generate any html output you programmed in php.  Once the rendered html is sent to the browser, php is no longer available and your css and  js/jquery do their work.

If you are having issues with your js/jquery, can you post your rendered html page?  The first thing to check is if php is outputting what you expect.   Next is checking for any js/jquery errors but we need the rendered html.
Avatar of JakeD14
JakeD14

ASKER

Yes the html renders. It just wont make it just wont run a second loop of the javascript while loop.
When I read code I like things to be absolutely clear. Having experience in many strongly typed programming languages I do like to see proper use of datatypes. When i see:

var qID = 'q'+j;

where j is an integer being added to a string it fills me with pain. Perhaps you could try:

var qID='q'+strval(j);

I also like to use the . concatenation operator (the full stop) to concatenate strings and keep + for adding numbers only.

Let me know if this solves your problem.
Javascript is not a strongly typed language and can be hard to get your head around for that reason. The example that  JakeD14 uses is correct.   However, this has nothing to do with his issue.

The problem appears to be a client side issue and we need either a link to the sample page or post the rendered html here or to jsbin.com or jsfiddle.com.   If you can simply use sample data and post that link to your own site would be easiest to diagnose. http://sscce.org/
ASKER CERTIFIED SOLUTION
Avatar of Adrian Heal
Adrian Heal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial