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 = "xx-xxxx@gmail.com";
$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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 = "xxx.xxxx@gmail.com";
$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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.