2014-10-07 88 views
2

我一直想發佈一個JavaScript警報(彈出窗口帶有消息),只是說「謝謝!您已被添加!」。一旦用戶輸入了表單中的詳細信息並單擊提交按鈕。在PhP中運行javascript警報

我已經使用Google如何做到這一點,但是當添加它時,它從來沒有工作過,並嘗試過不同的方式,但現在卻越來越難以做到。如果有人知道我做錯了什麼,那麼任何幫助將不勝感激。

我也想在一個單獨的php文件中注意到這一點。

下面你會發現我的代碼;

insert.php

<?php 
$con=mysqli_connect("localhost","cl51-main-3i6","password","cl51-main-3i6"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$firstname = mysqli_real_escape_string($con, $_POST['firstname']); 
$lastname = mysqli_real_escape_string($con, $_POST['lastname']); 
$email = mysqli_real_escape_string($con, $_POST['email']); 

$sql="INSERT INTO emails (Firstname, Lastname, Email) VALUES ('$firstname', '$lastname', '$email')"; 

if (!mysqli_query($con,$sql)) { 
    die('Error : ' . mysql_error($con)); 
} 

echo '<script language="javascript">'; 
echo 'alert("Thank you! You've now joined the e-mail club!")'; 
echo '</script>'; 

header("Location: http://isometricstudios.co.uk/news.html"); 
exit; 

mysqli_close($con); 
?> 
+0

首先,你不能改變'頭()'出發送HTML內容後頁。如果你正在這樣做,我建議使用'$ _GET'值將它們轉發到成功頁面,說明成功'header(「Location:http://isometricstudios.co.uk?success=true」)',然後打開如果你的回聲行帶警報有php語法錯誤,你甚至不會去header()(如果($ _ GET ['success'] =='true')'把你的警報代碼 – Kender 2014-10-07 21:20:47

+1

'行來觸發重定向... – 2014-10-07 21:23:16

回答

1

問題是因爲header()會立即執行重定向。在<script>代替header(location)

<?php 
$con=mysqli_connect("localhost","cl51-main-3i6","password","cl51-main-3i6"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$firstname = mysqli_real_escape_string($con, $_POST['firstname']); 
$lastname = mysqli_real_escape_string($con, $_POST['lastname']); 
$email = mysqli_real_escape_string($con, $_POST['email']); 

$sql="INSERT INTO emails (Firstname, Lastname, Email) VALUES ('$firstname', '$lastname', '$email')"; 

if (!mysqli_query($con,$sql)) { 
    die('Error : ' . mysql_error($con)); 
} 

echo '<script language="javascript">'; 
echo 'alert("Thank you! You've now joined the e-mail club!")'; 
echo 'window.location.href="http://isometricstudios.co.uk/news.html";'; 
echo '</script>'; 

exit; 

mysqli_close($con); 
?> 

現在的警報會顯示使用window.location,只有當按下OK按鈕重定向

0

你有沒有scaped的「在你的字符串?

echo 'alert("Thank you! You\'ve now joined the e-mail club!")'; 
0

你逃避你的字符串單引號,你迴音:

echo 'alert("Thank you! You\'ve now joined the e-mail club!")'; 
0

你的代碼是天晴從一條線

echo 'alert("Thank you! You've now joined the e-mail club!")'; 

您正在使用的引述行,但你有一個'你已經。寫下「你有」。 :)

echo 'alert("Thank you! You have now joined the e-mail club!")'; 

你也無法逃脫它的其他應答者說,但它使代碼在我opinnion

0

略有梅西耶如果您重定向一個位置標頭的瀏覽器,在網頁上任何JavaScript代碼沒有執行,因爲您正在重定向到的頁面被加載。

此外,我不認爲你可以在調用標題函數之前回顯或輸出任何內容。

如果你想顯示一個警告,這樣做你重定向到頁面上,或者,如果你的控制之下沒有,重定向使用的,而不是與Location頭的javascript:

document.location = 'http://isometricstudios.co.uk/news.html'; 

而且,它可能更容易,不使用回聲編寫JavaScript,因爲這樣你不得不逃離報價,你失敗

echo 'alert("Thank you! You've now joined the e-mail club!")'; 

它更容易只是不停的JavaScript你的PHP代碼之外的事,所以像

<?php 
(do server-side php stuff) 
?> 

<script> 
(do client-side javascript stuff) 
</script> 

<?php 
(do more server-side php stuff) 
?>