2017-04-20 157 views
0

我有以下.PHP代碼,其中包含HTML代碼中的表格。該代碼適用於事故報告,如果正確完成,應該返回用戶輸入。如果用戶沒有完成這些字段,那麼代碼將返回一條錯誤消息,並允許用戶再次嘗試並填寫報告。在PHP代碼中調用HTML代碼

php代碼當前編寫的方式如果報告不符合要求,我可以返回錯誤消息,但我似乎無法在報告正確填寫時返回用戶輸入列表。

代碼:

<!DOCTYPE html> 
<html lang="en"> 
<!---Main Content---> 
<div id= "content"> 
<h3> Report a Wildlife Road Collison</h3> 
<!---Start of php code---> 

<?php 
    $species1 = $_POST['species']; 
    $species2 = $_POST['txtSpecies']; 

    $reportDate = $_POST['reportDate']; 

    $address = $_POST['address']; 

    $lat = $_POST['lat']; 

    $long = $_POST['long']; 

    $gender = $_POST['rbGender']; 

    $age = $_POST['age']; 

// Checking to see if Other was selected by user. If this is true, the user input will be put in the other field. 

if($species1 === "Other") 
    $species1 = $species2; 

//checking for empty fields 

if(!empty($species1)&& is_string($species1)&& !empty($location)&& is_string($location) && isset($latitude) && is_numeric($latitude) && isset($longitude) && is_numeric($longitude)) 
    { 

?> <!---end of php code---> 

<!---HTML code ---> 
<p>Your Information has been Recorded</p> 
<dl> 
     <dt>Species</dt> 
     <dd><?php print $species1; ?></dd> 

     <dt>Date of Incident</dt> 
     <dd><?php print $reportDate; ?></dd> 

     <dt>Address of Incident</dt> 
     <dd><?php print $address; ?></dd> 

     <dt>Latitude</dt> 
     <dd><?php print $lat; ?></dd> 

     <dt>Longitude</dt> 
     <dd><?php print $long; ?></dd> 

     <dt>Gender</dt> 
     <dd><?php print $gender; ?></dd> 

     <dt>Age</dt> 
     <dd><?php print $age; ?></dd> 


    <!---HTML code complete---> 
</dl> <!---HTML code end---> 

    <!---Start PHP code---> 
<?php 

} 
else{ 
    print "<h4>Sorry</h4>"; 
    print "<p>You didn't fill out the form completely. <a href='index.php?action=report_collision'>Try again?</a></p>"; 
} 


?> 
</div> 
</html> 

我試圖html代碼一部分分配給變量,並調用它在PHP的標籤,但我不明白這是如何做正確。任何幫助,將不勝感激。

+1

所以你總是在'else'中結束? – chris85

+0

請將您的代碼示例簡化爲展示您的問題的最小示例。 – k0pernikus

+0

是的chris85,我知道我應該添加另一個if語句或者elseif? –

回答

1

使用echo而不是打印。 像

<dd><?php print $age; ?></dd> 
0

在你的,如果你是在許多變量,我不能看到你的代碼的任何地方測試條件。

isset($latitude) && is_numeric($latitude) && isset($longitude) && is_numeric($longitude) 

也許$ long而不是$ longitude和$ lat而不是$ latitude?

0

我猜他們在你的,如果條件是錯誤,你是 使用

!empty($location)&& is_string($location) && isset($latitude) && is_numeric($latitude) && isset($longitude) && is_numeric($longitude) 

但您在您的文章獲得

$lat = $_POST['lat']; 

    $long = $_POST['long']; 

我猜你已經錯過了一些變量聲明中創建一個如果其他塊錯誤

或者您在問題中錯誤地粘貼了腳本。否則,你做的方式似乎很好。

1

你即使

if(!empty($species1)&& is_string($species1)&& !empty($location)&& is_string($location) && isset($latitude) && is_numeric($latitude) && isset($longitude) && is_numeric($longitude))

條件檢查方式,僅值之一是空的/沒有設置正確,你在其他結束。你應該做的是單獨檢查每個變量。如果其中任何一個沒有輸出或輸出錯誤,則將它們設置爲默認值並顯示錯誤消息和表格。

短的例子:

$everything_ok = true; 
$species = 'Wrong value'; //default 
if(isset($_POST['species'])) { 
    $species1 = $_POST['species']; 
} else { 
    $everything_ok = false; 
} 

if(!$everything_ok) { 
    //display error 
} 

//Display the table regardless of if everything is ok. $species will display 'Wrong value' 
+0

我明白了你的觀點,但是我希望即使只有一個錯誤字段也會​​返回錯誤,並且只有所有字段都是正確的,才能提供用戶輸入。 –

+1

@蒂凡尼莫里斯:啊,對不起,我誤解了。在這種情況下,我猜想即使整個報告填寫完畢,也沒有正確填寫。緯度/經度可能不是數字(或不被認爲是數字)。可能是因爲空白。但是,將輸入提供給用戶是一種用戶友好的做法。這樣他不必再次填寫所有的價值,但只有「錯誤的」。 –

0

可以在HTML的部分從文件中讀取和回聲出來。例如:

<h2> pure html here </h2> 
<?php 
$myfile = fopen("header.html", "r") or die("Unable to open file!"); 
echo fread($myfile,filesize("header.html")); 
fclose($myfile); 
?> 
0

我想它總是在else語句中,因爲如果條件失敗。檢查你的條件

0

我修改了代碼,通過刪除html標籤並將代碼轉換爲php語法。現在一切正常。感謝所有的輸入。