Javascript & PHP Form/Decision Tree

I’m not really a php person but I tried to do my best before running for help :slight_smile: I’m trying to make a “personality quiz” like function or decision tree. Basically, users answer questions on our form and depending on their answer we recommend a product to them and an email gets sent to us telling us their info if they’d like us to contact them. The javascript portion (calculating which product is best) works well. I just finished formatting the php part and am stuck. When I hit submit on my form it will redirect to the php and then display a blank page. I tried echoing variables but to no avail. I’m not even sure if what I’m trying to do can be done this way.

Here’s my code, I’m shortening the form part for space reasons.

[code]

Where to Start
Name:
Address:
Email:
Phone:
Would you like us to contact you to assist and provide product recommendations?
Yes No
My experience level is:
Brand New
Less than 2 years
2 - 5 years
5 - 10 years
10+ years

Do you currently own biofeedback equipment?:



Yes

Type:


<input TYPE="radio" id="5" NAME="b" VALUE="no">
No<br>
<br>

Do you currently own neurofeedback equipment?:



Yes

Type:







[/code]

[php]<?php
if(isset($_POST[‘submit’])) {

$to = “removed…”;
$toa = “removed…”;
$tob = “removed…”;
$subject = “1”;
$subject2 = “2”;
$subject3 = “3”;
$name_field = $_POST[‘name’];
$email_field = $_POST[‘email’];
$address = $_POST[‘address’];
$phone = $_POST[‘phone’];
$contact = $_POST[‘contact’];
$experience = $_POST[‘a’];
$bioOwn = $_POST[‘b’];
$bioType = $_POST[‘bioType’];
$neuroOwn = $_POST[‘c’];
$neuroType = $_POST[‘neuroType’];
$goal = $_POST[‘d’];
$business = $_POST[‘e’];
$licensedSpecialty = $_POST[‘licensedPractitioner’];
$unlicensedSpecialty = $_POST[‘unlicensedPractitioner’];
$existingBusiness = $_POST[‘f’];
$customerBase = $_POST[‘g’];
$clientDistance = $_POST[‘h’];
$wantToKnow = $_POST[‘i’];
$techSmarts = $_POST[‘j’];
$feedbackType = $_POST[‘k’];
$bioPeriph = $_POST[‘l’];
$bioOther = $_POST[‘bioOther’];
$neuroPeriph = $_POST[‘m’];
$neuroOther = $_POST[‘neuroOther’];
$amountOfSystems = $_POST[‘n’];
$percentBusiness = $_POST[‘o’];
$consideringBio = $_POST[‘p’];
$percentIncome = $_POST[‘q’];
$budget = $_POST[‘r’];
$referralType = $_POST[‘s’];
$headers = “From: $email_field”;
$result = $_POST [“total”]; //Not sure if this is correct…

echo $result;
echo $address;
switch($result) {
case “total” <222:
$body = “From: $name_field\n E-Mail: $email_field\n Address: $address\n City: $city\n State: $state\n PostalCode: $zip\n Department $selected\n How did you about us: $hear\n Country: $country\n Comments: $comment”;
mail($to,$subject,$body, $headers);
header(“Location: http://www.mysite1.com/page/thank-you/”);
exit;
case “total” <325:
$body = “From: $name_field\n E-Mail: $email_field\n Address: $address\n City: $city\n State: $state\n PostalCode: $zip\n Department $selected\n How did you about us: $hear\n Country: $country\n Serial Number: $serial\n Comments: $comment”;
mail($toa, $subject2, $body, $headers);
header(“Location: http://www.mysite2.com/page/thank-you/”);
exit;

        die("<Please click the back button to enter a valid serial number!");
    
default:
    die("Invalid selection.");
   }

}

?>[/php]

I’m aware that some fields in the email part don’t make sense. I was trying to get it to work or display SOMETHING before I finish editing out what I want the email body to say, etc.

Can anyone please tell me what I’m doing wrong? Thank you!!

For starters you should check your php.ini to see if display_errors = On. If not the errors may be going to a log file. A blank page is typically the problem when there’s a syntax error and no error reporting.

You could try adding to the top of your script:

[php]
ini_set(‘display_errors’, ‘1’);
error_reporting(E_ALL);
[/php]

Aside from that, I see a problem with your switch. You are comparing a string ‘total’ to an integer. You are also missing your breaks after each case. Here is an example of how to use the switch properly

[php]
$result = (int) $_POST[‘total’];
switch($result) {
case $result < 222:
// Do something
echo “Case 1”;
break; // Break case for < 222
case $result >= 222 && $result < 325:
// Do something else
echo “Case 2”;
break; // Break case
default:
// Default
echo “Default case”;
break; // Break case
}
[/php]

Thank you for your fast reply!

We have no php.ini that I can find. (The website I inherited is set up like a disaster.)

I added both of your codes/changed my switch and I still get the blank page.

Create a new file, call it phpinfo.php

[php]<?php phpinfo(); ?>[/php]

Paste that inside the file and run it. If this works it will confirm that PHP is working and also give you the path to php.ini

That worked, but it’s giving me a location that does not exist in my ftp. I think it’s somewhere in my hosts and I cannot touch that.

I DID notice however that error reporting is set to 1

And you are using the .php extension for your code above?

At the top of your script, put this and see if it gives you a blank page still.

[php]print_r($_POST);[/php]

Yes, I am

Looking at your javascript, aren’t you preventing the form from posting with this redirect?

if (total<222){ window.location="http://www.mysite1.com/" }
else if (total<325){ window.location="http://www.mysite2.com/" }
else if (total<550){ window.location="http://www.mysite3.com/" }
else { window.location="http://www.mysite5.com/" }

Same problem even if I comment it out or remove the javascript completely.

Oh, I just realized you are looking for isset($_POST[‘submit’]) without specifying that input name.

Add name=“submit” to your button.

 <input name="submit" TYPE="submit" VALUE="View Results">

Yay now it works!

Only one problem now: It will only redirect to the 2nd case, no matter how many “points” there are. Any ideas?

Post your current code. I assume you fixed your switch problems?

[code]<?php
if(isset($_POST[‘submit’])) {
ini_set(‘display_errors’, ‘1’);
error_reporting(E_ALL);

$to = “removed…”;
$toa = “removed…”;
$tob = “removed…”;
$subject = “Atlantis 2x2”;
$subject2 = “Atlantis 4x4”;
$subject3 = “Discovery”;
$name_field = $_POST[‘name’];
$email_field = $_POST[‘email’];
$address = $_POST[‘address’];
$phone = $_POST[‘phone’];
$contact = $_POST[‘contact’];
$experience = $_POST[‘a’];
$bioOwn = $_POST[‘b’];
$bioType = $_POST[‘bioType’];
$neuroOwn = $_POST[‘c’];
$neuroType = $_POST[‘neuroType’];
$goal = $_POST[‘d’];
$business = $_POST[‘e’];
$licensedSpecialty = $_POST[‘licensedPractitioner’];
$unlicensedSpecialty = $_POST[‘unlicensedPractitioner’];
$existingBusiness = $_POST[‘f’];
$customerBase = $_POST[‘g’];
$clientDistance = $_POST[‘h’];
$wantToKnow = $_POST[‘i’];
$techSmarts = $_POST[‘j’];
$feedbackType = $_POST[‘k’];
$bioPeriph = $_POST[‘l’];
$bioOther = $_POST[‘bioOther’];
$neuroPeriph = $_POST[‘m’];
$neuroOther = $_POST[‘neuroOther’];
$amountOfSystems = $_POST[‘n’];
$percentBusiness = $_POST[‘o’];
$consideringBio = $_POST[‘p’];
$percentIncome = $_POST[‘q’];
$budget = $_POST[‘r’];
$referralType = $_POST[‘s’];
$headers = “From: $email_field”;

$result = (int) $_POST[‘total’];
switch($result) {
case $result < 222:
$body = “From: $name_field\n E-Mail: $email_field\n Address: $address\n City: $city\n State: $state\n PostalCode: $zip\n Department $selected\n How did you about us: $hear\n Country: $country\n Comments: $comment”;
mail($to,$subject,$body, $headers);
header(“Location: http://www.mysite1.com/page/thank-you/”);
echo “Case 1”;
break; // Break case for < 222
case $result >= 222 && $result < 325:
$body = “From: $name_field\n E-Mail: $email_field\n Address: $address\n City: $city\n State: $state\n PostalCode: $zip\n Department $selected\n How did you about us: $hear\n Country: $country\n Serial Number: $serial\n Comments: $comment”;
mail($toa, $subject2, $body, $headers);
header(“Location: http://www.mysite1.com/page/thank-you/”);
echo “Case 2”;
break; // Break case
default:
// Default
echo “Default case”;
break; // Break case
}

/*switch($result) {
case “total” <222:
$body = “From: $name_field\n E-Mail: $email_field\n Address: $address\n City: $city\n State: $state\n PostalCode: $zip\n Department $selected\n How did you about us: $hear\n Country: $country\n Comments: $comment”;
mail($to,$subject,$body, $headers);
header(“Location: http://www.site1.com/page/thank-you/”);
exit;
case “total” <325:
$body = “From: $name_field\n E-Mail: $email_field\n Address: $address\n City: $city\n State: $state\n PostalCode: $zip\n Department $selected\n How did you about us: $hear\n Country: $country\n Serial Number: $serial\n Comments: $comment”;
mail($toa, $subject2, $body, $headers);
header(“Location: http://www.mysite1.com/page/thank-you/”);
exit;

        die("<Please click the back button to enter a valid serial number!");
    
default:
    die("Invalid selection.");
   }*/

}

?>[/code]

And how are you sending $_POST[‘total’] through your form? The problem is likely with that value.

You can test the different cases manually just define the $result for example:

[php]
$result = 200; // (int) $_POST[‘total’];
[/php]

After I posted I realized that was probably the problem. I’m not sending it through my form, it’s being calculated from the javascript and I suppose I was hoping it would pull the variable from the previous page…wishful thinking I suppose!

Testing my own cases works, but I really need it to somehow read the javascript variables.

You could have it as a hidden input and use javascript to set the value before submitting the form.

I changed the domains to a generic example as requested.

Okay here is what I tried.

[code][/code]

And then I changed result to [php]$result = (int) $_GET[‘total’];[/php]

Not working. What am I doing wrong?

P.s. Thank you so much for your help btw,

You can’t submit a form using window.location. My suggestion was to use a hidden input on the form. Then you would use javascript to set the value and submit the form.

Example:

<script type="text/javascript">
function submitForm() {
	var form = document.forms['myForm'];
	form.elements['total'].value = 200;
	form.submit();
}
</script>
<form name="myForm" method="POST" action="script.php" onsubmit="submitForm(); return false;">
	<input type="hidden" name="total" />
	<input type="submit" name="submit" value="Submit" />
</form>

Okay I added the function. It works great if I manually put in a value for total but once I try to get it to read the calculated “total” it will redirect always to case 2. Any help?

Sponsor our Newsletter | Privacy Policy | Terms of Service