我正在尝试制作一个输入表表单,以在我的MVC页面中输入产品的新记录,这是View的代码(我使用的是MongoDB)
<?php
for ($i = 0; $i <= 1; $i++) {
<tr>
<td><?php echo CHtml::activeTextField($modelItem, "[$i]name"); ?></td>
<td><?php echo CHtml::activeTextArea($modelItem, "[$i]description"); ?></td>
<td><?php echo CHtml::activeTextField($modelItem, "[$i]price"); ?></td>
</tr>
<?php } ?>
因此,为了使它成为一个可扩展的输入表单,我使用了:
<script type="text/javascript">
$(document).ready(function(){
addNewRow();
function addNewRow() {
var i = $("#listItem tbody tr").size();
var arr = {};
$("#listItem tbody tr:last").click(function(){
if($(this).hasClass('addedRow') == false){
arr.i = i;
$("#ADD_PRICE_LIST_ROW").tmpl(arr).appendTo("#listItem tbody");
addNewRow();
}
$(this).addClass('addedRow')
});
}
});
</script>
(listItem tbody是表的id)那么,我必须做些什么才能完成下面的功能呢?
<script id="ADD_PRICE_LIST_ROW" type="text/x-jquery-tmpl">
<tr>
????
</tr>
</script>
抱歉英语不好
您需要使用AJAX来实现这一点:
$.ajax({
type: 'POST',
url: '/your/php.php',
data: {
post1: $('#element').val(),
post2: $('#element2').text()
},
success: function(result) {
alert(result);
},
error: function() {
$('#errors').text('Failed to get response from PHP');
}
});
如果你不需要那么多选项,你也可以使用$.post()
来做类似的事情:
$.post('/your/php.php', {
post1: $('#element').val(),
post2: $('#element2').text()
},
function(result) {
alert(result);
});
你可以把它们放在类似$('#button').on('click', function() { /* $.ajax() or $.post() */ });
或$('#textbox1').on('keyup', function() { /* $.ajax() or $.post() */ });
的东西里面。
在你的PHP中,你可以做这样的事情来处理它,就像任何其他POST:一样
if(isset($_POST['post1'])) {
// $result = do stuff with $_POST['post1'];
echo $result; // The jQuery will grab this as "result" and alert it...
}