Javascript 动态Js文本框-MySQL
我已经创建了可以由用户添加或删除的动态文本框,我想将用户输入的任何内容直接发送到数据库。此时,它似乎只发送第一个文本框中的内容,而忽略其余内容 我需要的是-对于输入的每个品牌名称,将brandName、brandCategory、brandKeyword作为新行发送到数据库中 对我能做什么有什么建议吗Javascript 动态Js文本框-MySQL,javascript,php,mysql,Javascript,Php,Mysql,我已经创建了可以由用户添加或删除的动态文本框,我想将用户输入的任何内容直接发送到数据库。此时,它似乎只发送第一个文本框中的内容,而忽略其余内容 我需要的是-对于输入的每个品牌名称,将brandName、brandCategory、brandKeyword作为新行发送到数据库中 对我能做什么有什么建议吗 <form id="manage_brands" action="add_brands.php" method="post"> <form role="form" met
<form id="manage_brands" action="add_brands.php" method="post">
<form role="form" method="post">
<h2>Add Brand Information</h2>
<p class="text-box">
<label for="box1">Brand Name:<span class="box-number">1</span></label>
<input type="text" class="textbox" name="brandName[]" value="" id="brandName" />
<a class="add-box" href="#"><img id="icon2" src="images/plus.png"></a>
</p>
<p>
<label for="bCategory">Brand Category:</label>
<select class="textbox" id="bCategory" name="brandCategory">
<option value="">Select...</option>
<option value="Arts and Entertainment">Arts and Entertainment</option>
<option value="Automotive">Automotive</option>
<option value="Business">Business</option>
<option value="Careers">Careers</option>
<option value="Education">Education</option>
<option value="Family and Parenting">Family and Parenting</option>
<option value="Health and Fitness">Health and Fitness</option>
<option value="Food and Drink">Food and Drink</option>
<option value="Hobbies and Interests">Hobbies and Interests</option>
<option value="Home and Garden">Home and Garden</option>
<option value="Law, Government and Politics">Law, Government and Politics</option>
<option value="News">News</option>
<option value="Personal Finance">Personal Finance</option>
<option value="Pets">Pets</option>
<option value="Society">Society</option>
<option value="Science">Science</option>
<option value="Shopping">Shopping</option>
<option value="Sports">Sports</option>
<option value="Style and Fashion">Style and Fashion</option>
<option value="Technology and Computing">Technology and Computing</option>
<option value="Travel">Travel</option>
<option value="Real Estate">Real Estate</option>
<option value="Religion and Spirituality">Religion and Spirituality</option>
</select>
</p>
<p>
<label for="brandKeyword">Brand Keyword:</label>
<textarea class="FormElement" name="brandKeyword" id="brandKeyword" placeholder="Add relevant keywords"></textarea>
</p>
<br />
<input type="submit" class="button" name="submit" value="Store Brand" onclick="return confirm('Do you wish to add a new brand?');"/>
 
<a href="home.php" class="link">Cancel</a>
</form>
添加品牌信息
品牌名称:1
品牌类别:
选择
艺术与娱乐
汽车
商业
事业
教育
家庭与养育
健康和健身
饮食
兴趣爱好
家与花园
法律、政府和政治
消息
个人理财
宠物
社会
科学
购物
体育
风格与时尚
技术与计算
长途行走
房地产
宗教与灵性
品牌关键词:
 
PHP
if(空($errors)){
//执行创建
$brandName=mysql_prep($_POST[“brandName[]”);
$brandCategory=mysql_prep($_POST[“brandCategory]”);
$brandKeyword=mysql_prep($_POST[“brandKeyword”]);
$addedBy=mysql_prep($_SESSION['username']);
$keyword=数组($brandKeyword);
$count=计数($keyword);
//内爆-要连接到字符串的数组
//逗号是每个字符串之间的粘合字符串/使用定义的参数“,”将数组的内容显示为字符串
对于($i=0;$i<$count;$i++){
$brandKeyword=内爆(“,”,$keyword);
}
$query=“插入品牌(”;
$query.=“品牌名称、品牌类别、品牌关键词、addedBy”;
$query.=”)值(“;
$query.=“{$brandName}'、{$brandCategory}'、{$brandKeyword}'、{$addedBy}';
$query.=”;
$result=mysqli\u查询($connection,$query);
如果($结果){
//成功
$\会话[“消息”]=“品牌已存储”;
将_重定向到(“add_brands.php”);
}否则{
//失败
$\u会话[“消息”]=“无法存储信息”;
}
Javascript
jQuery(document).ready(function($){
$('#manage_brands .add-box').click(function(){
var n = $('.text-box').length + 1;
var box_html = $('<p class="text-box"><label for="box' + n + '">Brand Keyword <span class="box-number">' +
'' + n + '</span></label> <input type="text" class="textbox" name="brandName[]" value="" id="box' + n + '" />' +
' <a href="#" class="remove-box"><img id="icon2" src="images/minus.png"></a></p>');
box_html.hide();
$('#manage_brands p.text-box:last').after(box_html);
box_html.fadeIn('slow');
return false;
});
$('#manage_brands').on('click', '.remove-box', function(){
$(this).parent().css( 'background-color', '#ffffff' );
$(this).parent().fadeOut("slow", function() {
$(this).remove();
$('.box-number').each(function(index){
$(this).text( index + 1 );
});
});
return false;
});
});
jQuery(文档).ready(函数($){
$(“#管理品牌。添加框”)。单击(函数(){
var n=$('.text box')。长度+1;
var box_html=$(“品牌关键字”+
“+n+”+
“
”);
box_html.hide();
$(“#manage_brands p.text-box:last”)。在(box_html)之后;
box_html.fadeIn('slow');
返回false;
});
$(“#管理品牌”)。在('单击','删除框',函数()上{
$(this.parent().css('background color','#ffffff');
$(this.parent().fadeOut(“slow”,function()){
$(this.remove();
$('.box number')。每个(函数(索引){
$(此).text(索引+1);
});
});
返回false;
});
});
name=“yourInputName[]”
-PHP将收到一个数组。@NiettheDarkAbsol Absol我已经尝试过了,但仍然不起作用,我在JS、PHP和HTML中对其进行了更改。HTML中有一个名为“brandKeyword”的文本区域,在Javascript中创建的文本框也被命名为“brandKeyword”.PHP将只获得其中一个。如果用户仍然可以为关键字添加文本框,那么“brandKeyword”文本区域的用途是什么?----您刚刚编辑了文章并将Javascript文本框更改为使用“brandName”而不是“brandKeyword”,但是,您的HTML中已经有一个文本框具有“brandName”所有新创建的文本框也将具有相同的名称。@BlahArgh我看到了,我已经更改了它,但仍然不起作用。@Kate我不知道你是否看到了我对上一条回复的更新。你回复得太快了,我想我应该更新我的原始评论。我可能应该再创建一条。所以关于这一点,我仍然不能完全确定他们在这个网站上是如何处理的。
jQuery(document).ready(function($){
$('#manage_brands .add-box').click(function(){
var n = $('.text-box').length + 1;
var box_html = $('<p class="text-box"><label for="box' + n + '">Brand Keyword <span class="box-number">' +
'' + n + '</span></label> <input type="text" class="textbox" name="brandName[]" value="" id="box' + n + '" />' +
' <a href="#" class="remove-box"><img id="icon2" src="images/minus.png"></a></p>');
box_html.hide();
$('#manage_brands p.text-box:last').after(box_html);
box_html.fadeIn('slow');
return false;
});
$('#manage_brands').on('click', '.remove-box', function(){
$(this).parent().css( 'background-color', '#ffffff' );
$(this).parent().fadeOut("slow", function() {
$(this).remove();
$('.box-number').each(function(index){
$(this).text( index + 1 );
});
});
return false;
});
});