2016-12-17 3 views
0

Ich habe keine Antwort gefunden also ich frage euch. window.location.reload() wird ständig ohne Pause neu geladen.window.location.reload lädt ständig

Ich versuche etwas zu machen, das überprüft, ob das Formular keine Eingabe hat, und wenn ich nicht möchte, dass es eine Warnung ausgibt, funktioniert es, aber es lädt ständig neu. Hier ist mein Code:

<?php 
$from = $_POST['email_adress']; 
$subject = $_POST['subject']; 
$select = $_POST['select']; 
$message2 = $_POST['message2']; 
$name = $_POST['firstname']; 
$name2 = $_POST['lastname']; 
if ($_POST['firstname'] == "") { 
    echo '<script language="javascript">'; 
    echo 'alert("First Name is Mandatory.");'; 
    echo 'window.location.reload("contactus.html");'; 
    echo '</script>'; 
    exit; 

} 
elseif ($_POST['subject'] == "") { 
    echo '<script language="javascript">'; 
    echo 'alert("Subject is Mandatory.");'; 
    echo 'window.location.reload("contactus.html");'; 
    echo '</script>'; 
    exit; 
} 
elseif ($_POST['email_adress'] == "") { 
    echo '<script language="javascript">'; 
    echo 'alert("Email Adress is Mandatory.");'; 
    echo 'window.location.reload("contactus.html");'; 
    echo '</script>'; 
    exit; 
} 
elseif ($_POST['message2'] == "") { 
    echo '<script language="javascript">'; 
    echo 'alert("A Message is Mandatory.");'; 
    echo 'window.location.reload("contactus.html");'; 
    exit; 
    echo '</script>'; 
    exit; 
} else { 
header("Location: contactus.html"); 
$email_subject = "A submittion form"; 
$to ="[email protected]"; 
$headers = "From: [email protected]"; 
$email_body = 'You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the general subject of $select and more specifically $subject and the message is $message2'; 
mail($to,$email_subject,$email_body, $headers); 
} 

?> 
+0

'window.location.reload' nimmt keine Zeichenkette, es braucht einen booleschen Wert. Sie laden gerade die gleiche Seite neu. https://developer.mozilla.org/en-US/docs/Web/API/Location/reload – chris85

+0

Ich würde sagen, wenn Sie nicht möchten, dass eine Seite neu geladen wird, verwenden Sie window.location.reload nicht. – raichu

+0

Ich benutze window.location.reload, weil es die anderen Sachen behält, anstatt eine neue Seite zu öffnen und die Sachen zu entfernen, die sie geschrieben haben. –

Antwort

0

Diese Lösung erfordert, dass Sie Ihre contactus.html-contactus.php um sessions zu verwenden, ändern.

<?php 
// to use a session, you must start it 
session_start(); 
// variable to store any error message 
$error = null; 

$from = $_POST['email_adress']; 
$subject = $_POST['subject']; 
$select = $_POST['select']; 
$message2 = $_POST['message2']; 
$name = $_POST['firstname']; 
$name2 = $_POST['lastname']; 

if ($_POST['firstname'] == "") { 
    $error = "First Name is Mandatory."; 
} elseif ($_POST['subject'] == "") { 
    $error = "Subject is Mandatory."; 
} elseif ($_POST['email_adress'] == "") { 
    $error = "Email Adress is Mandatory."; 
} elseif ($_POST['message2'] == "") { 
    $error = "A Message is Mandatory."; 
} 

if ($error) { 
    // store the error in a session so that you can use it on another page 
    $_SESSION['error'] = $error; 
} else { 
    $email_subject = "A submittion form"; 
    $to ="[email protected]"; 
    $headers = "From: [email protected]"; 
    $email_body = 'You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the general subject of $select and more specifically $subject and the message is $message2'; 
    mail($to,$email_subject,$email_body, $headers); 
} 

// regardless of whether there is an error or not, it always goes to Contact Us page 
header("Location: contactus.php"); 
exit; 
?> 

Dann in Ihrem contactus.php, würden Sie haben

<?php 
// resume session again 
session_start(); 
?> 

<!-- somewhere in your HTML code --> 

<?php if (isset($_SESSION['error'])) : ?> 
    <div class="error-message"><?php echo $_SESSION['error'] ?></div> 
<?php endif ?> 

<?php 
// you probably will want to remove the error afterwards 
unset($_SESSION['error']); 
?> 

Wenn Sie das nicht tun, dann wird diese Lösung nicht funktionieren, und Sie werden zu AJAX zurückgreifen müssen (wo Sie zurückschicken Ihre Fehlermeldung über die Antwort erhalten Sie von Ihrer AJAX-Antwort zurück).

0

Vielleicht kann dies eine Inspiration sein?

<?php 
session_start(); 

if ($_SERVER['REQUEST_METHOD'] === 'GET') { 

    $is_redirect = isset($_SESSION['to']); 
    if ($is_redirect && !is_mail_valid($_SESSION)) { 
     $to = $_SESSION['to']; 
     $subject = $_SESSION['subject']; 
     $body = $_SESSION['body']; 
    } 
    $_SESSION = array(); 
} else { 
    if (is_mail_valid($_POST)) { 
     $to = $_POST['to']; 
     $subject = $_POST['subject']; 
     $body = $_POST['body']; 
     mail($to, $subject, $body); 
    } else { 
     $_SESSION = $_POST; 
    } 
    header("Location: index.php"); 
    exit; 
} 

function is_mail_valid($mail) { 
    global $errors; 
    global $valid; 
    $errors = array(); 
    if (trim($mail['to']) == FALSE) { $errors['empty_to'] = TRUE; } 
    if (trim($mail['subject']) == FALSE) { $errors['empty_subject'] = TRUE; } 
    if (trim($mail['body']) == FALSE) { $errors['empty_body'] = TRUE; } 
    $valid = empty($errors); 
    return $valid; 
} 

?> 



<?php if (!$valid) { ?> 
<script type="text/javascript"> 
    <?php if ($errors['empty_to']) {?> 
     alert('To: cannot be empty') 
    <?php } ?> 
    <?php elseif ($errors['empty_subject']) {?> 
     alert('Subject: cannot be empty') 
    <?php } ?> 
    <?php elseif ($errors['empty_body']) {?> 
     alert('Body: cannot be empty') 
    <?php } ?> 
</script> 
<?php } ?> 

<form method="post"> 
    <div> 
     <label for="to">To</label> 
     <input id="to" type="email" name="to" value="<?php echo $to;?>" /> 
    </div> 
    <div> 
     <label for="subject">Subject</label> 
     <input id="subject" type="text" name="subject" value="<?php echo $subject;?>" /> 
    </div> 
    <div> 
     <label for="body">Body</label> 
     <textarea id="body" name="body"><?php echo $body;?></textarea> 
    </div> 
    <div> 
     <input type="submit" value="Send"> 
    </div> 
</form> 

Grundsätzlich starten Sie Session mit session_start() verwenden

Auf POST:

  • Wenn Sie keinen Fehler haben:
    • die E-Mail senden
    • Umleitung zur conctactus Seite.
  • Wenn Sie einen Fehler haben:
    • speichern Sie die Eingabe in der Sitzung
    • auf die Kontakt-Seite

Auf GET umleiten:

  • Wenn du nichts in der Session hast oder die Mail in Session ist gültig, render einfach den HTML-Code.
  • Wenn Sie etwas in der Sitzung haben und diese Daten ungültig sind, rendern Sie das HTML, rendern Sie das Alarmskript und legen Sie die "value" -Attribute der Eingabe fest, um die Eingabewerte des Benutzers beizubehalten.
  • Löschen Sie die Sitzungsdaten.
Verwandte Themen