I have a form with a textbox. If I enter number 3 for example in that textbox then 3 textboxes appear that I've made with Javascript..like this:
我有一个文本框。如果我输入数字3,比如在那个文本框中,我用Javascript做了三个文本框。是这样的:
this is the html code:
这是html代码:
<span>Cate intrebari va contine chestionarul?</span>
<input type='text' id='nrintrebari'/>
</td>
<td>
<input type='button' value='Creaza intrebari' onclick='generate()'/>
<input type='button' value='Save' id='btn_intrebari'/>
</td>
</tr>
</table>
<br><br>
<div id='sim'>
</div>
and this is the javascript code that creates the textboxes:
这是创建文本框的javascript代码:
var a=0;
function generate()
{
var tot = document.getElementById("nrintrebari").value;
var tbl = document.getElementById("sim");
for(var i =1;i<=tot;i++)
{
a++;
tbl.innerHTML = tbl.innerHTML +'Intrebare nr.'+ a +' <input type="text" size="100" maxlength= "200" name="intrebare[]" style="height:30px; background-color:#B8B8B8; " > <br><br><br> ';
}
}
If I want to pass the data from the textboxes to a php file I'll do a foreach like this:
如果我想将数据从文本框传递到php文件,我将这样做:
foreach($_POST['intrebare'] AS $textbox)
{
$sql="INSERT INTO intrebari values ('','".$_SESSION['cod']."','$textbox','1')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
}
All is well but this is done with refresh of the page. I want to do this without refreshing the page so I've decided to use Ajax. Like this:
一切都很好,但这是通过刷新页面完成的。我想在不刷新页面的情况下这么做,所以我决定使用Ajax。是这样的:
$(document).ready(function () {
$('#btn').click(function () {
$.ajax({
type: "POST",
url: 'pag1.php',
data: "intrebare[]=" + $('#intrebare[]').val(),
success: function (data) {
$('#status').html(data);
}
});
});
});
I'm using the name of the textbox that i named and declared as an array in the Javascript code: intrebare[] but when I hit the button nothing happens. If I declare the textbox simple, not like an array as intrebare then the value is passed. How can I send the data through Ajax like an array?
我正在使用我在Javascript代码中命名并声明为数组的文本框的名称,但当我按下按钮时,什么也没有发生。如果我声明文本框很简单,而不是像无畏的数组那样,那么这个值就会被传递。如何像数组一样通过Ajax发送数据?
3 个解决方案
#1
0
do not use "intrebare[] ...
不要使用“无畏的……”
u can only use "intrebare"
你只能使用“无畏”
Also another way is var str = $("form").serialize(); that will get the values from the form, if u want to use a form.
另一种方法是var str = $("form").serialize();这将从表单得到值,如果你想使用表单。
#2
0
you can not use a name as "#whatevername" Use ID of the control for this. and given you can not have controls with same ID (we can, but not recommended) you can give a class to all the text boxes and get all the values from all the controls.
你不能用“#whatevername”的名称来使用这个控件的ID。如果您不能拥有相同ID的控件(我们可以,但不推荐),您可以向所有文本框提供一个类,并从所有控件中获取所有值。
var a = 0;
function generate() {
var tot = $("#nrintrebari").val();
var tbl = $("#sim");
for (var i = 1; i <= tot; i++) {
a++;
tbl.append('Intrebare nr.' + a + '<input type="text" size="100" maxlength= "200" class="intrebare" style="height:30px; background-color:#B8B8B8; " >');
}
}
Also this is not a perfect approach to append controls, text in a table/div. but it can help you in some way.
此外,这也不是在表/div中添加控件的完美方法。但它可以在某种程度上帮助你。
Get values like this.
得到这样的价值观。
var values = [];
$(".interbare").each(function () {
values.push($(this).val());
});
Now you can pass values array to any page or service, as a string (values.join(',')) or as an array.
现在,您可以将值数组传递到任何页面或服务,如字符串(values.join(','))或作为数组。
Hope this will work.
希望这将工作。
#3
0
<form method="POST" id="textvalues">
<span>Cate intrebari va contine chestionarul?</span>
<input type='text' id='nrintrebari'/>
</td>
<td>
<input type='button' value='Creaza intrebari' onclick='generate()'/>
<input type='button' value='Save' id='btn_intrebari'/>
</td>
</tr>
</table>
<br><br>
<div id='sim'>
</div>
</form>
$(document).ready(function () {
$('#btn').click(function () {
$.ajax({
type: "POST",
url: 'pag1.php',
data: "intrebare[]=" + $('#intrebare[]').val(),
success: function (data) {
$('#status').html(data);
}
});
});
});
you can send values in this way also to your php page
您也可以以这种方式发送值到您的php页面。
#1
0
do not use "intrebare[] ...
不要使用“无畏的……”
u can only use "intrebare"
你只能使用“无畏”
Also another way is var str = $("form").serialize(); that will get the values from the form, if u want to use a form.
另一种方法是var str = $("form").serialize();这将从表单得到值,如果你想使用表单。
#2
0
you can not use a name as "#whatevername" Use ID of the control for this. and given you can not have controls with same ID (we can, but not recommended) you can give a class to all the text boxes and get all the values from all the controls.
你不能用“#whatevername”的名称来使用这个控件的ID。如果您不能拥有相同ID的控件(我们可以,但不推荐),您可以向所有文本框提供一个类,并从所有控件中获取所有值。
var a = 0;
function generate() {
var tot = $("#nrintrebari").val();
var tbl = $("#sim");
for (var i = 1; i <= tot; i++) {
a++;
tbl.append('Intrebare nr.' + a + '<input type="text" size="100" maxlength= "200" class="intrebare" style="height:30px; background-color:#B8B8B8; " >');
}
}
Also this is not a perfect approach to append controls, text in a table/div. but it can help you in some way.
此外,这也不是在表/div中添加控件的完美方法。但它可以在某种程度上帮助你。
Get values like this.
得到这样的价值观。
var values = [];
$(".interbare").each(function () {
values.push($(this).val());
});
Now you can pass values array to any page or service, as a string (values.join(',')) or as an array.
现在,您可以将值数组传递到任何页面或服务,如字符串(values.join(','))或作为数组。
Hope this will work.
希望这将工作。
#3
0
<form method="POST" id="textvalues">
<span>Cate intrebari va contine chestionarul?</span>
<input type='text' id='nrintrebari'/>
</td>
<td>
<input type='button' value='Creaza intrebari' onclick='generate()'/>
<input type='button' value='Save' id='btn_intrebari'/>
</td>
</tr>
</table>
<br><br>
<div id='sim'>
</div>
</form>
$(document).ready(function () {
$('#btn').click(function () {
$.ajax({
type: "POST",
url: 'pag1.php',
data: "intrebare[]=" + $('#intrebare[]').val(),
success: function (data) {
$('#status').html(data);
}
});
});
});
you can send values in this way also to your php page
您也可以以这种方式发送值到您的php页面。