www.webdeveloper.com
Results 1 to 9 of 9

Thread: automated assessment form?

  1. #1
    Join Date
    Jun 2011
    Posts
    5

    automated assessment form?

    Hey all, long time browser - first time poster!

    I'm after a script, service or even a method where the user would be presented with a recommended answer after making their selections from 2 to 4 drop down boxes.

    Might be best if I make an example.. The form is going to let the user know what the preferred ISP they should use based on their answers.. Questions would be:

    1. Please select your region
    - region 1
    - region 2
    - region 3
    2. What services do you require
    - service 1
    - service 2
    - service 3

    And etc.. The user would then be told who the preferred ISP(s) would be (from about 6 ISPs) and the preferred ISP(s) would then be emailed the form info.

    Any help would be greatly appreciated. Even if someone could point me in the right direction of what code I need to investigate to do this. Cheers!

  2. #2
    Join Date
    Jun 2011
    Posts
    5
    After some research I've discovered the best method for this is to use a multidimensional array with a 'while loop'. However I'm unsure how to make the array choose the best answer and display it.

    I'm going to make the form easier by making a form which acts like a normal form (emails to one person at the end of it) but redirects the user to a "thank you" style page where it would then suggest who their preferred ISP is..

    So I guess I would have a bunch of arrays within an array which the data from the form would then be matched with and then display the suitable ISP?

    Would anyone have any example code of how this can be done?!

  3. #3
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    I would store the data in a database. Then you could select from the database where the conditions picked in the form are met. IE:

    SELECT provider FROM isps WHERE state="ca" AND service_type="cable" AND area="Los Angelos"

    etc. etc.
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  4. #4
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125

    Talking Simple Way

    Simple way i would do it is with a bunch of If statements, may not be very clean but still easy to follow and change.

    PHP Code:
    <?php
    //1. Please select your region
    //- region 1
    //- region 2
    //- region 3
    //2. What services do you require
    //- service 1
    //- service 2
    //- service 3

    if (isset($_POST['get_isp'])) { 

    if (
    $_POST['region'] == "region 1")

    if(
    $_POST['service'] == "service 1"){ $Best_ISP "ISP Name #1"; }
    if(
    $_POST['service'] == "service 2"){ $Best_ISP "ISP Name #2"; }
    if(
    $_POST['service'] == "service 3"){ $Best_ISP "ISP Name #3"; }
    }

    if (
    $_POST['region'] == "region 2")

    if(
    $_POST['service'] == "service 1"){ $Best_ISP "ISP Name #4"; }
    if(
    $_POST['service'] == "service 2"){ $Best_ISP "ISP Name #5"; }
    if(
    $_POST['service'] == "service 3"){ $Best_ISP "ISP Name #6"; }
    }

    if (
    $_POST['region'] == "region 3")

    if(
    $_POST['service'] == "service 1"){ $Best_ISP "ISP Name #7"; }
    if(
    $_POST['service'] == "service 2"){ $Best_ISP "ISP Name #8"; }
    if(
    $_POST['service'] == "service 3"){ $Best_ISP "ISP Name #9"; }
    }

    if (
    $_POST['region'] == "")

    $Best_ISP "Did not Choose"//What ever error message you want i suppose
    }

    }
    ?>
    than you would just use a html setup like this

    HTML Code:
    <?php echo $Best_ISP; ?>
    
    <form method="post">
    <select name="region">
    <option value="region 1">Region 1</option>
    <option value="region 2">Region 2</option>
    <option value="region 3">Region 3</option>
    </select><br />
    
    <select name="service">
    <option value="service 1">Service 1</option>
    <option value="service 2">Service 2</option>
    <option value="service 3">Service 3</option>
    </select><br />
    
    <input type="submit" name="get_isp" value="Create Ticket">
    </form>
    that is one of the easiest ways, another is to create an algorithm although its not really needed with only a few choices.

    i would also have to say that you don't need a database for this, when its this simple. Pulling from a database will only bog it down.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,914
    A database solution, however, scales much better. When you only have 3 ISP's in 3 regions providing 3 services, it's not too big a deal to write some pure PHP code to handle it, (though even that starts getting hairy if you consider different ISP's can provide different combinations of services and might serve different regions. Also, if two ISP's rank equally, do you want to randomly choose the order they are displayed? Now, what happens as your site grows and you want to support 50 ISP's in 10 regions with 6 possible services per ISP -- and maybe some ISP's support multiple regions?

    You might have 5 tables, 1 each to define ISP's, services, and regions, plus 2 tables to establish relationships between ISP's and the regions and services each supports:
    Code:
    isp
    =========
    id (int, auto-increment, PK)
    name
    [other data...]
    
    region
    =========
    id
    name
    [other data...]
    
    service
    ==========
    id
    name
    [other data...]
    
    isp_to_region
    =============
    isp_id
    region_id
    
    isp_to_service
    ==============
    isp_id
    service_id
    Suppose from your form you have selected a region ID as $_POST['region'] and an array of service ID's as $_POST['service] (use the square bracket naming convention in the form):
    PHP Code:
    $region = (int) $_POST['region'];
    $service = array();
    foreach(
    $_POST['service'] as $serv) {
       
    $service[] = (int) $serv;
    }
    $sql "
    SELECT * FROM isp
    INNER JOIN isp_to_region ON isp_to_region.isp_id = isp.id
    "
    ;
    foreach(
    $service as $ix => $serv) {
       
    $sql .= "INNER JOIN isp_to_service AS i2s$ix ON i2s$ix.isp_id = isp.id\n";
    }
    $sql .= "ORDER BY RAND";
    $result mysql_query($sql);
    // ...etc.... 
    Adding a new criteria just means adding a couple DB tables and a couple lines of code to handle it in the SQL. Also, the form selection elements can be generated dynamically from the DB, simplifying that code and making changes as simple as adding/deleting/editing records in the relevant DB table.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jun 2011
    Posts
    5
    Thanks heaps guys! I'll have to get you guys a beer when I see ya next.

    I decided to go with the non-database method as this is going to stay quite small and simple. But might have to go to database method if this gets any bigger..

    Anyway here's my code for the form:

    Code:
    <body>
    <table width="600" height="500" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="top" align="center">
        
    <form name="form" method="post" action="thankyou.php">
    				<table cellspacing='0' cellpadding='2' width='600px' > 
    					<tr> 
    						<td class='form_field_name' valign='top'>Your Name * </td> 
    						<td valign='top'><input type='text' name='name' value='' maxlength='99' /></td> 
    					</tr> 
    					<tr> 
    						<td class='form_field_name' valign='top'>Phone * </td> 
    						<td valign='top'><input type='text' name='phone' value='' maxlength='' /></td> 
    					</tr> 
    					<tr> 
    						<td class='form_field_name' valign='top'>Email Address * </td> 
    						<td valign='top'><input type='text' name='email' value='' maxlength='99' /></td> 
    					</tr> 
    					<tr> 
    						<td class='form_field_name' valign='top'>Address * </td> 
    						<td valign='top'><textarea name='address' rows='4' cols='40'></textarea></td> 
    					</tr>  
    					<tr> 
    						<td class='form_field_name' valign='top'>Region * </td> 
    						<td valign='top'><select name='region' > 
    <option value=''>- Please select one -</option> 
    <option value='0'>Hawke's Bay</option> 
    <option value='1'>Taupo</option> 
    <option value='2'>Rotorua </option> 
    </select> 
    </td> 
    					</tr> 
    					<tr> 
    						<td class='form_field_name' valign='top'>Additional Services Req</td> 
    						<td valign='top'><p>
    						  <label>
    						    <input type="checkbox" name="wantphone" />
    						    Phone</label>
    						  <br />
    						  <label>
    						    <input type="checkbox" name="wanthosting" />
    						    Hosting</label>
    						  <br />
    						  </p>  
    </td> 
    					</tr>  
    				<tr> 
    					<td><input type="hidden" name="doit" value="doit" /></td> 
    					<td><input type="button" value="Submit" onclick="verify();"></td> 
    				</tr></table> 
                    
    </form></td>
      </tr>
    </table>
    
    </body>
    Then here's the code for thankyou.php:

    Code:
    <body>
    <h1>Thanks</h1>
    <?php 
    
    
    //print_r($_REQUEST);
    
    if(isset($_REQUEST["doit"])) {
    
    
    $regionnames = array('Hawkes Bay', 'Taupo', 'Rotorua');
    
    // Parse passed form data
    $name = $_REQUEST['name'] ;
    $phone = $_REQUEST['phone'] ;
    $email = $_REQUEST['email'] ;
    $address = $_REQUEST['address'] ;
    $region = $_REQUEST['region'] ;
    if(isset($_REQUEST['wantphone'])) {
    	$wantphone =  true;
    } else {
    	$wantphone = false;	
    }
    if(isset($_REQUEST['wanthosting'])) {
    	$wanthosting = true;
    } else {
    	$wanthosting = false;	
    }
    
    // ISP Multidimentional Array
    $isps['airnet']['Fullname'] = 'Airnet NZ Ltd';
    $isps['airnet']['Website'] = 'http://www.airnet.co.nz';
    $isps['airnet']['Regions'] = array('0');
    $isps['airnet']['Phone'] = true;
    $isps['airnet']['Hosting'] = true;
    
    $isps['woosh']['Fullname'] = 'Woosh NZ Ltd';
    $isps['woosh']['Website'] = 'http://www.woosh.co.nz';
    $isps['woosh']['Regions'] = array('0','1');
    $isps['woosh']['Phone'] = true;
    $isps['woosh']['Hosting'] = false;
    
    $ispstocontact = '';
    $ispcount = 0;
    
    
    foreach ($isps as $key => $info) {
    	$fullname = $info['Fullname'];
    	$website = $info['Website'];
    	$regions = $info['Regions'];
    	$doesphone = $info['Phone'];
    	$doeshosting = $info['Hosting'];
    
    	// Check if the region the user is in is within the regions the ISP covers
    	if(in_array($region, $regions)) {
    		// ISP is in this region...
    		
    		// If the user want's phone services and the ISP does them, or if the user doesn't want phone services...
    		if($wantphone == $doesphone || !$wantphone) {
    			
    			// If the user want's hosting services and the ISP does them, or if the user doesn't want hosting services...
    			if($wanthosting == $doeshosting || !$wanthosting) {
    				
    				// Passed all criteria we're testing, valid prefered ISP.
    				print "You can signup with ".$fullname." at ".$website.".<br />";
    				$ispstocontact .= $fullname.", ";
    				$ispcount++;
    				
    			} else {
    				// User want's hosting but the ISP doesn't do it...
    			}
    			
    		} else {
    			// User want's phone but the ISP doesn't do it...
    		}
    	} else {
    		// ISP does not cover this region...
    	}
    }
    
    
    if($ispcount == 0) {
    	print "<p>Sorry we found no ISP matching your criteria. Please go back and try again.<br /></p>";	
    } else {
    	print "<p>Alternatively, the below ".$ispcount." ISP(s) are also available to you.</p>";		
    	?>
        
        <?
    }
    
    $message = "
    Name: $name\n
    Phone: $phone\n
    Email: $email\n
    Address: $address\n
    \n	 
    Region: ".$regionnames[$region]."\n
    Additional Services: ";
    	 
    if($wantphone) {
    	$message .= "Phone";
    }
    if($wantphone && $wanthosting) {
    	$message .= ", ";
    }
    if($wanthosting) {
    	$message .= "Hosting";	
    }
    
    $message .= "\n\nPrefferred ISP: $ispstocontact";
    
    	 
      @mail( "email address", "Form results",
        $message, "From: $name <$email>" );
    	
    } else {
    	print "It appears you have found this page in error. Please go back and try again.";	
    }
    ?> 
    
    </body>
    It's pretty basic but it's doing what I want it to for the moment. Might have to change it up a bit for other functionality. But yeah that's it.

    Are there any recommendations or alterations to the above that anyone could pin? Cheers!

  7. #7
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    124
    i would go ahead and setup the database... you can keep client info and stuff like that and always refer back to it out of the database. as well as keep track of everything else you might want to like billing and other information all in one place.

    One thing i have noticed in my life is people like it when you can "remember" something about them like what they ordered 6 months ago. Plus if you want to mass email all the clients you can without having to use a mail client...

    Other than that the script looks pretty good at a glance..
    Samantha Morrison
    Knock-Out Web Designs
    http://kowebdesigns.com
    Website Developer/Designer

  8. #8
    Join Date
    Jun 2011
    Posts
    5
    Thanks ebar! I wish I had started coding all this with databases in mind as this has grown pretty big. There are 9 ISPs with 10 services each.

    Unfortunately I don't know how to make the kind of tables I'd need for this and don't know enough about PHP to link to it! Anyway new code following..

    See attached txt file.

    Pretty massive as you can see. I have the form working but I can't make it display the services or pick the best ISP from the selected services. Any help?
    Attached Files Attached Files

  9. #9
    Join Date
    Jun 2011
    Posts
    5
    After much messing around with the coding - trial and error style.. I've got this mostly working like how I intend it to. Even have it posting to a database afterwards!

    Although there's still a glitch or two where the 'region' database entries are saving as "ArrayTaupo" instead of just "Taupo". Also, can't get the list of ISPs to save properly into the database either.. Could someone take a look at let me know what I'm doing wrong?!

    Code:
    <?php 
    
    
    //print_r($_REQUEST);
    
    if(isset($_REQUEST["doit"])) {
    
    
    $regionnames = array('Hawkes Bay', 'Taupo', 'Rotorua');
    
    // Parse passed form data
    $name = $_REQUEST['name'] ;
    $phone = $_REQUEST['phone'] ;
    $email = $_REQUEST['email'] ;
    $address = $_REQUEST['address'] ;
    $region = $_REQUEST['region'] ;
    if(isset($_REQUEST['wantinternet'])) {
    	$wantinternet =  true;
    } else {
    	$wantinternet = false;	
    }
    if(isset($_REQUEST['wantwan'])) {
    	$wantwan = true;
    } else {
    	$wantwan = false;	
    }
    if(isset($_REQUEST['wantphone'])) {
    	$wantphone = true;
    } else {
    	$wantphone = false;	
    }
    if(isset($_REQUEST['wantcloud'])) {
    	$wantcloud = true;
    } else {
    	$wantcloud = false;	
    }
    if(isset($_REQUEST['wantother'])) {
    	$wantother = true;
    } else {
    	$wantother = false;	
    }
    // ISP Multidimentional Array
    $isps['fxnetworks']['Fullname'] = 'FX Networks';
    $isps['fxnetworks']['Website'] = 'http://www.fx.net.nz/';
    $isps['fxnetworks']['Regions'] = array('0','1','2');
    $isps['fxnetworks']['Highspeed Internet'] = true;
    $isps['fxnetworks']['WAN'] = true;
    $isps['fxnetworks']['Phone/Voice Services'] = true;
    $isps['fxnetworks']['Cloud Services'] = false;
    $isps['fxnetworks']['Other'] = false;
    
    $isps['airnet']['Fullname'] = 'Airnet NZ Ltd';
    $isps['airnet']['Website'] = 'http://www.airnet.co.nz/';
    $isps['airnet']['Regions'] = array('0');
    $isps['airnet']['Highspeed Internet'] = true;
    $isps['airnet']['WAN'] = true;
    $isps['airnet']['Phone/Voice Services'] = false;
    $isps['airnet']['Cloud Services'] = false;
    $isps['airnet']['Other'] = true;
    
    $isps['solarix']['Fullname'] = 'Solarix Ltd';
    $isps['solarix']['Website'] = 'http://www.solarix.co.nz/';
    $isps['solarix']['Regions'] = array('0');
    $isps['solarix']['Highspeed Internet'] = true;
    $isps['solarix']['WAN'] = true;
    $isps['solarix']['Phone/Voice Services'] = true;
    $isps['solarix']['Cloud Services'] = true;
    $isps['solarix']['Other'] = false;
    
    $isps['itco']['Fullname'] = 'ITCO Ltd';
    $isps['itco']['Website'] = 'http://www.itco.co.nz/';
    $isps['itco']['Regions'] = array('1','2');
    $isps['itco']['Highspeed Internet'] = true;
    $isps['itco']['WAN'] = true;
    $isps['itco']['Phone/Voice Services'] = true;
    $isps['itco']['Cloud Services'] = true;
    $isps['itco']['Other'] = false;
    
    $isps['wasp']['Fullname'] = 'WASP';
    $isps['wasp']['Website'] = 'http://www.waspnet.co.nz/';
    $isps['wasp']['Regions'] = array('0');
    $isps['wasp']['Highspeed Internet'] = true;
    $isps['wasp']['WAN'] = true;
    $isps['wasp']['Phone/Voice Services'] = false;
    $isps['wasp']['Cloud Services'] = true;
    $isps['wasp']['Other'] = true;
    
    $isps['safenz']['Fullname'] = 'SafeNZ Networks Ltd';
    $isps['safenz']['Website'] = 'http://www.safenz.net/';
    $isps['safenz']['Regions'] = array('0');
    $isps['safenz']['Highspeed Internet'] = true;
    $isps['safenz']['WAN'] = false;
    $isps['safenz']['Phone/Voice Services'] = true;
    $isps['safenz']['Cloud Services'] = true;
    $isps['safenz']['Other'] = true;
    
    $isps['datacom']['Fullname'] = 'Datacom Group Ltd';
    $isps['datacom']['Website'] = 'http://www.datacom.co.nz/';
    $isps['datacom']['Regions'] = array('0');
    $isps['datacom']['Highspeed Internet'] = true;
    $isps['datacom']['WAN'] = true;
    $isps['datacom']['Phone/Voice Services'] = true;
    $isps['datacom']['Cloud Services'] = true;
    $isps['datacom']['Other'] = false;
    
    $isps['modica']['Fullname'] = 'Modica Group Ltd';
    $isps['modica']['Website'] = 'http://www.modicagroup.com/';
    $isps['modica']['Regions'] = array('0','2');
    $isps['modica']['Highspeed Internet'] = true;
    $isps['modica']['WAN'] = true;
    $isps['modica']['Phone/Voice Services'] = false;
    $isps['modica']['Cloud Services'] = true;
    $isps['modica']['Other'] = true;
    
    $isps['vip']['Fullname'] = 'VIP';
    $isps['vip']['Website'] = 'none';
    $isps['vip']['Regions'] = array('0','2');
    $isps['vip']['Highspeed Internet'] = true;
    $isps['vip']['WAN'] = true;
    $isps['vip']['Phone/Voice Services'] = true;
    $isps['vip']['Cloud Services'] = true;
    $isps['vip']['Other'] = true;
    
    $ispstocontact = '';
    $ispcount = 0;
    
    
    foreach ($isps as $key => $info) {
    	$fullname = $info['Fullname'];
    	$website = $info['Website'];
    	$regions = $info['Regions'];	
    	$doesinternet = $info['Highspeed Internet'];
    	$doeswan = $info['WAN'];
    	$doesphone = $info['Phone/Voice Services'];
    	$doescloud = $info['Cloud Services'];
    	$doesother = $info['Other'];
    
    	// Check if the region the user is in is within the regions the ISP covers
    	if(in_array($region, $regions)) {
    		// ISP is in this region...
    		
    		// If the user want's highspeed internet and the ISP does them, or if the user doesn't want highspeed internet...
    		if($wantinternet == $doesinternet || !$wantinternet) {
    			
    			// If the user want's wan metro & national and the ISP does them, or if the user doesn't want wan metro & national...
    			if($wantwan == $doeswan || !$wantwan) {
    			
    				// If the user want's phone/voice services and the ISP does them, or if the user doesn't want phone/voice services...
    				if($wantphone == $doesphone || !$wantphone) {
    			
    					// If the user want's cloud services and the ISP does them, or if the user doesn't want cloud services...
    					if($wantcloud == $doescloud || !$wantcloud) {
    			
    						// If the user want's other services and the ISP does them, or if the user doesn't want other services...
    						if($wantother == $doesother || !$wantother) {
    				
    				
    				// Passed all criteria we're testing, valid prefered ISP.
    				print "<p>You can signup with ".$fullname." at ".$website.".<br /></p>";
    				$ispstocontact .= $fullname.", ";
    				$ispcount++;
    				
    											
    						} else {
    							// User want's other services but the ISP doesn't do it...
    						}
    					} else {
    						// User want's cloud services but the ISP doesn't do it...
    					}
    				} else {
    					// User want's phone services but the ISP doesn't do it...
    				}
    			} else {
    				// User want's wan but the ISP doesn't do it...
    			}			
    		} else {
    			// User want's highspeed internet but the ISP doesn't do it...
    		}
    	} else {
    		// ISP does not cover this region...
    	}
    }
    
    
    if($ispcount == 0) {
    	print "<p>Sorry we found no ISP matching your criteria. Please go back and try again.<br /></p>";	
    } else {
    	print "<p>Alternatively, the below ".$ispcount." ISP(s) are also available to you.</p>";		
    	?>
        
        <?
    }
    
    $message = "
    Name: $name\n
    Phone: $phone\n
    Email: $email\n
    Address: $address\n
    \n	 
    Region: ".$regionnames[$region]."\n
    Additional Services: ";
    	 
    if($wantinternet) {
    	$message .= "High Speed Internet, ";
    }
    if($wantwan) {
    	$message .= "WAN, ";	
    }
    if($wantphone) {
    	$message .= "Phone/Voice Services, ";	
    }
    if($wantcloud) {
    	$message .= "Cloud Services, ";	
    }
    if($wantother) {
    	$message .= "Other Services, ";	
    }
    
    $message .= "\n\nPrefferred ISP: $ispstocontact";
    
    	 
      @mail( "f1ateric@hotmail.com", "Get On It form results",
        $message, "From: $name <$email>" );
    	
    } else {
    	print "It appears you have found this page in error. Please go back and try again.";	
    }
    
    // make database connection
    $con = mysql_connect ("database-address","username","password"); 
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db ("database", $con);
    
    $sql="INSERT INTO FormDatabase (Name,Phone,Email,Address,Region,wantinternet,wantwan,wantphone,wantcloud,wantother,PreferredISP) VALUES ('$_POST[name]','$_POST[phone]','$_POST[email]','$_POST[address]','$_POST.$regionnames[$region].','$_POST[wantinternet]','$_POST[wantwan]','$_POST[wantphone]','$_POST[wantcloud]','$_POST[wantother]','$_POST$ispstocontact')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    //echo "<p>1 record added</p>";
    
    mysql_close($con)
    
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles