• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Newbie PHP help

rumpi

Junior Member
I and taking a class on PHP and am having trouble getting something to work. I am doing a form to POST info and have the php page respond back.

Compose a script that accepts four string variables fed from an html form and returns an html table, with each row displaying the variable's row number, data type, name and value.

I did the form OK, I just have no idea how to get the response back as a table. Any ideas? I am so clueless with XHTML and PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />

<title>Feedback Form</title>

</head>



<body>
<div><p>This is a form:</p>
<form action="response.php" method="post">

<p>Name:<input type="text\"
name=\"name\" size=\"20\" /></p>

<p>Email Address: <input type="text"
name+"email" size="20" /></p>

<p>Date of Birth: <input type="text"
name+"email" size="20" /></p>

<p>Student ID <input type="text"
name+"email" size="20" /></p>

<input type="submit" name="submit"
value="Submit" />




</form>
</div>


</body>
</html>
 
All you have so far is HTML, so I think your class might be missing the php part 🙂. You need to write a server script to handle the form post and write the table to the response. If you want help with that you're going to have to show that you at least know how to get started, and aren't just trolling for answers to homework questions.
 
I finally figured it out. My original form page had errors in it and would not generate a response.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />

<title>Feedback Form</title>

</head>



<body>
<div><p>This is a form:</p>
<form action="lab02moeresponse.php" method="post">

<p>Name:<input type="text"
name="name" size=\"20\" /></p>

<p>Email Address: <input type="text"
name="email" size="20" /></p>

<p>Date of Birth: <input type="text"
name="date" size="20" /></p>

<p>Student ID: <input type="text"
name="id" size="20" /></p>

<input type="submit" name="submit"
value="Submit" />




</form>
</div>


</body>
</html>





And using a tutorial on tables I was fianally able to get the return in the format I needed for the assignment.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />
<title>Returned Response</title>
</head>

<body>

<?php


$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$id = $_POST['id'];
$type1 = gettype($name);
$type2 = gettype($email);
$type3 = gettype($date);
$type4 = gettype($id);
//Compose a script that accepts four string variables fed from an html form and returns an html table, with each row displaying the variable's row number, data type, name and value.

print "<table>
<tr>
<td>1</td>
<td>$type1</td>
<td>Name</td>
<td>$name</td>
</tr>

<tr>
<td>2</td>
<td>$type2</td>
<td>Email</td>
<td>$email</td>
</tr>

<tr>
<td>3</td>
<td>$type3</td>
<td>Date of Birth</td>
<td>$date</td>
</tr>

<tr>
<td>4</td>
<td>$type4</td>
<td>Student ID</td>
<td>$id</td>
</tr>";
?>

</body>

</html>




Now if I could just auto number the rows.
 
I wouldn't do it that way. Here is something I'd probably do if I were you:

PHP:
<?php

function create_rows($array = FALSE) {

$count = 1;

foreach($array as $key => $val) {
echo '<tr ';
echo $odd = ($count & 1) ? 'class="rowA">' : 'class="rowB">'; //I think this is what you wanted :P
echo '<td>' . $count . '</td>';
echo '<td>' . gettype($key). '</td>';
echo '<td>' . htmlspecialchars_decode($key) . '</td>';
echo '<td>' . htmlspecialchars_decode($val) . '</td>';
echo '</tr>';
$count++;
}
}

if(! $_POST) {
$name = '';
$email = '';
$date = '';
$id = '';
}
else {
$input = array(
'Name' => htmlspecialchars($_POST['name']),
'Email' => htmlspecialchars($_POST['email']),
'Date of Birth' => htmlspecialchars($_POST['date']),
'Student ID' => htmlspecialchars($_POST['id'])
);

echo '<html></body><table>';
create_rows($input);
echo '</table></body></html>';
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />

<title>Feedback Form</title>

</head>



<body>
<div><p>This is a form:</p>
<form action="lab02moeresponse.php" method="post">

<p>Name:<input type="text"
name="name" size="20" /></p>

<p>Email Address: <input type="text"
name="email" size="20" /></p>

<p>Date of Birth: <input type="text"
name="date" size="20" /></p>

<p>Student ID <input type="text"
name="id" size="20" /></p>

<input type="submit" name="submit"
value="Submit" />




</form>
</div>


</body>
</html>
 
Last edited:
Hoo, thx for the input. Your did yours with arrays, probably what I should be learning how to do it. My html table got what I needed, but I will be studying yours to see how you did the same thing I did. Your's is certainly the more advanced way of doing it.
 
Hoo, thx for the input. Your did yours with arrays, probably what I should be learning how to do it. My html table got what I needed, but I will be studying yours to see how you did the same thing I did. Your's is certainly the more advanced way of doing it.

No problem. Array's are the lifeblood of PHP so I am sure you'll be learning them soon enough. I just think of array's, specifically associative arrays, as the PHP equivalent to C STRUCTS.

What I posted above would be 2 files. And the first if condition where I check for the post superglobal I'd put a header() function to send it to the form instead of it doing nothing but setting an empty string variables. Or you could output some simple html that adds a meta refresh... or an html page with error message with link to form page. I wrote the first revision of that code when I was using it for one file then I saw you were doing it in 2 so I changed everything but that.
 
Last edited:
Back
Top