Tiffany Morris
Tiffany Morris

Reputation: 323

Calling HTML code within PHP code

I have the following .PHP code that contains a table in the HTML tags. The code is for an accident report that should return the users input if completed correctly. If the user does not complete the fields, then the code will return an error message and allow the user to try and fill out the report again.

The way the php code is currently written I am able to return the error message if the report doesn't meet the requirements, but I can't seem to return the list of users input when the report is filled out correctly.

Code:

<!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>

I have attempted to assign the html code portion to a variable and call it in the php tag, but I am not clear how that is done correctly. Any help would be appreciated.

Upvotes: 0

Views: 1023

Answers (7)

Tiffany Morris
Tiffany Morris

Reputation: 323

I modified the code by removing the html tag and converting the code to php syntax. Everything works now. Thanks for all of the input.

Upvotes: 0

Zohra Gadiwala
Zohra Gadiwala

Reputation: 216

I guess it always go in else statement because your if condition fails. Check your if condition

Upvotes: 0

blue-tree-frog
blue-tree-frog

Reputation: 1

You can read in a section of HTML from a file and echo it out. For example:

<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);
?>

Upvotes: 0

Viliam Aboši
Viliam Aboši

Reputation: 447

You have a condition checking for

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

That way, even if only one of the values is empty/not set properly, you end up in else. What you should rather do is check for each of the variables individually. If any of them has none or wrong output, you set them to default and display both the error message and table.

Short example:

$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'

Upvotes: 1

Sagar
Sagar

Reputation: 493

I guess their is error in your if condition , you are using

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

but you are getting in your post

$lat = $_POST['lat']; 

  $long = $_POST['long'];

I guess you have missed some variable declaration in created a wrong if else block

or you have pasted your script incorrectly in the question. Otherwise the way you have done it seems fine.

Upvotes: 0

Oussama Ben Ghorbel
Oussama Ben Ghorbel

Reputation: 2119

In your if condition you are testing on many variables that I cannot see anywhere in your code.

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

Maybe $long instead of $longitude and $lat instead of $latitude?

Upvotes: 0

Faisal Ahmed
Faisal Ahmed

Reputation: 11

use echo instead of print. like

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

Upvotes: 1

Related Questions