mercredi 29 juin 2016

PHP if else statement, one works other not. why? [duplicate]

This question already has an answer here:

I have a form in HTML which is submitted to a PHP page.

It is working fine but I wonder why I had to change code to do so.

what is wrong with my personal way. I set the variable $sendcopy to eigther Ja or Nee at the end I check whether it's Ja or Nee and have a if else statement.

if ($sendcopy = 'Ja') { 
//echo yes; 
} else {
//echo no; 
}

this doesn't work for me and I don't know why because through troubleshooting $sendcopy does have the correct value Ja or Nee.

here is the form and php page

HTML just the form part nothing wrong here

there is a checkbox to choose to get a copy of the submitted form its name and id is scopy.

            <section id="form" class="main">
                    <div class="container">
                    <form method="POST" action="./form-submit.php" onsubmit="versturen.disabled = true; return true;">
                        <div class="row uniform 50%">
                            <div class="6u 12u$(xsmall)">
                                <input type="text" name="naam" id="naam" value="" placeholder="Naam Verplicht" required />

                            </div>
                            <div class="6u$ 12u$(xsmall)">
                                <input type="email" name="clientemail" id="clientemail" value="" placeholder="Email Verplicht" required />

                            </div>
                            <div class="12u$">
                                <div class="select-wrapper">
                                    <select name="onderwerp" id="onderwerp">
                                        <option value="">- Onderwerp -</option>
                                        <option value="Info">Informatie</option>
                                        <option value="Offerte">Offerte</option>
                                        <option value="Admin">Administratie</option>
                                        <option value="Overig">Anders</option>
                                    </select>
                                </div>
                            </div>
                            <div class="12u$">
                                <textarea name="bericht" id="bericht" placeholder="Laat uw bericht achter" rows="4"></textarea>
                            </div>

                                </div>
                                <div class="6u 12u$(small)">
                                    <input type="checkbox" id="scopy" name="scopy">
                                    <label for="scopy">Email een copy naar u zelf</label>
                                <br />
                                    <input type="checkbox" id="ismens" name="ismens" required>
                                    <label for="ismens">check als je een mens bent</label>

                                </div>
                                <div class="12u$">
                                    <ul class="actions">
                                        <li><input type="submit" name="versturen" value="Send Message" class="special" /></li>
                                        <li><input type="reset" value="Reset" onclick="versturen.disabled = false; return true;"/></li>
                                    </ul>
                                </div>
                            </div>
                        </form>
                        </div>
                    </section>

PHP full page

<?php
//var_dump($_POST);

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

    $email_to = "webmaster@yourdomain.com"; // Je eigen Email adres


    function died($error) {

        // fout code gaat hier

        echo "Het spijt ons heel erg, maar er zijn fout(en) gevonden in het formulier dat u verstuurde. ";

        echo "Deze fout(en) staat hier beschreven.<br /><br />";

        echo $error."<br /><br />";

        echo "Gaat u alstublieft terug om de fout te herstellen.<br /><br />";

        echo "<a href='#' onClick='history.go(-1)'>Go Back</a>";  

        die();

    }

 // Obsolete checks for html5 

 //    echo "Test line 1";

    // validatie van het formulier

    if(!isset($_POST['naam']) ||

        !isset($_POST['clientemail']) ||

        !isset($_POST['ismens']) ||

        !isset($_POST['bericht'])) {

        died('Het spijt ons, het formulier is onjuist ingevuld. <br /> Controleer dat de verplichte velden zijn ingevuld.');       

    }
 // End Checks

 // Set variables 

 //    echo "Test line 2";

    $first_name = $_POST['naam']; // verplicht

    $email_from = $_POST['clientemail']; // verplicht

    $comments = $_POST['bericht']; // verplicht max 300 tekens

    $subject = $_POST['onderwerp'];
#############################################
    if(!isset($_POST['scopy'])) {
        $sendcopy = 'Nee';
    } else {
        $sendcopy = 'Ja';
    }
#############################################
 // Check for errors, Obsolete for html5   

    $error_message = "";

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'Onbekende tekens in uw Naam.<br />';

  }

  if(strlen($comments) < 9) {

    $error_message .= 'De inhoud van het bericht wat u probeert te versturen is te kort.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }


    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }

    $email_subject = "Website Contact $subject";

    $comments = wordwrap($comments, 50,"<br />\n");

    $email_message = "<br /><br />Formulier gegevens: <br /><br />\n\n";

    $email_message .= "Klant Naam: ".clean_string($first_name)."<br />\n";

    $email_message .= "Klant Email: ".clean_string($email_from)."<br />\n";

    $email_message .= "Kopie gestuurd: ".clean_string($sendcopy)."<br /><br />\n";

    $email_message .= "Bericht: ".clean_string($comments)."<br />\n\n";



     #######################################

// create email headers
//if ($sendcopy = 'Ja') {
if(isset($_POST['scopy'])) {

$headers = 'From: '.$email_from."\r\n".

'Cc: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

} else {

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();


}
#########################################

//mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->



Thank you for contacting us. We will be in touch with you very soon.


<?php
/*
echo "<br /><br />";

echo "Subject: $email_subject <br /><br />";
echo "$email_message <br /><br />";
echo "$headers";
 */
}

?>

this solved it but I don't understand why can someone explain this to me.

if(isset($_POST['scopy'])) {
//echo yes; 
} else {
//echo no;
}

Aucun commentaire:

Enregistrer un commentaire