php & checkboxes

Discussion in 'Web Design and Development' started by cwesty, Nov 5, 2006.

  1. cwesty macrumors member

    Joined:
    Oct 22, 2005
    #1
    Hi there,

    I'm trying to get the checkboxes on my form to pickup and be included in the email to me.. For some reason it doesn't work...

    here's the php:
    <?php
    $to = "email@somewhere";
    $subject = "$subject";
    $message = "$message";
    $from = "$email";
    $headers = "From: $from";
    $checkbox = "$Yes Please";
    $checkbox = "$No Thanks";

    mail($to,$subject,$message,$headers,$checkbox);
    echo('<h2>Thank you for contacting');; ?>

    Any ideas????
     
  2. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #2
    just guessing it's because your variables are called $Yes Please and $No Thanks - the spaces mean it's looking for $Yes and $No.

    I'd have thought also that they would be radio buttons rather than checkboxes (or can you say Yes and No at the same time?), and that your PHP should be doing an if else query to see which one was checked and generating a variable value in response, rather than looking for a value passed from the checkbox
     
  3. cwesty thread starter macrumors member

    Joined:
    Oct 22, 2005
    #3
    i've just tried amending it to show $Yes and $No, but the same result,
    I get an email, but without the checkbox response..
     
  4. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #4
    Normally I wouldn't expect a checkbox to pass a value, just whether it was checked or not, the PHP then checks each checkbox to see if checked=checked and assigns a variable value to it at that point based on whether it was true or not
     
  5. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #5
    actually, sorry, completely wrong :p -

    if ( isset ($_POST['submit'])) {

    $yesorno=addslashes($_POST['yesorno']);

    $response = "Would you like my spam? ".$yesorno;

    ... email sending stuff..

    } else {

    echo "<form action=\"email.php\">

    .. other form bits..

    <p><input type=\"radio\" checked=\"checked\" value=\"Yes Please!\" name=\"yesorno\"> Yes Please or <input type=\"radio\" value=\"No Thanks\" name=\"yesorno\"> No Thanks</p>";

    echo "<p><input type=\"submit\" name=\"submit\" value=\"Email\"><br></p></form>"; // submit

    }
     
  6. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #6
    Also, I'm presuming that whatever code you're using to build the email is combining both the $message and the $checkbox response into one variable at some point to pass to the email 'body'? You may only need to update the $message variable with

    $message = .$checkbox

    and the way you have it set at the moment, you will always get a negative response because the second $checkbox = "$No Thanks"; is overwriting the value of $checkbox you set in the line $checkbox = "$Yes Please"; above it
     

Share This Page