2016-10-19 55 views
0

我的PHP表單似乎運行良好。頁面出現,當我點擊提交按鈕時,isset($ _ POST ['submitted'])條件在開始時被擊中並且代碼運行。無法使用PHP形式的回聲

唯一的問題是,我在代碼中有幾條回聲線來產生JS警報框。似乎沒有被召喚。我不確定外部PHP函數是否被調用,因爲我無法測試返回的值。

我的PHP看起來像這樣

<?php if (isset($_POST['submitted'])) { 
$output = checkData(); 
if ($output != "done") 
{ 
    echo '<script type="text/javascript">alert("' . $output . '"); </script>'; 
} 
else 
{ 
    createMeeting(); 
    echo '<script type="text/javascript">alert("You meeting has been created. All of the recipients should shortly receive an email"); </script>'; 
    header('Location: index.php'); 
} 
} else { ?> 
<center> 
<form method="POST" action=""> 
... 
<input type="submit" name="submitted" value="Create Meeting"> 
</form> 
<?php 
} 
?> 

我checkData()函數將進行檢查,看是否表單數據的其他部分是空的退出或者用「完成」(沒有錯誤),或者如果消息其中一個表單元素是空的。

createMeeting()將根據數據創建一個會議並將其提交給我的服務器 - 目前,它將採用與checkData()相同的數據,然後返回。

當我通過在線PHP代碼檢查器運行它時,這兩個函數都會返回而不會出錯。

+0

把'echo'';'告訴我們'$ output'的值。 – Irvin

+0

服務器解析並提供頁面後,php是否繼續運行? – theonlygusti

+0

你的php代碼似乎是錯誤的,爲什麼你把2個其他條件。 –

回答

0

我試過你的代碼,它似乎在我的情況下工作正常。

<?php if (isset($_POST['submitted'])) { 
$output = "done"; //even if this line is not equal to 'done', the code still works fine 
if ($output != "done") 
{ 
    echo '<script type="text/javascript">alert("' . $output . '");</script>'; 
} 
else 
{ 
    // createMeeting(); 
    echo '<script type="text/javascript">alert("You meeting has been created. All of the recipients should shortly receive an email"); </script>'; 
    // header('Location: index.php'); 
} 
} else { ?> 
    <center> 
    <form method="POST" action=""> 
    ... 
    <input type="submit" name="submitted" value="Create Meeting"> 
    </form> 
<?php 
} 
?> 

所以就我所知,createMeeting()或checkData()函數存在錯誤。你可以更具體的錯誤信息(如果有的話)?

+0

沒有錯誤消息正在通過。 checkData的來源可以在這裏找到:http://pastebin.com/W9SJDHmZ - 我有一個require_once('checkData.php');在文件的最頂端 – Nodoid

+0

你有沒有在上面的代碼中回覆第3行的'$ output'? – Pragun

+0

是的 - 什麼也沒有顯示 – Nodoid

0

檢查你的php.ini。它是設置爲顯示還是隱藏錯誤?它可能會在checkdata中遇到錯誤,並且在沒有任何警告的情況下死亡。

+0

它顯示錯誤(至少,顯示其中的一些) - 因爲它在我的網絡託管,我真的沒有太多的控制水平顯示的錯誤 – Nodoid

0

如果你想彈出警告框然後重定向它,那麼這不是正確的方法來做到這一點。

注意:警告框將不會執行或顯示,因爲頁面在顯示前會重定向。最好的辦法是創建一個JavaScript函數來代替

<script> 
// this function pop up and redirect your page after closing the alert box 
function PopupThenRedirect() { 
    // pop up the alert box 
    alert("You meeting has been created. All of the recipients should shortly receive an email"); 
    // redirect your page 
    location.href = '/index.php'; 
} 
</script> 

<?php if (isset($_POST['submitted'])) { 
$output = checkData(); 
if ($output != "done") 
{ 
    echo '<script type="text/javascript">alert("' . $output . '"); </script>'; 
} 
else 
{ 
    createMeeting(); // make sure this php function exist or else it leads to error 
    echo '<script type="text/javascript"> PopupThenRedirect(); </script>'; 
} 
} else { ?> 
<center> 
<form method="POST" action=""> 
... 
<input type="submit" name="submitted" value="Create Meeting"> 
</form> 
<?php 
} 
?>