Also if/else trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
basbult
Forum Newbie
Posts: 1
Joined: Fri Mar 17, 2006 7:03 am

Also if/else trouble

Post by basbult »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a problem with the if/else structure i'm using, first there is a check, (if (isset ($_POST["prijs3"])) ), if some var's earlier are posted. if this is not okay a message will be shown else a table with content will be shown.

Then there will be a new form for users to fill in there adress and stuff, this will be send to an emailadress and to the mysql database. This works fine BUT the check if the form is submitted does not work this check:

Code: Select all

<? 
if (!$_POST['submit'] || $_POST['submit'] && (!$_POST['naam'] || !$_POST['mail'] || !checkmail($_POST['mail']) || !$_POST['onderwerp'])) 
{ 
    if ($_POST['submit'] && (!$_POST['naam'] || !$_POST['mail'] || !checkmail($_POST['mail']) || !$_POST['onderwerp']))    
    { 
        echo "U bent uw naam, e-mail adres, onderwerp of bericht vergeten in te vullen. Ook kan het zijn "; 
        echo "dat u een verkeerd e-mail adres hebt ingevuld.<p>"; 
    } 
?>
it checks if the submit button is used and if there is something filled in. But it is ignored so the form will be automaticly send when the first check (if (isset ($_POST["prijs3"])) )is done. I think i have a problem with the if/else structure does somebody see what's wrong?? I'll already tried but cannot find it....

Code: Select all

<?php 
//error_reporting(E_ALL); 

if (isset ($_POST["prijs3"])) 
{ 
        if($_POST['meters'] == '') // Als het veld leeg is. 
        {   
        echo  
        "<p></p> 
        <table class=\"content\"> 
            <tr> 
                <td>U heeft het aantal vierkante meters vergeten in te vullen!</td> 
            </tr> 
            <tr> 
                <td><a href='javascript:history.back();'>Klik hier om terug te gaan!</a></td> 
            </tr> 
        </table>"; 
        }   

        elseif($_POST['verwarming'] == '') // Als het veld leeg is. 
        {   
        echo  
        "<p></p> 
        <table class=\"content\"> 
            <tr> 
                <td>U heeft de verwarmingssoort vergeten in te vullen!</td> 
            </tr> 
            <tr> 
                <td><a href='javascript:history.back();'>Klik hier om terug te gaan!</a></td> 
            </tr> 
        </table>"; 
        }  
else 
        {  
        echo" 
<table class=\"content\"> 
    <tr> 
        <td> 
        Wij weten dat veel mensen afhaken bij 
        een prijsaanvraag op het moment dat men naam, adres, telefoon en woonplaatsgegevens moet invullen. 
        In ruil voor een serieuze en professionele behandeling van uw aanvraag vragen wij u deze gegevens toch 
        te verstrekken. Wij zullen hier discreet mee omgaan en wanneer er geen vragen zijn van onze kant zullen 
        wij u niet onnodig lastig vallen. 
        </td> 
    </tr> 
    <tr> 
    <tr> 
        <td>Vul hieronder uw contactgegevens in en druk op versturen</td> 
    </tr> 
</table> 
"; 
//} 
//} 
// geef e-mail adres op van ontvanger 
$mail_ontv = "mailadres@mail.nl"; 

// is niet 100% !!! 
function checkmail($mail)  
{  
    $email_host = explode("@", $mail);  
    $email_host = $email_host['1'];  
    $email_resolved = gethostbyname($email_host);  

    if ($email_resolved != $email_host && eregi("^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$mail))  
        $valid = 1;  

    return $valid;  
}  
// als er niet op submit is gedrukt, of als er wel op is gedrukt maar niet alles ingevoerd is 
// controleren of formulier verzonden wordt 
if (!$_POST['submit'] || $_POST['submit'] && (!$_POST['naam'] || !$_POST['mail'] || !checkmail($_POST['mail']) || !$_POST['onderwerp']))  
{  
    if ($_POST['submit'] && (!$_POST['naam'] || !$_POST['mail'] || !checkmail($_POST['mail']) || !$_POST['onderwerp']))    
    {  
        echo "U bent uw naam, e-mail adres, onderwerp of bericht vergeten in te vullen. Ook kan het zijn ";  
        echo "dat u een verkeerd e-mail adres hebt ingevuld.<p>";  
    }  
       
    // form + tabel 
    echo " 
<table class=\"content\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> 
<form method=\"POST\" ACTION=\"" . $_SERVER['PHP_SELF'] . "\"> 
<input type=hidden name=onderwerp value=\"Offerte Pakketnr $pakket $verwarming -verwarming \"> 

    <tr> 
        <td>Pakket:</td> 
        <td>$omschrijving</td> 
    </tr> 
    <tr> 
        <td>Pakketnr:</td> 
        <td><input type=\"hidden\" name=\"pakket\" value=\"$pakket\">$pakket</td> 
    </tr> 

    <tr> 
        <td>Soort verwarming:</td> 
        <td><input type=\"hidden\" name=\"verwarming\" value=\"$verwarming\">$verwarming</div></td> 
    </tr> 
    <tr> 
        <td>Aantal vierkantemeters:</td> 
        <td><input type=hidden name=meters value=$meters>$meters</td> 
    </tr> 
    <tr> 
        <td>Naam:</td> 
        <td><input type=\"text\" name=\"naam\" value=\"" . $_POST['naam'] . "\"></td> 
    </tr> 
    <tr> 
        <td>Adres:</td> 
        <td><input type=\"text\" name=\"adres\" value=\"" . $_POST['adres'] . "\"></td> 
    </tr> 
    <tr> 
        <td>Postcode:</td> 
        <td><input type=\"text\" name=\"postcode\" value=\"" . $_POST['postcode'] . "\" maxlength=\"7\"></td> 
    </tr> 
    <tr> 
        <td>Woonplaats:</td> 
        <td><input type=\"text\" name=\"woonplaats\" value=\"" . $_POST['woonplaats'] . "\"></td> 
    </tr> 
    <tr> 
        <td>&nbsp;</td> 
    </tr> 
     <tr> 
        <td>Telefoon:</td> 
        <td><input type=\"text\" name=\"telefoon\" value=\"" . $_POST['telefoon'] . "\"></td> 
    </tr> 
    <tr> 
        <td>E-mail adres:</td> 
        <td><input type=\"text\" name=\"mail\" value=\"" . $_POST['mail'] . "\"></td> 
    </tr>      
    <tr> 
        <td>Eventuele opmerkingen</td> 
        <td><TEXTAREA name=\"opmerking\" ROWS=\"6\" COLS=\"30\">" . htmlentities($_POST['opmerking']) . "</TEXTAREA></td> 
    </tr> 
    <tr> 
        <td>Offerte via:</td> 
        <td>     
                <select name=\"contact\"> 
                    <option value=\"Email\">E-mail</option> 
                    <option value=\"Post\">Post</option> 
                    <option value=\"Telefoon\">Telefoon</option> 
                 </select> 
        </td> 
    </tr> 
    <tr> 
        <td align=\"left\"><input type=button value=\"<-- ga terug\" onclick=\"history.back()\"> 
          <td><input type=\"submit\" name=\"submit\" value=\"Offerte aanvragen\"></td> 
    </tr> 
</form> 
</table>";  
//} 
// versturen naar 
//else 
//{       
   // set datum 
    $datum = date("d/m/Y H:i");  
       
    // set ip 
    $ip = $_SERVER['REMOTE_ADDR'];  
     
    //database verbinding 
    include ("connect.php"); 
    
   //offerte in database opslaan 
    $query="insert into offerte 
            (datum, ipadres, pakket, verwarming,meters, naam, adres, postcode, woonplaats, telefoon, email, contact, opmerking) 
            values 
            ('$datum','$ip','".$pakket."','".$verwarming."','".$meters."','".$naam."','".$adres."','".$postcode."','".$woonplaats."','".$telefoon."','".$mail."','".$contact."','".$opmerking."')" or die ('FOUT : Query maken mislukt');  
    $result= mysql_query($query) or die ("FOUT:" .mysql_error() ); 

       
    $inhoud_mail = "===================================================\n";  
    $inhoud_mail .= "Offerte \n";  
    $inhoud_mail .= "===================================================\n\n";  

    $inhoud_mail .= "Pakket: " . $_POST['pakket'] . "\n";  
    $inhoud_mail .= "Soortverwarming: " . $_POST['verwarming'] . "\n";  
    $inhoud_mail .= "Aantal m2: " . $_POST['meters'] . "\n\n";  

       
    $inhoud_mail .= "Naam: " . $_POST['naam'] . "\n";  
    $inhoud_mail .= "Adres: " . $_POST['adres'] . "\n";  
    $inhoud_mail .= "Postcode: " . $_POST['postcode'] . "\n";  
    $inhoud_mail .= "Woonplaats: " . $_POST['woonplaats'] . "\n\n";  
     
    $inhoud_mail .= "E-mail adres: " . $_POST['mail'] . "\n";  
    $inhoud_mail .= "Telefoon: " . $_POST['telefoon'] . "\n";  
    $inhoud_mail .= "Contact via: " . $_POST['contact'] . "\n\n";  

    $inhoud_mail .= "Opmerkingen vragen:\n";  
    $inhoud_mail .= $_POST['opmerking'] . "\n\n";  
       
    $inhoud_mail .= "Verstuurd op $datum via het ip " . $ip . "\n\n";  
       
    $inhoud_mail .= "===================================================\n\n";  
     
    // -------------------- 
    // spambot protectie 
    // ------ 
    // van de tutorial: http://www.phphulp.nl/php/tutorials/10/340/ 
    // ------ 
     
    $headers = "From: " . $_POST['naam'] . " <" . $_POST['mail'] . ">"; 
     
    $headers = stripslashes($headers); 
    $headers = str_replace("\n", "", $headers); // Verwijder \n 
    $headers = str_replace("\r", "", $headers); // Verwijder \r 
    $headers = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $headers)); // Slashes van quotes 
     
    $_POST['onderwerp'] = str_replace("\n", "", $_POST['onderwerp']); // Verwijder \n 
    $_POST['onderwerp'] = str_replace("\r", "", $_POST['onderwerp']); // Verwijder \r 
    $_POST['onderwerp'] = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_POST['onderwerp'])); // Slashes van quotes 
      
    mail($mail_ontv, $_POST['onderwerp'], $inhoud_mail, $headers);  
      
    echo "<p class=\"content\"><b>Uw offerte aanvraag is succesvol verstuurd!</b></p>"; 
     
    }  
    } 
    } 
?>  
</body> 
</html>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

run this code with

Code: Select all

error_reporting(E_ALL);
and do

Code: Select all

var_export($_POST);  // at the beginning of the script.
I am pretty sure you will solve it yourself.
Have fun coding
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I wouldn't check for all those post variables in that manor, instead I'd opt to look at $_SERVER['REQUEST_METHOD'] and/or use isset() against the post variables I'm expecting.
Post Reply