php 如何回显 <form> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18564121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to echo a <form> tag
提问by Tyler
Here is some pseudo code
这是一些伪代码
echo "<table class='container'><tr class='stuff'></tr>
<form method='post' action='edit.php' name='edit'>
<tr class='setting' hidden>
... Some inputs and such go here...
<td class='checkbox'>
<input class='cancel' type='button'>
<input class='save' name='update' value='update' type='submit'>
</td></tr></form></table>";
EDIT: Here is the actual code
编辑:这是实际的代码
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if($_SESSION['school'] == "drl") {
$sql = 'SELECT id, student_name, student_id, grad_year, grade, test_name, individual, team_event, member_number, team_number, captain FROM drl';
}
elseif($_SESSION['school'] == "Butler Tech") {
$sql = 'SELECT id, student_name, student_id, grad_year, grade, test_name, individual, team_event, member_number, team_number, captain FROM butlertech';
}
elseif($_SESSION['school'] == "MIT") {
$sql = 'SELECT student_name, student_id, grad_year, grade, test_name, individual, team_event, member_number, team_number, captain FROM mit';
}
mysql_select_db('contestants');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo
"<table class='container'><tr class='stuff'><td hidden>{$row['id']}</td><td>{$row['student_name']}</td></input><td>{$row['student_id']}</td><td class='shcell'>{$row['grade']}</td><td>{$row['test_name']}</td><td>{$row['individual']}</td><td>{$row['team_event']}</td><td class='shcell'>{$row['team_number']}</td><td class='shcell'>{$row['captain']}</td><td class='checkbox' class='odd'> <input class='edit' type='button'> <input class='delete' type='button'> </td></tr>
<tr class='setting' hidden> <form method='post' action='edit.php' name='edit'>
<td><input type='text' name='id' value='"; echo $row['id']; echo "' hidden></input><input id='school' value='"; echo $_SESSION['school']; echo "'name='school' hidden>
<input id='student_name' type='text' value='{$row['student_name']}' name='student_name' /></td>
<td><input id='student_id' type='text' value='{$row['student_id']}' name='student_id' /></td>
<td class='shcell'><select id='grade' name='grade'>
<option value = '7th'"; if($row['grade'] == '7'){ echo 'selected="selected"';} echo ">7th</option>
<option value = '8th'"; if($row['grade'] == '8'){ echo 'selected="selected"';} echo ">8th</option>
<option value = '9th'"; if($row['grade'] == '9'){ echo 'selected="selected"';} echo ">9th</option>
<option value = '10th'"; if($row['grade'] == '10'){ echo 'selected="selected"';} echo ">10th</option>
<option value = '11th'"; if($row['grade'] == '11'){ echo 'selected="selected"';} echo ">11th</option>
<option value = '12th'"; if($row['grade'] == '12'){ echo 'selected="selected"';} echo ">12th</option>
</select></td>
<td><select class='settings' id='test_name' name='test_name'>
<option value = 'None'"; if($row['captain'] == 'None'){ echo 'selected="selected"';} echo ">None</option>
<option value = '(01) Fundamental Accounting'"; if($row['test_name'] == '(01) Fundamental Accounting'){ echo 'selected="selected"';} echo ">(01) Fundamental Accounting</option>
<option value = '(02) Advanced Accounting'"; if($row['test_name'] == '(02) Advanced Accounting'){ echo 'selected="selected"';} echo ">(02) Advanced Accounting</option>
<option value = '(04) Payroll Accounting"; if($row['test_name'] == '(04) Payroll Accounting'){ echo 'selected="selected"';} echo "'>(04) Payroll Accounting</option>
<option value = '(12) Financial Math & Analysis'"; if($row['test_name'] == '(12) Financial Math & Analysis'){ echo 'selected="selected"';} echo ">(12) Financial Math & Analysis</option>
<option value = '(13) Insurance Concepts'"; if($row['test_name'] == '(13) Insurance Concepts'){ echo 'selected="selected"';} echo ">(13) Insurance Concepts</option>
<option value = '(14) Federal Income Tax Accounting'"; if($row['test_name'] == '(14) Federal Income Tax Accounting'){ echo 'selected="selected"';} echo ">(14) Federal Income Tax Accounting</option>
<option value = '(20) Keyboarding Production'"; if($row['test_name'] == '(20) Keyboarding Production'){ echo 'selected="selected"';} echo ">(20) Keyboarding Production</option>
<option value = '(46) Fundamentals of Web Design'"; if($row['test_name'] == '(46) Fundamentals of Web Design'){ echo 'selected="selected"';} echo ">(46) Fundamentals of Web Design</option>
<option value = '(47) PC Servicing & Troubleshooting'"; if($row['test_name'] == '(47) PC Servicing & Troubleshooting'){ echo 'selected="selected"';} echo ">(47) PC Servicing & Troubleshooting</option>
<option value = '(48) Computer Security'"; if($row['test_name'] == '(48) Computer Security'){ echo 'selected="selected"';} echo ">(48) Computer Security</option>
</select></td>
<td><select class='settings' name='individual' value='{$row['individual']}'>
<option value = 'None'"; if($row['individual'] == 'None'){ echo 'selected="selected"';} echo ">None</option>
<option value = '(03) Computerized Accounting'"; if($row['individual'] == '(03) Computerized Accounting'){ echo 'selected="selected"';} echo ">(03) Computerized Accounting</option>
<option value = '(06) Fundamental Spreadsheet Applications'"; if($row['individual'] == '(06) Fundamental Spreadsheet Applications'){ echo 'selected="selected"';} echo ">(06) Fundamental Spreadsheet Applications</option>
<option value = '(07) Advanced Spreadsheet Applications'"; if($row['individual'] == '(07) Advanced Spreadsheet Applications'){ echo 'selected="selected"';} echo ">(07) Advanced Spreadsheet Applications</option>
<option value = '(08) Banking & Finance'"; if($row['individual'] == '(08) Banking & Finance'){ echo 'selected="selected"';} echo ">(08) Banking & Finance</option>
<option value = '(10) Economic Research Project Individual'"; if($row['individual'] == '(10) Economic Research Project Individual'){ echo 'selected="selected"';} echo ">(10) Economic Research Project Individual</option>
<option value = '(21) Fundamental Word Processing Skills'"; if($row['individual'] == '(21) Fundamental Word Processing Skills'){ echo 'selected="selected"';} echo ">(21) Fundamental Word Processing Skills</option>
<option value = '(22) Advanced Word Processing Skills'"; if($row['individual'] == '(22) Advanced Word Processing Skills'){ echo 'selected="selected"';} echo ">(22) Advanced Word Processing Skills</option>
<option value = '(31) Administrative Support Research Project Individual'"; if($row['individual'] == '(31) Administrative Support Research Project Individual'){ echo 'selected="selected"';} echo ">(31) Administrative Support Research Project Individual</option>
<option value = '(64) Interview Skills'"; if($row['individual'] == '(64) Interview Skills'){ echo 'selected="selected"';} echo ">(64) Interview Skills</option>
<option value = '(69) Presentation Management Individual'"; if($row['individual'] == '(69) Presentation Management Individual'){ echo 'selected="selected"';} echo ">(69) Presentation Management Individual</option>
</select></td>
<td><select class='settings' name='team_event' value='{$row['team_event']}'>
<option value = 'None'"; if($row['team_event'] == 'None'){ echo 'selected="selected"';} echo ">None</option>
<option value = '(09) Financial Analyst Team'"; if($row['team_event'] == '(09) Financial Analyst Team'){ echo 'selected="selected"';} echo ">(09) Financial Analyst Team</option>
<option value = '(11) Economic Research Project Team'"; if($row['team_event'] == '(11) Economic Research Project Team'){ echo 'selected="selected"';} echo ">(11) Economic Research Project Team</option>
<option value = '(30) Administrative Support Team'"; if($row['team_event'] == '(30) Administrative Support Team'){ echo 'selected="selected"';} echo ">(30) Administrative Support Team</option>
<option value = '(50) Computer Animation Team'"; if($row['team_event'] == '(50) Computer Animation Team'){ echo 'selected="selected"';} echo ">(50) Computer Animation Team</option>
<option value = '(51) Network Design Team'"; if($row['team_event'] == '(51) Network Design Team'){ echo 'selected="selected"';} echo ">(51) Network Design Team</option>
<option value = '(53) Video Production Team'"; if($row['team_event'] == '(53) Video Production Team'){ echo 'selected="selected"';} echo ">(53) Video Production Team</option>
<option value = '(55) Web Site Design Team'"; if($row['team_event'] == '(55) Web Site Design Team'){ echo 'selected="selected"';} echo ">(55) Web Site Design Team</option>
<option value = '(57) Broadcast News Production Team'"; if($row['team_event'] == '(57) Broadcast News Production Team'){ echo 'selected="selected"';} echo ">(57) Broadcast News Production Team</option>
<option value = '(62) Small Business Management Team'"; if($row['team_event'] == '(62) Small Business Management Team'){ echo 'selected="selected"';} echo ">(62) Small Business Management Team</option>
<option value = '(70) Presentation Management Team'"; if($row['team_event'] == '(70) Presentation Management Team'){ echo 'selected="selected"';} echo ">(70) Presentation Management Team</option>
</select></td>
<td class='shcell'><input id='team_number' name='team_number' min='1' value='{$row['team_number']}' class='number' type='number' /></td>
<td class='shcell'><select id='captain' name='captain'>
<option value = '-'"; if($row['captain'] == '-'){ echo 'selected="selected"';} echo ">-</option>
<option value = 'No'"; if($row['captain'] == 'No'){ echo 'selected="selected"';} echo ">No</option>
<option value = 'Yes'"; if($row['captain'] == 'Yes'){ echo 'selected="selected"';} echo ">Yes</option>
</select></td>
<td class='checkbox'>
<input class='cancel' type='button'>
<input class='save' name='update' value='update' type='submit'></td></form></tr></table>";}
mysql_close($conn);
?>
Everything works as intended, except for the form part. As you can see in the picture, the form is being echoed before everything else. What I want is for <tr class='setting' hidden></tr>
to be inside the form.
除了表单部分,一切都按预期工作。正如您在图片中看到的那样,表单在其他一切之前得到了回应。我想要的是<tr class='setting' hidden></tr>
在表单内。
回答by Charaf JRA
Change your code so that <form>
is before<table>
not inside it:
更改您的代码,以便<form>
为之前<table>
没有在它里面:
echo "<form method='post' action='edit.php' name='edit'>
<table class='container'>
<tr class='stuff'></tr>
<tr class='setting' hidden>
... Some inputs and such go here...
<td class='checkbox'>
<input class='cancel' type='button'>
<input class='save' name='update' value='update' type='submit'>
</td></tr>
</table>
</form>";
回答by Charaf JRA
When you're using <form>
with <table>
you need to either put the <form>
tags completely outside the <table>
, or have the entire <form>
inside one <td>
. Any other structure breaks the syntax of the <table>
and will be ignored by the browser, or rendered incorrectly.
当你使用<form>
与<table>
你需要要么把该<form>
标签完全不在<table>
,或对整个<form>
内一个<td>
。任何其他结构都会破坏 的语法,<table>
并且会被浏览器忽略或错误呈现。
Valid HTML:
有效的 HTML:
<form>
<table>
<tr>
<td>
<input name="data" type="text">
<input name="moredata" type="text">
</td>
<td>
<input name="yetmoredata" type="text">
</td>
</tr>
</table>
</form>
Alternatively
或者
<table>
<tr>
<td>
<form>
<input name="data" type="text">
<input name="moredata" type="text">
</form>
</td>
</tr>
</table>
回答by Havenard
Code you place in between table rows without specificing a cell to contain it, will end messed up, probably pushed outside the table body. Its no place to put anything.
您在表格行之间放置的代码没有指定包含它的单元格,最终会搞砸,可能会被推到表格主体之外。它没有地方放任何东西。
You can either put the form inside a cell or put the whole table inside the form. This thing you are doing makes no sense, thats why the browser is ignoring it.
您可以将表单放在一个单元格中,也可以将整个表格放在表单中。你做的这件事毫无意义,这就是浏览器忽略它的原因。
If the form is ruining your formatting, create a CSS style for form { padding: 0; margin: 0 }
如果表单破坏了您的格式,请为 form { padding: 0; margin: 0 }