0

Hi,

I'm open to suggestions on the following project I'm trying to complete. I will post what I have currently done but I think that I'm limited in what I can do with the code as a form can only have 1 action.

Problem - I have a short questionnaire that requires 5 questions to be answered. Question 1 is a text field. Questions 2-4 are radio button/single choice and question 5 is a free text box.

Based on the inputs of questions 2,3 and 4 I want it to choose the most appropriate team for the player (of which there are 3 choices).

I also require it to send me an email containing their answers aswell as the best team for them (which the script is obviously deciding).

How I've approached it - I have found a javascript which redirects to the appropriate team page but will not allow me to send an email. I don't think there is a way for the values to be carried across either as the action of the form is javascript:finish()?

Could anybody assist me with the logic of resolving this problem? Would I be better off storing teams in a database and scrapping the whole javascript? If I did that what sort of code would I be looking to write?

Many thanks for your help in advance

JayJ

Quiz page

<head><!-- begin quiz script -->
<script>
function finish() {
	var ext = '.php';
	var results = new Array("none","Team1","Team2","Team3");
	var nums = new Array(4);
	for(var i = 0; i < nums.length; i++) nums[i] = 0;
	for(var i = 1; i <= 4; i++) {
		var q = document.forms['quiz'].elements['question_'+i];
		if(q[0].type=='checkbox') {
			var n = 0;
		}
		for(var j = 0; j < q.length; j++) {
			if(q[j].checked) {
				var a = q[j].value.split(',');
				for(var k = 0; k < a.length; k++) {
					nums[a[k]]++;
				}
				if(q[j].type=='radio') break;
				else n++;
			}
			if(j == q.length-1&&q[j].type=='radio') {nums[0]++;}
		}
		if(q[0].type=='checkbox'&&((document.forms['quiz'].elements['question_'+i+'_min']&&n<document.forms['quiz'].elements['question_'+i+'_min'].value)||(document.forms['quiz'].elements['question_'+i+'_max']&&n>document.forms['quiz'].elements['question_'+i+'_max'].value))) nums[0]++;
	}
	var j = new Array('0');
	for (i in nums) if(nums[i]>nums[j[0]]){j=new Array(''+i);} else if(nums[i]==nums[j[0]])j[j.length] = i;
	//var o = '';for(var i in results)o+=results[i]+'='+nums[i]+'\n';
	//alert(o);
	if(nums[0]!=0) {
		alert('You missed or incorrectly answered '+nums[0]+' questions!');
	}
	else if(j[0]==0) {
		alert('No result could be determined.');
	}
	else {
		location = results[j[0]]+ext;
	}
}
</script>
<!-- end quiz script --> 

</head>

<body>
<!-- begin quiz html -->
<p>
	<img alt="" src="http://i866.photobucket.com/albums/ab221/jse363840876/hdr_bg.jpg" style="width: 99%; height: 127px;" /></p>
<table border="0" cellpadding="1" cellspacing="1" style="width: 1074px; height: 461px;">
	<tbody>
		<tr>
			<td>
			
				<form action="javascript:finish();" method="post" name="quiz">
					<fieldset style="width:300px;">
						<legend><strong>Username</strong></legend>
						<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
						<p>
							Username:</p></font>
						<input name="name" maxlength="40" size="25" type="text" /></fieldset>
					<br /><br />
					<fieldset style="width:300px;">
<legend><strong>Which game mode do you like to play most?</strong> </legend>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_1" type="radio" value="2"> Capture the Flag</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_1" type="radio" value="1"> Domination</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_1" type="radio" value="3"> Kill Confirmed</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_1" type="radio" value="3"> Demolition</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
</fieldset>
					<br /></td> <td>
					<fieldset style="width:300px;">
<legend><strong>Desired Gamebattles per Month?</strong> </legend>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_2" type="radio" value="3"> 0-5</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_2" type="radio" value="2"> 5-7</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_2" type="radio" value="1"> 7+</font><br />
</fieldset>
					<br />
					<fieldset style="width:300px;">
<legend><strong>Which description below suits you best?</strong> </legend>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_3" type="radio" value="1"> Competitive and Active</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_3" type="radio" value="2"> Active and Social</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_3" type="radio" value="3"> Casual and Social</font><br />
</fieldset><br /><input type="submit" value="Click here to find your Squad and Graduate" method="get" /> <input type="reset" value="Reset" />
			
				<td><fieldset style="width:300px;">
<legend><strong>Do you prefer to play Core or Hardcore?</strong> </legend>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_4" type="radio" value="1,2"> Core</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_4" type="radio" value="3"> Hardcore</font><br /><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="question_4" type="radio" value="3"> Both</font><br />
</fieldset>
				<br />
				<fieldset style="width:300px;">
<legend><strong>How have you found your time with us?</strong> </legend>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<textarea cols="45" rows="6" name="overlord">How did you enjoy it? Let us know!</textarea>
</fieldset>
					<br />
				</form>
				</td>

		</tr>
	</tbody>
</table>
<p>
	&nbsp;</p>
<p>
	&nbsp;</p>
<p>
	&nbsp;</p>


<!-- end quiz html -->          </body>

Email script - could be posted on the output page but how do I get the values to it?

// $to = "a@b.com"; $subject = "Transfer request form"; $username = $_POST['name'] ; $q1 = $_POST['question_1'] ; $q2 = $_POST['question_2'] ; $q3 = $_POST['question_3'] ; $q4 = $_POST['question_4'] ; $q5 = $_POST['overlord'] ; $header = "Transfer requested by $username"; 

 // $body = $header.'
	
//	Username:  '.$username.'
//	 Transfer to: this is where I want the team name
 
//Favourite Game Mode (1=Domination, 2=CTF 3=Demolition: '.$q1.'
//Desired number of GB per month (1=0-5 2=5-7 3=7+: '.$q2.'
//Type of Player (1=Competitive 2=Friendly 3=Casual): '.$q3.'
//Core/Hardcore (1,2=Core 3=Hardcore/Both): '.$q4.'
	
//	 Comments: 
//	 '.$q5.'
	
//	|---------END MESSAGE----------|'; 

//	$sent = mail($to, $subject, $headers, $body) ;

Is there a mySQL query that will help find "best match"?

Edited by JayJ: n/a

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by JayJ
0

I appreciate that having re-read my above post that it is unclear what specific help I am requesting.

Clearly trying to use javascript to make a decision as to the best team and then redirect to a page named either team1.php, team2.php, team3.php dependant on the result won't help me pass variables to be sent by email.

What I want to know is whether there is a way to query the 'best match' based on the 3 inputs from a database, for the variables in the form to be submitted to an email as well as the result of best team for the user, and the user be redirected to the team page best for them i.e. <result goes here>.php

1

The flow I'd use would be:
1. Let Javascript validate if everything has been answered.
2. Post the form to a PHP page.
3. Determine the best match (with a query).
4. Send the e-mail.
5. Redirect to the selected team page.

Votes + Comments
Very helpful with a concise post - thankyou
0

Thanks for your help. With a bit of playing around and attempts to replan this I have come to the same conclusion as you above and your post helps reaffirm what I'm trying to do. Thankyou!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.