您好,我将不胜感激任何帮助或建议。我有一个webform,其中有一个部分"作者"。这可以复制多达5次。复制后表格ID更改。这是一个有效的例子:https://jsfiddle.net/sdeg4nps/。当只有一个作者时,很容易从表单传递数据,但是当有多个作者时如何实现插入作者。我有5个sql查询,当没有作者时,它会推送NULL。我想在表单填充作者凭证时仅存储数据。
简单的sql:
$author = "INSERT INTO Author (AuthorTitle, F_Name, L_Name, Email, Contact) VALUES ('$AuthorTitle','$F_Name','$L_Name','$Email','$Contactauthor')";
$author2 = "INSERT INTO Author (AuthorTitle, F_Name, L_Name, Email, Contact) VALUES ('$AuthorTitle2','$F_Name2','$L_Name2','$Email2','$Contactauthor2')";
var_dump($ _ POST)的结果是:
array(20) { ["Title"]=> string(0) "" ["Summary"]=> string(0) "" ["WhatDid"]=> string(0) "" ["Other_theme_text"]=> string(0) "" ["WhyDid"]=> string(0) "" ["WhatWell"]=> string(0) "" ["WhatDifferently"]=> string(0) "" ["Scalability"]=> string(0) "" ["FurtherInfo"]=> string(0) "" ["Location"]=> string(0) "" ["title"]=> string(0) "" ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["email"]=> string(0) "" ["titleID2"]=> string(0) "" ["first_name_ID2"]=> string(0) "" ["last_name_ID2"]=> string(0) "" ["email_ID2"]=> string(0) "" ["engagement"]=> string(0) "" ["submit"]=> string(6) "Submit" }
我想这将是if else或某种循环来检查表单字段是否有一些输入。但我不知道从哪里开始。
谢谢
答案 0 :(得分:0)
像这样构建你的表单,它在你的服务器端更具结构性和可遍历性:
$(function() {
$(document).on('click', '[name="btnAdd"]', function() {
console.log(1111);
$('[name="btnAdd"]').css('display', 'none');
var num = $('.clonedInput').length, // Checks to see how many "duplicatable" input fields we currently have
newNum = new Number(num + 1), // The numeric ID of the new input field being added, increasing by 1 each time
newElem = $('#author' + num).clone().attr('id', 'author' + newNum).fadeIn('slow');
newElem.find('.label1').attr('id', 'author_' + 'ID' + newNum).attr('name', 'author_' + 'ID' + newNum).html('Author' + newNum);
// Title - select
newElem.find('.input_title').attr('id', 'title' + 'ID' + newNum).attr('name', 'data[authors][' + newNum + '][title]').val('');
// First name - text
newElem.find('.input_fn').attr('id', 'first_name_' + 'ID' + newNum).attr('name', 'data[authors][' + newNum + '][first_name]').val('');
// Last name - text
newElem.find('.input_ln').attr('id', 'last_name_' + 'ID' + newNum).attr('name', 'data[authors][' + newNum + '][last_name]').val('');
// Email - text
newElem.find('.input_email').attr('id', 'email_address_' + 'ID' + newNum).attr('name', 'data[authors][' + newNum + '][email]').val('');
$('[name="btnDel"]').css('display', 'none');
newElem.find('[name="btnDel"]').css('display', 'inline');
newElem.find('[name="btnDel"]').attr('id', 'btnDel' + num);
newElem.find('[name="btnAdd"]').css('display', 'inline');
newElem.find('[name="btnAdd"]').attr('id', 'btnAdd' + num);
$('#author' + num).after(newElem);
$('#ID' + newNum + '_title').focus();
$('[name="btnAdd"]').css('display', 'none');
// Right now you can only add 4 sections, for a total of 5. Change '5' below to the max number of sections you want to allow.
if (newNum == 5)
$('[name="btnAdd"]').attr('disabled', true);
$('#btnAdd' + num).css('display', 'inline');
// value here updates the text in the 'add' button when the limit is reached
});
$(document).on('click', '[name="btnDel"]', function() {
// Confirmation dialog box. Works on all desktop browsers and iPhone.
if (confirm("Are you sure you wish to remove this section? This cannot be undone.")) {
var num = $('.clonedInput').index();
var count = $('.clonedInput').length;
// if only one element remains, disable the "remove" button
$(this).parents('.clonedInput').remove();
console.log(count);
var newCount = $('.clonedInput').length;
if (count - 1 === 1) {
$('#btnDel').css('display', 'none');
$('#btnAdd').css('display', 'inline')
} else {
$('#btnDel' + (newCount - 1)).css('display', 'inline');
}
$('[name="btnAdd"]').attr('disabled', false);
$('#btnAdd' + (newCount - 1)).css('display', 'inline');
// enable the "add" button
}
return false; // Removes the last section you added
});
// Enable the "add" button
$('#btnAdd').attr('disabled', false);
// Disable the "remove" button
$('#btnDel').css('display', 'none');
});

.main {
margin-top: 1%;
background: #f8f8f8;
padding: 4%;
margin-bottom: 100px;
/*border: 1px solid red;*/
}
.label1 {
font-family: sans-serif;
font-size: 13px;
font-weight: bold;
min-width: 96%;
max-width: 96%;
padding: 5px;
/*margin: 1%;*/
/*border: 1px solid black;*/
}
.help-block,
.help-block-title,
.help-block-fn,
.help-block-ln,
.help-block-email,
.help-block-checkbox {
font-family: sans-serif;
font-size: 11px;
color: #AAA;
min-width: 75%;
max-width: 75%;
padding: 5px;
margin-top: 1px;
margin-left: 0;
/*margin-left: 1%;*/
/*border: 1px solid green;*/
}
.help-block-checkbox {
margin-top: 10px;
}
textarea {
font-family: sans-serif;
min-height: 100px;
min-width: 100%;
max-width: 100%;
padding: 5px;
background: #EEEEEE;
border-radius: 4px;
/*border-top-right-radius: 4px;*/
border-style: inset;
border: 1px solid #d1d1d1;
}
.singleline1 {
/* this is used for textfield expanded when checkbox other is checked */
font-family: sans-serif;
position: relative;
left: 5%;
width: 50%;
padding: 5px;
background: #EEEEEE;
border-radius: 4px;
border-style: inset;
border: 1px solid #d1d1d1;
}
.submit {
position: relative;
left: 47%;
bottom: 30%;
}
.checkbox {
position: relative;
font-family: sans-serif;
font-size: 12px;
line-height: 2em;
padding: 5px;
margin-left: 15px;
*/
/*border: 1px solid black;*/
}
.textboxstyle {
font-family: sans-serif;
padding: 5px;
width: 100%;
background: #EEEEEE;
border-radius: 4px;
border-style: inset;
border: 1px solid #d1d1d1;
}
.checboxauthor {
position: relative;
padding-top: 11px;
/*to be deleted*/
width: 2%;
height: 60px;
}
.boxtitle {
position: relative;
float: left;
padding-right: 10px;
padding-bottom: 5px;
/*margin-right: 3px;*/
margin: 0;
height: 50px;
width: 6%;
/*border: 1px solid red;*/
}
.date {
font-family: sans-serif;
padding: 5px;
width: auto;
background: #EEEEEE;
border-radius: 4px;
border-style: inset;
border: 1px solid #d1d1d1;
}
.prg-text {
font-family: sans-serif;
display: inline;
/*font-size: 10px;*/
font-weight: bolder;
}
.clonedInput + .clonedInput {
/*border-top: 2px solid #2a2a2a;*/
padding-top: 20px;
}
#btnAdd,
#btnDel {
color: #fff;
padding: 3px 18px 5px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
-moz-box-shadow: 0 1px 2px rgba(000, 000, 000, 0.5), inset 0 0 0 rgba(255, 255, 255, 0.9);
-webkit-box-shadow: 0 1px 2px rgba(000, 000, 000, 0.5), inset 0 0 0 rgba(255, 255, 255, 0.9);
box-shadow: 0 1px 2px rgba(000, 000, 000, 0.5), inset 0 0 0 rgba(255, 255, 255, 0.9);
}

<div id="author1" class="clonedInput">
<p id="author" name="author" class="label1">Author</p>
<div class="row">
<div class="col-md-1 col-sm-6 col-xs-4">
<input id="title" name="data[authors][0][title]" type="text" placeholder="" class="input_title textboxstyle" required="">
<p id="help-block-title" name="help-block-title" for="title" class="help-block-title">Title</p>
</div>
<div class="col-md-2">
<input id="first_name" name="data[authors][0][first_name]" type="text" placeholder="" class="input_fn textboxstyle" required="">
<p id="help-block-fn" name="help-block-fn" for="fn" class="help-block-fn">First Name</p>
</div>
<div class="col-md-2">
<input id="last_name" name="data[authors][0][last_name]" type="text" placeholder="" class="input_ln textboxstyle" required="">
<p id="help-block-ln" name="help-block-ln" for="ln" class="help-block-ln">Last Name</p>
</div>
<div class="col-md-2">
<input id="email" name="data[authors][0][email]" type="email" placeholder="abc@example.com" class="input_email textboxstyle" required="">
<p id="help-block-email" name="help-block-email" for="email" class="help-block-email">Email</p>
</div>
<div class="col-md-1 col-sm-1 col-xs-2">
<input id="checkbox" name="data[authors][0][contact]" type="checkbox" value="YES">
<p id="help-block-checkbox" name="help-block-checkbox" for="checkbox" class="help-block-checkbox">Contact</p>
</div>
<div class="col-md-2 col-sm-2 col-xs-4">
<button id="btnAdd" name="btnAdd" type="button" class="btn btn-info">Add author</button>
</div>
<div class="col-md-2 col-sm-2 col-xs-3">
<button id="btnDel" name="btnDel" type="button" class="btn btn-danger">Remove author</button>
</div>
</div>
</div>
&#13;
您的PHP脚本
$data = $_POST['data'];
foreach($data['authors'] as $author){
$query = "INSERT INTO Author (AuthorTitle, F_Name, L_Name, Email, Contact) VALUES ('".$author['title']."','".$author['first_name']."','".$author['last_name']."','".$author['email']."','".$author['contact']."')";
// your other code
}
上面的代码应该可以正常工作......希望这会对你有帮助......
P.S。:我还没有预防sql注射,因为这是一个简单的例子,你可以继续...