Jump to content

[SOLVED] PHP Form + Checkbox Issue


gamerzfuse

Recommended Posts

I know this has probably been addressed hundreds of times, but everytime I search I can't seem to find something that works for me.

 

The code I have now is:

<input type="checkbox" name="CheckBox" CHECKED> I Would Like to Receive Future Alerts of Free Trials and Deals

AND

<?php

     
// get posted data into local variables
$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$EmailTo = "[email protected]";
$Subject = "Acai X3 Inquiry";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email']));
$AreaCode = Trim(stripslashes($_POST['AreaCode']));
$Prefix = Trim(stripslashes($_POST['Prefix'])); 
$Suffix = Trim(stripslashes($_POST['Suffix']));  
$Question = Trim(stripslashes($_POST['Question']));
$CheckBox = Trim(stripslashes($_POST['CheckBox'])); 



// validation
$validationOK=true;
if (Trim($FirstName)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "First Name: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone Number: ";
$Body .= $AreaCode;
$Body .= $Prefix;
$Body .= $Suffix;
$Body .= "\n";
$Body .= "Zip Code: ";
$Body .= $Question;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=https://www.securecartcenter.com/NjA5N3w2ODR8Mjc0NzEzfHYy/l7vem4b9/g\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contact3.html\">";
}
?>

 

That's the PHP I'm working with.

All the variables I have work fine and it emails me fine when I test this.

I want to setup the checkbox to choose whether or not the contact details are sent to my email address.

I only want the contact information from those who choose to submit it.

 

If anyone could show/tell me how to make this change, I would be forever in your debt.

 

CraiG

Link to comment
https://forums.phpfreaks.com/topic/155150-solved-php-form-checkbox-issue/
Share on other sites

First change the name of the checkbox so you're not using a reserved word. Something meaningful to the reason behind its existence. For example:

<input type="checkbox" name="chkfuturealerts" checked> I Would Like to Receive Future Alerts of Free Trials and Deals

 

Then amongst all your lines of code you can add this line:

$futurealerts=(isset($_POST['chkfuturealerts']) ? true : false);

 

If the checkbox is ticked $futurealerts will be TRUE if unticked it'll be FALSE.

Not reallyuseless  - it assigns $futurealerts a value of either true or false meaning you don't have to use isset() later in the code.

 

Yes your method would work just fine.

 

Some prefer (including me) prefer/like to assign certain data to variables for ease of use later in the code. $_POST is read-only and if the user had to override this value for some reason resetting $futurealerts would be possible.

The ternary operator there is a bit useless

If isset is true, then true

If isset is false, then false

 

So basically just wrapping the entire thing in

isset( $_POST['chkfuturealerts'] )
{
  #Code to send a mail goes here.
}

would do the trick

 

I'm going to put this in and see if it works, but I feel like I need to define chkfuturealerts first.

Therefore, I have to use some sort of code from the post above, but you said it wasn't necessary.

Will play around with it and see if I can figure out, otherwise will check back.

Thanks everyone, SO helpful!

Sorry for the double post, but I can't find the EDIT button for the life of me.

Update:

 

<?php

  

$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$EmailTo = "[email protected]";
$Subject = "Acai X3 Inquiry";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email']));
$AreaCode = Trim(stripslashes($_POST['AreaCode']));
$Prefix = Trim(stripslashes($_POST['Prefix'])); 
$Suffix = Trim(stripslashes($_POST['Suffix']));  
$Question = Trim(stripslashes($_POST['Question']));
$CheckBox = Trim(stripslashes($_POST['CheckBox'])); 


$validationOK=true;
if (Trim($FirstName)=="") $validationOK=false;
if (!$validationOK) {
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
 exit;
}


$Body = "";
$Body .= "First Name: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone Number: ";
$Body .= $AreaCode;
$Body .= $Prefix;
$Body .= $Suffix;
$Body .= "\n";
$Body .= "Zip Code: ";
$Body .= $Question;
$Body .= "\n";

isset( $_POST['chkfuturealerts'] ){
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}

if ($success){
 print "<meta http-equiv=\"refresh\" content=\"0;URL=https://www.securecartcenter.com/NjA5N3w2ODR8Mjc0NzEzfHYy/l7vem4b9/g\">";
}
else{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=contact3.html\">";
}

?>

 

Parse error: syntax error, unexpected '{' in /home8/craighoo/public_html/testing/submit.php on line 39

 

I tried placing the values anywhere and everywhere to make it send the email only then. (Yes, I also changed the name in the HTML)

I am now getting an error at whichever line contains the opening { of the PHP command I got from here.

Any help would be greatly appreciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.