Jump to content

if condition will not work


warmiak
Go to solution Solved by QuickOldCar,

Recommended Posts

Hey guys, 

 

my code stop at the condition 

if ((!empty($name)) && (!empty($email)) && (!empty($nachricht))) {

 I try a lot and i know it should be easy. But maybe i don't see the door staying in front of there :)

Maybe someone can help me with this few lines  ::) Thanks for reading the code

<?php
$msg='';
$from ='my email address';
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$send_form = false;


if($_SERVER["REQUEST_METHOD"] == "POST") 
{
	// Post wurde uebergeben
	if (empty($name) && empty($email) && empty($message)) {
	// Felder sind leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Pflichtfelder wurden nicht ausgefuellt</p></div>";
	$field_name = 'warning';
	$field_email = 'warning';
	$field_nachricht = 'warning';
	$send_form = true;
	}	
	if (empty($name) && (!empty($email)) && (!empty($message))) {
	// Name ist leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Das Feld Name wurde nicht ausgefuellt</p></div>";
	$field_name = 'warning';
	$send_form = true;
	}
	if (empty($name) && empty($email) && (!empty($message))) {
	// Name und E-Mail sind leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Das Feld Name und E-Mail wurden nicht ausgefuellt</p></div>";
	$field_name = 'warning';
	$field_email = 'warning';
	$send_form = true;
	}
	if (empty($name) && (!empty($email)) && empty($message)) {
	// Name und Nachricht sind leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Das Feld Name und Nachricht wurden nicht ausgefuellt</p></div>";
	$field_name = 'warning';
	$field_message = 'warning';
	$send_form = true;
	}
	if ((!empty($name)) && empty($email) && (!empty($message))) {
	// E-Mail ist leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Das Feld E-Mail wurde nicht ausgefuellt</p></div>";
	$field_email = 'warning';
	$send_form = true;
	}
	if ((!empty($name)) && empty($email) && empty($message)) {
	// E-Mail und Nachricht sind leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Das Feld E-Mail und Nachricht wurden nicht ausgefuellt</p></div>";
	$field_email = 'warning';
	$field_nachricht = 'warning';
	$send_form = true;
	}
	if ((!empty($name)) && (!empty($email)) && empty($message)) {
	// Nachricht ist leer
	$msg="<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'>
	<span aria-hidden='true'>×</span></button><p>Das Feld Nachricht wurde nicht ausgefuellt</p></div>";
	$field_message = 'warning';
	$send_form = true;
	}
	if ((!empty($name)) && (!empty($email)) && (!empty($message))) {
	// recaptcha pruefen
		$recaptcha=$_POST['g-recaptcha-response'];
			if(!empty($recaptcha)) {
				include("getCurlData.php");
				$google_url="https://www.google.com/recaptcha/api/siteverify";
				$secret='my secret code';
				$ip=$_SERVER['REMOTE_ADDR'];
				$url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
				$res=getCurlData($url);
				$res= json_decode($res, true);
				
					if($res['success']) {
					$msg="<div class='alert alert-success alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
	  		</button><p>Ihre Nachricht wurde erfolgreich an Warmiak Digitale Medien versendet.<br />Sie erhalten in kürze eine Kopie dieser Nachricht.</p></div>";	
					//mail($to, $subject, $message, $headers);
					echo 'it works !'; // For testing
					}
					
					else {
						$msg="<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
	  					</button><p>reCAPTCHA wurde nicht korrekt eingegeben ! Bitte versuchen Sie es erneut.</p></div>";
					}
			}
			else {
				$msg="<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
	  			</button><p>reCAPTCHA wurde nicht korrekt eingegeben ! Bitte versuchen Sie es erneut.</p></div>";
			}
		
		}	
}
else 
{
	$send_form = true;
}


if ($send_form) {

?>
<div class="container">
<?php echo $msg; ?>
<?php echo $felder; ?>
<form method="post" action="">
    	<!--    
    	<div class="input-group">
  			<span class="input-group-addon" id="sizing-addon1">Vorname   </span>
  			<input type="text" class="form-control" placeholder="Vorname" aria-describedby="sizing-addon1">
		</div><br />
        -->
        
        <div class="input-group <?php if($field_name == 'warning'): ?> has-warning has-feedback<?php endif ?>">
  			<span class="input-group-addon" id="sizing-addon1">Name</span>
  			<input id="name" name="name" type="text" class="form-control" placeholder="Vor u. Nachname" aria-describedby="sizing-addon1" value="<?php echo $name ?>">
		</div><br />
        
        <div class="input-group">
  			<span class="input-group-addon" id="sizing-addon1">Firma</span>
  			<input id="company" name="company" type="text" class="form-control" placeholder="Firma" aria-describedby="sizing-addon1" value="<?php echo $firma ?>">
		</div><br />        
        
        <div class="input-group <?php if($field_email == 'warning'): ?> has-warning has-feedback<?php endif ?>">
  			<span class="input-group-addon" id="sizing-addon1">E-Mail</span>
  			<input id="email" name="email" type="text" class="form-control" placeholder="E-Mail" aria-describedby="sizing-addon1" value="<?php echo $email ?>">
		</div><br />
        
        <div class="input-group">
  			<span class="input-group-addon" id="sizing-addon1">Betreff</span>
  			<select id="subject" name="subject" class="form-control">
  				<option>Allgemein</option>
  				<option>Termin</option>
  				<option>Anfrage</option>
			</select>
		</div><br />
        
        <div>
        <textarea id="message" name="message" class="form-control" placeholder="Nachricht eingeben" rows="8" <?php if($field_message == 'warning'): ?>style="background:#fcf8e3"<?php endif ?>><?php echo $nachricht ?></textarea><br />
        </div>
        
        <div class="g-recaptcha" data-sitekey="6LckhPASAAAAAM-dJPmrFmovICuk5EFvMSIxUrEI"></div><br />
        
        <button type="submit" class="btn btn-default">Absenden</button>
        
    </form><br /><br />
</div> 
<?php 
}
?>

Link to comment
Share on other sites

  • Solution

 

my code stop at the condition

  if ((!empty($name)) && (!empty($email)) && (!empty($nachricht))) {

This isn't in your posted code and the variable $nachricht does not exist.

 

Instead of multiple checking and changing around which is empty and not, why not make an error array and just test each POST, if does not meet your criteria then add it to the error array.

 

 

 

You can test this, style and change anything different in the form or use this as an idea.

<?php
$msg = '';
$from ='my email address';

if (isset($_POST['submit'])) {
    $errors = array();    
    
    if (isset($_POST['name']) && trim($_POST['name']) != '') {
        $name = trim($_POST['name']);
    } else {
        $name     = '';
        $errors[] = 'name';
    }
    
    if (isset($_POST['company']) && trim($_POST['company']) != '') {
        $company = trim($_POST['company']);
    } else {
        $company  = '';
        $errors[] = 'company';
    }
    
    if (isset($_POST['email']) && filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)) {
        $email = trim($_POST['email']);
    } else {
        $email    = '';
        $errors[] = 'email';
    }
    
    if (isset($_POST['subject']) && trim($_POST['subject']) != '') {
        $subject = trim($_POST['subject']);
    } else {
        $subject  = '';
        $errors[] = 'subject';
    }
    
    if (isset($_POST['message']) && trim($_POST['message']) != '') {
        $message = trim($_POST['message']);
    } else {
        $message  = '';
        $errors[] = 'message';
    }
    
    if(isset($_POST['g-recaptcha-response'])){
        $captcha=$_POST['g-recaptcha-response'];
    }
    
    if(!$captcha){
          $errors[] = 'captcha';
    } else {
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LckhPASAAAAAM-dJPmrFmovICuk5EFvMSIxUrEI&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        if($response.success==false){
          $errors[] = 'captcha';
        }
    }
    
    
    if (empty($errors)) {
        //escape,filter,sanitize before inserting data to mysql
        $msg = "Form was submitted";
    } else {
        $msg = "<p style='color:red;'>You have the following errors:" . implode($errors, ", ") . "</p>";
    }
}
?>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
<style>
*:focus {
    outline: none;
}
body {
    font: 14px/21px "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;
    max-width:99%;
}
.wrap{
    position:relative;
    width:75%;
    margin-left:auto;
    margin-right:auto;
    padding:5px;
}
ul{
    list-style-type: none;
    padding:0;
    margin:0;
}
.contact_form h2, .contact_form label {
    font-family:Georgia, Times, "Times New Roman", serif;
}
.form_hint, .required_notification {
    font-size: 11px;
}
.contact_form ul {
    width:750px;
    list-style-type:none;
    list-style-position:outside;
    margin:0px;
    padding:0px;
}
.contact_form li{
    padding:12px;
    border-bottom:1px solid #eee;
    position:relative;
}
.contact_form li:first-child, .contact_form li:last-child {
    border-bottom:1px solid #777;
}
.contact_form h2 {
    margin:0;
    display: inline;
}
.required_notification {
    color:#d45252;
    margin:5px 0 0 0;
    display:inline;
    float:right;
}
.contact_form label {
    width:150px;
    margin-top: 3px;
    display:inline-block;
    float:left;
    padding:3px;
}
.contact_form input {
    height:20px;
    width:300px;
    padding:5px 8px;
    -moz-transition: padding .25s;
    -webkit-transition: padding .25s;
    -o-transition: padding .25s;
    transition: padding .25s;
}
.contact_form textarea {
    padding:8px;
    width:300px;
    -moz-transition: padding .25s;
    -webkit-transition: padding .25s;
    -o-transition: padding .25s;
    transition: padding .25s;
}
.contact_form button {
    margin-left:156px;
}
.contact_form input, .contact_form textarea {
    padding-right:30px;
    border:1px solid #aaa;
    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
    border-radius:2px;
}
.contact_form input:focus, .contact_form textarea:focus {
    background: #fff;
    border:1px solid #555;
    box-shadow: 0 0 3px #00FF00;
    padding-right:70px;
}
/* Button Style */
button.submit {
    background-color: #68b12f;
    background: -webkit-gradient(linear, left top, left bottom, from(#68b12f), to(#50911e));
    background: -webkit-linear-gradient(top, #68b12f, #50911e);
    background: -moz-linear-gradient(top, #68b12f, #50911e);
    background: -ms-linear-gradient(top, #68b12f, #50911e);
    background: -o-linear-gradient(top, #68b12f, #50911e);
    background: linear-gradient(top, #68b12f, #50911e);
    border: 1px solid #509111;
    border-bottom: 1px solid #5b992b;
    border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    -ms-border-radius: 3px;
    -o-border-radius: 3px;
    box-shadow: inset 0 1px 0 0 #9fd574;
    -webkit-box-shadow: 0 1px 0 0 #9fd574 inset ;
    -moz-box-shadow: 0 1px 0 0 #9fd574 inset;
    -ms-box-shadow: 0 1px 0 0 #9fd574 inset;
    -o-box-shadow: 0 1px 0 0 #9fd574 inset;
    color: white;
    font-weight: bold;
    padding: 6px 20px;
    text-align: center;
    text-shadow: 0 -1px 0 #396715;
}
button.submit:hover {
    opacity:.85;
    cursor: pointer;
}
button.submit:active {
    border: 1px solid #20911e;
    box-shadow: 0 0 10px 5px #356b0b inset;
    -webkit-box-shadow:0 0 10px 5px #356b0b inset ;
    -moz-box-shadow: 0 0 10px 5px #356b0b inset;
    -ms-box-shadow: 0 0 10px 5px #356b0b inset;
    -o-box-shadow: 0 0 10px 5px #356b0b inset;
    
}
input:required, textarea:required {
    background: #fff;
    border-color:#FF0000;
    
}
.contact_form input:required:valid, .contact_form textarea:required:valid { /* when a field is considered valid by the browser */
    background: #fff;
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
}
.form_hint {
    background: #d45252;
    border-radius: 3px 3px 3px 3px;
    color: white;
    margin-left:8px;
    padding: 1px 6px;
    z-index: 999; /* hints stay above all other elements */
    position: absolute; /* allows proper formatting if hint is two lines */
    display: none;
}
.form_hint::before {
    content: "\25C0"; /* left point triangle in escaped unicode */
    color:#d45252;
    position: absolute;
    top:1px;
    left:-6px;
}
.contact_form input:focus + .form_hint {
    display: inline;
}
.contact_form input:required:valid + .form_hint {
    background: #28921f;
}
.contact_form input:required:valid + .form_hint::before {
    color:#28921f;
}

</style>
</head>
<body>
<div class="wrap">
<form class="contact_form" action="" method="post" name="contact_form" novalidate>
<ul>
<li>
    <h2>Contact Form</h2>
</li>    
<?php echo $msg;?>
<li>
<label for="name">Name:</label>
<input id="name" name="name" type="text" value="<?php echo $name;?>" required/>
</li>
<li>
    <label for="company">Company:</label>
    <input id="company" name="company" type="text" value="<?php echo $company;?>" required/>
</li>
<li>
    <label for="email">Email:</label>
    <input id="email" name="email" type="email" value="<?php echo $email;?>" required/>
    <span class="form_hint">Proper format "name@domain.com"</span>
</li>
<li>
    <label for="subject">Subject:</label>
    <textarea id="subject" name="subject" cols="40" rows="4" required><?php echo $subject;?></textarea>    
</li>
<li>
    <label for="message">Message:</label>
    <textarea id="message" name="message" cols="40" rows="4" required><?php echo $message;?></textarea>
</li>
<div class="g-recaptcha" data-sitekey="6LckhPASAAAAAM-dJPmrFmovICuk5EFvMSIxUrEI"></div>
<li>
    <button class="submit" type="submit" name="submit">Submit Form</button>
</li>
</ul>
</form>

<div>
</body>
</html>
Link to comment
Share on other sites

Thx you a lot QuickOldCar for the fast answer and the time you spent on my code and your example. This is really a smart way to check this values. Thx you a lot.

 

I make a mistake in my Post. I translate the variable $nachricht to $message for better understanding. Sorry for this.

 

Like i say your code is really great and i will use it and modify it for my needs. I will also thx you for this code pattern.

 

Maybe you or someone else can give me a hint why the condition stop work. I get no error with "error_reporting = E_ALL".

 

For better understanding and to write better codes. I still learning php and want to get as good a possible  :rolleyes:

	if ((!empty($name)) && (!empty($email)) && (!empty($message))) {
	// recaptcha pruefen
		$recaptcha=$_POST['g-recaptcha-response'];
			if(!empty($recaptcha)) {
				include("getCurlData.php");
				$google_url="https://www.google.com/recaptcha/api/siteverify";
				$secret='my secret code';
				$ip=$_SERVER['REMOTE_ADDR'];
				$url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
				$res=getCurlData($url);
				$res= json_decode($res, true);
				
					if($res['success']) {
					$msg="<div class='alert alert-success alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
	  		</button><p>Ihre Nachricht wurde erfolgreich an Warmiak Digitale Medien versendet.<br />Sie erhalten in kürze eine Kopie dieser Nachricht.</p></div>";	
					//mail($to, $subject, $message, $headers);
					echo 'it works !'; // For testing
					}
					
					else {
						$msg="<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
	  					</button><p>reCAPTCHA wurde nicht korrekt eingegeben ! Bitte versuchen Sie es erneut.</p></div>";
					}
			}
			else {
				$msg="<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span>
	  			</button><p>reCAPTCHA wurde nicht korrekt eingegeben ! Bitte versuchen Sie es erneut.</p></div>";
			}
		
		}	
}
else 
{
	$send_form = true;
}
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.