Page 1 of 1

Submit problem with my PHP code ?

Posted: Tue Mar 01, 2011 9:20 pm
by shellee
Hello, '

I am an extreme noob in PHP but I would very much like to improve my skills. The problem that I am having is that when I create an HTML page that is linked to a PHP page and I click the submit button it never gives an error message it goes to the PHP code in my web browser when I open it under local host. Is this normal? Am I doing something wrong? If it is wrong how do I fix it? Here is the code from the HTML FILE & PHP FILE: (The HTML file is a work in progress I am trying to figure out the best way to write the validation code)

//HTML

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Company Database</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>Please enter the following details for Company XYZ</h2>
<form method="post" action="EmployeeData.php">

<p>Employee Name <input type="text" name="employee_name" /></p>
<p>Age <input type="text" name="age" /></p>
<p>Employee Address <input type="text" name="employee_address" /></p><br /><br />

<p>Language Spoken<br /><br />
<select name="listbox" size="3">
<option value="language_spoken" selected>English</option>
<option value="language_spoken">Spanish</option>
<option value="language_spoken">French</option>
</select>
</p>

<p>Is Employee Currently Married?</p><br />
<input type="radio" name="is_married" value="Yes" />Yes<br />
<input type="radio" name="is_married" value="No" />No<br />

<p><input type="submit" value="Submit" />
<p><input type="submit" value="Search Database" />

</form>

<p><a href="SubmittedEmployeeInformation.php"></a></p>
</body>
</html>
//HTML

//PHP

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Employee Data Input</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<?php
    echo 'employee name = '. $_POST['employee_name'].'<br />';
    echo 'employee address = '. $_POST['employee_address'].'<br />';
    echo 'employee age = '. $_POST['age'].'<br />';
    echo 'language spoken = '. $_POST['language_spoken'].'<br />';
    echo 'is married = '. $_POST['is_married'].'<br />';
die(); 

if(isset($_POST['Submit'])) {
   $link = mysqli_connect('localhost', 'root', 'seether7', 'company_db');
   if (mysqli_connect_errno()) {
      echo '<p>Cannot connect to DB: ' . mysqli_connect_error() . '</p>'; 
   }    
      if('age' >= 16){
          echo "<p style=\"color:red;\">Age not valid! Please retry.</p>";
   } 
      
} else { 
          
$EName = $_POST['employee_name'];
$EAddress = $_POST['employee_address'];
$EAge = $_POST['age'];
$ELanguage = $_POST['language_spoken'];
$EMarried = $_POST['is_married'];  

$query = sprintf("INSERT INTO `employee_data`
        (`employee_name`, `employee_address`, `age`, `languge_spoken`, `is_married`)
        VALUES
        ('%s', '%s', '%s', '%s', '%s')", $EName, $EAddress, $EAge, $ELanguage, $EMarried);  

        $result= mysqli_query($link, $query);        
}



      
if (!$result) {
   echo '<p>Unable to query the database: ' . mysqli_error($link) . ' .</p>';
} else {
   echo '<p>Successfully Added!</p>';
}           

mysqli_close($link);

?>
</body>
</html>
//PHP

Thanks in advance to all who respond.

Re: Submit problem with my PHP code ?

Posted: Tue Mar 01, 2011 10:14 pm
by Jonah Bron
If you can see the PHP while viewing the file through your localhost, you don't have PHP installed properly. Are you sure you installed it? It' a lot easier to just install the WAMP stack.

http://www.wampserver.com/