簡體   English   中英

在PHP代碼中調用HTML代碼

[英]Calling HTML code within PHP code

我有以下.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標記中調用它,但是我不清楚如何正確完成。 任何幫助,將不勝感激。

使用echo而不是print。 喜歡

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

您有條件檢查

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'

在您的if條件下,您正在測試許多在代碼中任何地方都看不到的變量。

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

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

我猜他們在您的if條件中是錯誤的,您正在使用

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

但是你在發帖

$lat = $_POST['lat']; 

  $long = $_POST['long'];

我想你在創建錯誤的if else塊時錯過了一些變量聲明

或您在問題中錯誤地粘貼了腳本。 否則,您的操作方式看起來不錯。

您可以從文件中讀取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);
?>

我猜它總是放在else語句中,因為您的if條件失敗。 檢查您的狀況

我通過刪除html標記並將代碼轉換為php語法來修改了代碼。 現在一切正常。 感謝您的所有投入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM