I am using a script that counts clicks on various links in the home page. The site is for the community channel of a cable TV company and the user can purchase ads or post an obituary or a weather related delay or calcellation.

We want to add a count so the siteowner can see how many clicks on the paid ads links actually result in an ad being submitted for posting. When the user clicks on the button "Place Ad", an email is sent to the community channel. The following code was added to email.php

echo "The ad selected is $ad";  
//Yes the name of the ad is printed to screen 
    
    switch($ad){
        case "One Day General or Business Message":
            $id=2;
            break;
        case "One Month Nonprofit Message":
            $id=3;
            break;
        default:
            echo "The ID is not getting thru.";
            break;
}
completed($id);

The function called is as follows:

function completed($id){

// First check if the ID is set and if it is valid (contains only numbers)
$id = $_POST['id'];  echo "ID in function completed($id) is $id<br />";
             //NO! the ID does not print to screen

if(empty($id) || preg_match("/\D/",$id)) {die("Invalid ID, numbers only allowed!");}
// This scentence does print to screen

// Get lines from file
$lines=file($settings['logfile']);

// Let's find the line that contains our ID
$found=0;
$i=0;
foreach ($lines as $thisline) {
    if (preg_match("/^($id\%\%)/",$thisline)) {
        $thisline=chop($thisline);
        // We found the line, now we get number of ads PLACED from the line
        list($id,$added,$url,$count,$placed,$name)=explode("%%",$thisline);
        // Increase placed by 1 and update this line
        $placed++;
        $lines[$i]=$id."%%".$added."%%".$url."%%".$count."%%".$placed."%%".$name.$newline;
        $found=1;
        break;
    }
    // This line didn't contain ID, lets go to the next one
    $i++;
}
if($found != 1) {die("This ID doesn't exist!");}

// Rewrite the log file with the updated line
$content = implode('', $lines);
$fp = fopen($settings['logfile'],"wb") or die("Can't write to log file! Please Change the file permissions (CHMOD to 666 on UNIX machines!)");
fputs($fp,$content);
fclose($fp);

// Redirect to the link URL
Header("Location: $url");
exit();
}
?>

Does anyone have any idea why the above code doesn't work. Is there something special that must be done to assign a string to an integer value?

Any help will be greatly appreciated

This line in your function is wrong:

$id = $_POST['id'];  echo "ID in function completed($id) is $id<br />";

You are passing the $id through the function call, so no need to reference the $_POST value. Change the first line to:

echo "ID in function completed($id) is $id<br />";

The problem is that I'm NOT passing the ID to the function. I'm not setting the ID. The switch statement always produces the default.

Any suggestions to fix that?

If $ad is being passed through a $_POST submission then it is possible that there are extra hidden characters being returned, and so the switch statements will never match.

Try using trim and strtolower on the $ad variable, and also on the switch match value, so that both values should be the same:

echo "The ad selected is $ad";  
//Yes the name of the ad is printed to screen
    
    switch(trim(strtolower($ad))){
        case "one day general or business message":
            $id=2;
            break;
        case "one month nonprofit message":
            $id=3;
            break;
        default:
            echo "The ID is not getting thru.";
            break;
}
completed($id);
This article has been dead for over six months. Start a new discussion instead.