2017-04-20 160 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语法。现在一切正常。感谢所有的输入。