Ridiculously easy else if() problem

ok guys,

for the life of me I can’t get this. here is my code:

$product_page = $_SERVER[‘HTTP_REFERER’];
$demo_requested = “”;

if($product_page = ‘…\reghh.php’) {
$demo_requested = “Home Health Edition”; }
else if($product_page = ‘…\regsnf.php’) {
$demo_requested = “Skilled Nursing Edition”; }
else {
$demo_requested = “Assisted Living Edition”; }

what I get in my email message, regardless of the page where the form is submitted is: Home Health Edition

I have tried almost everything I can think of as far as modifying the $product_page = ‘…\PAGE.php’ code, but i still get the same damn demo requested in the email message. if I run the following code on the page:

$product_page = $_SERVER[‘HTTP_REFERER’];
echo “< script >alert(’$product_page’);< /script >”;

I get this in the alert window:

http://www.domain.com/reggrp.php, which of course should give me a different result as I’m getting in the email. I’ve tried putting that into the if else() arguments too and I get the same thing…always the same value for **$demo_requested **. can someone spot the error? (for some reason the forum software is adding an extra “.” in those directory pointers. there are only 2 in the script).

The most immediate problem is because one = is an assignment operator and two == is a comparison operator. By using one = in the conditional statements, you are assigning the value on the right side to the variable, then testing the result of the assignment, which is TRUE, so the 1st assignment is what wins.

If you are just mapping input values to output values, don’t use discrete conditional logic at all. This is a waste of typing. Instead, use a ‘mapping’ array, where the array indexes are the input values and the stored array values are the output. You can then add, remove, or change the mapping simply by editing the data in the array. You would use isset() to test if the input value exists in the array. If it does, retrieve the output value from the array, If it doesn’t, use the default value.

hello phdr,

I get confused a lot of times with all the operators and crap in PHP. What I ended up doing was putting a hidden input element on the form page, naming it “currentpage”, putting “<?php echo $currentpage; ?>” in the value attribute, which gets it’s variable value from PHP_SELF, and then writing this code:

$product_page = $_POST[‘currentpage’];

if($product_page = ‘\reghh.php’) {
$demo_requested = “Home Health Edition”; }
else if($product_page = ‘\regsnf.php’) {
$demo_requested = “Skilled Nursing Edition”; }
else {
$demo_requested = “Assisted Living Edition”; }

if($product_page = ‘\reghh.php’) {
$subject = “Demo Request Received - Home Health Edition”; }
elseif($product_page = ‘\regsnf.php’) {
$subject = “Demo Request Received - Skilled Nursing Edition”; }
else {
$subject = “Demo Request Received - Assisted Living Edition”; }

but I still get the same damn thing! what in the world am I doing wrong!?

Apparently not reading the reply is it spells out what’s wrong with the comparisons.

Buried in that wall code are the conditional tests with the wrong comparison in them. The following is an example of using the suggest mapping array -

$mapping = [];
$mapping['reghh.php'] = 'Home Health Edition';
$mapping['regsnf.php'] = 'Skilled Nursing Edition';

$default_mapping = 'Assisted Living Edition'; // define a default value

$product_page = strtolower(basename($_POST['currentpage'] ?? '')); // condition and normalize the input value

echo $demo_requested = $mapping[$product_page] ?? $default_mapping;

I will try what you gave me and get back to you. but what is wrong with what I did? another guy here told me to try basename() but I couldn’t get that to work either. it was returning the same thing as I am getting stuck with.

that works fine. thank you very much. I still am unsure as to what was wrong with mine, but I’m sure I’ll learn it in time. here is what I used, based on what you gave me:

$mappingproductpage = [];
$mappingproductpage[‘reghh.php’] = ‘Home Health Edition’;
$mappingproductpage[‘regsnf.php’] = ‘Skilled Nursing Edition’;

$default_mapping_productpage = ‘Assisted Living Edition’; // define a default value
$product_page = strtolower(basename($_POST[‘currentpage’] ?? ‘’)); // condition and normalize the input value
$demo_requested = $mappingproductpage[$product_page] ?? $default_mapping_productpage;

$mappingsubject = [];
$mappingsubject[‘reghh.php’] = ‘Demo Request Received - Home Health Edition’;
$mappingsubject[‘regsnf.php’] = ‘Demo Request Received - Skilled Nursing Edition’;

$default_mapping_subject = ‘Demo Request Received - Assisted Living Edition’; // define a default value
$subject = strtolower(basename($_POST[‘currentpage’] ?? ‘’)); // condition and normalize the input value
$subject = $mappingsubject[$product_page] ?? $default_mapping_subject;

Sponsor our Newsletter | Privacy Policy | Terms of Service