我在 for 循环中发生了插入,将 4 条记录插入数据库。我只想让这些新记录中的一个从表单中的选择菜单中获取值,该菜单包含在变量 $action 中。该菜单仅针对表单上的一条记录显示,具体取决于用户选择。
表结构为:
CREATE TABLE IF NOT EXISTS `pathway` (
`pathway_pk` int(3) NOT NULL AUTO_INCREMENT,
`case_fk` int(3) NOT NULL,
`level` int(1) NOT NULL,
`pathway_allowed` char(1) NOT NULL DEFAULT 'n',
`pathway_action_fk` int(2) NOT NULL DEFAULT '0',
`comment` text NOT NULL,
`created` datetime NOT NULL,
`created_by` int(3) NOT NULL,
`updated` datetime NOT NULL,
`updated_by` int(3) NOT NULL,
PRIMARY KEY (`pathway_pk`)
)
PHP:
$author_pk = $_GET['author_pk'];
$case_pk = $_GET['case_pk'];
$patient_pk = $_GET['patient_pk'];
$pathway_allowed = intval($_POST['allowed']);
$action = mysql_real_escape_string($_POST['actions']);
if(isset($_POST['submit'])){
$pathway_comment = array();
foreach($_POST['comment'] as $comment) {
$pathway_comment[]= mysql_real_escape_string($comment);
}
for($i=0, $count = count($pathway_comment);$i<$count;$i++) {
$comment = $pathway_comment[$i];
$query = sprintf(
"INSERT INTO pathway (
pathway_pk,
case_fk,
level,
pathway_action_fk,
pathway_allowed,
comment
) VALUES (
'',
'$case_pk',
'1',
'$action',
'%s',
'$comment')", $pathway_allowed === $i ? 'y' : 'n');
$result = mysql_query($query, $connection) or die(mysql_error());
其余的部分:
<script type="text/javascript">
$(document).ready(function() {
$("#jMenu").jMenu();
$("select, input, a.button, button").uniform();
$("input:radio[id='allowed_0']").click(function() {
$("#required_p2").remove();
$("#required_p3").remove();
$("#required_p4").remove();
if($(".p1_action").text().trim()==""){
$(".p1_action").append('<div class="required" id="required_p1"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}
$.getJSON('../scripts/get_pathway_actions.php', function(data){
var html = '';
var len = data.length;
for (var i = 0; i< len; i++) {
html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
}
if($('select#actions option').length == 1){
$('#actions').append(html);
}
});
});
$("input:radio[id='allowed_1']").click(function() {
$("#required_p1").remove();
$("#required_p3").remove();
$("#required_p4").remove();
if($(".p2_action").text().trim()==""){
$(".p2_action").append('<div class="required" id="required_p2"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}
$.getJSON('../scripts/get_pathway_actions.php', function(data){
var html = '';
var len = data.length;
for (var i = 0; i< len; i++) {
html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
}
if($('select#actions option').length == 1){
$('#actions').append(html);
}
});
});
$("input:radio[id='allowed_2']").click(function() {
$("#required_p1").remove();
$("#required_p2").remove();
$("#required_p4").remove();
if($(".p3_action").text().trim()==""){
$(".p3_action").append('<div class="required" id="required_p3"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}
$.getJSON('../scripts/get_pathway_actions.php', function(data){
var html = '';
var len = data.length;
for (var i = 0; i< len; i++) {
html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
}
if($('select#actions option').length == 1){
$('#actions').append(html);
}
});
});
$("input:radio[id='allowed_3']").click(function() {
$("#required_p1").remove();
$("#required_p2").remove();
$("#required_p3").remove();
if($(".p4_action").text().trim()==""){
$(".p4_action").append('<div class="required" id="required_p4"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}
$.getJSON('../scripts/get_pathway_actions.php', function(data){
var html = '';
var len = data.length;
for (var i = 0; i< len; i++) {
html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
}
if($('select#actions option').length == 1){
$('#actions').append(html);
}
});
});
});
function validate(){
valid = true;
if ( document.form.comment_1.value == ""){
alert ( "Please enter a comment for Pathway 1" );
valid = false;
return valid;
}
if ( document.form.comment_2.value == ""){
alert ( "Please enter a comment for Pathway 2" );
valid = false;
return valid;
}
if ( document.form.comment_3.value == ""){
alert ( "Please enter a comment for Pathway 3" );
valid = false;
return valid;
}
if ( document.form.comment_4.value == ""){
alert ( "Please enter a comment for Pathway 4" );
valid = false;
return valid;
}
return valid;
}
function delayer(){
window.location = "create_pathways.php?author_pk=<?php echo $author_pk; ?>&case_pk=<?php echo $case_pk; ?>&patient_pk=<?php echo $patient_pk; ?>"
}
</script>
<body>
<?php include('menu_bar.php'); ?>
<div class="settings">
<div class="buttonn"> <a href="<?php echo $_SERVER['PHP_SELF'], '?logout=true' ?>" class="buttonn">Logout</a> </div>
<div class="buttonn"> <a href="change_password.php?author_pk=<?php echo $author_pk; ?>" class="buttonn">Change Password</a> </div>
</div>
<div class="title">Create Pathways for Level 1<span class="message"><?php echo $message; ?></span></div>
<div class="top_form">
<form action="" method="post" name="form" id="form" onSubmit="return validate();">
<table width="182%" border="0" cellpadding="5">
<tr>
<td valign="top"><strong>Pathway</strong></td>
<td align="center" valign="top"><strong>Allowed</strong></td>
<td colspan="2"><strong>Blocking or Accepting Comment</strong></td>
<td valign="top"> </td>
</tr>
<tr>
<td width="23%" valign="top"><strong>Pathway 1:</strong> No action - See again</td>
<td width="6%" align="center" valign="top"><input type="radio" name="allowed" value="0" id="allowed_0" /></td>
<td colspan="2"><div class="required">
<textarea name="comment[]" id="comment_1" cols="35" rows="5"></textarea>
</div></td>
<td width="33%" align="left" valign="top" class="p1_action"></td>
</tr>
<tr>
<td valign="top"><strong>Pathway 2:</strong> See again</td>
<td align="center" valign="top"><input type="radio" name="allowed" value="1" id="allowed_1" /></td>
<td colspan="2"><div class="required">
<textarea name="comment[]" id="comment_2" cols="35" rows="5"></textarea>
</div></td>
<td width="33%" align="left" valign="top" class="p2_action"></td>
</tr>
<tr>
<td valign="top"><strong>Pathway 3:</strong> Refer to ED</td>
<td align="center" valign="top"><input type="radio" name="allowed" value="2" id="allowed_2" /></td>
<td colspan="2"><div class="required">
<textarea name="comment[]" id="comment_3" cols="35" rows="5"></textarea>
</div></td>
<td width="33%" align="left" valign="top" class="p3_action"></td>
</tr>
<tr>
<td valign="top"><strong>Pathway 4:</strong> Refer to Specialist</td>
<td align="center" valign="top"><input type="radio" name="allowed" value="3" id="allowed_3" /></td>
<td colspan="2"><div class="required">
<textarea name="comment[]" id="comment_4" cols="35" rows="5"></textarea>
</div></td>
<td width="33%" align="left" valign="top" class="p4_action"></td>
</tr>
<tr>
<td valign="top"> </td>
<td align="center" valign="top"> </td>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Create Pathways for Level 1" /></td>
<td> </td>
</tr>
</table>
</form>