如何将jQuery值传递给PHP代码?
我试图制作一个输入表表单,在我的MVC页面中输入产品的新记录,这是视图的代码(我使用MongoDB)如何将jQuery值传递给PHP代码?,php,jquery,yii,Php,Jquery,Yii,我试图制作一个输入表表单,在我的MVC页面中输入产品的新记录,这是视图的代码(我使用MongoDB) 因此,为了使其成为可扩展的输入表单,我使用: <script type="text/javascript"> $(document).ready(function(){ addNewRow(); function addNewRow() { var i = $("#listItem tbody tr").size(); var arr = {};
因此,为了使其成为可扩展的输入表单,我使用:
<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>
$(文档).ready(函数(){
addNewRow();
函数addNewRow(){
变量i=$(“#listItem tbody tr”).size();
var arr={};
$(“#listItem tbody tr:last”)。单击(函数(){
if($(this).hasClass('addedRow')==false){
arr.i=i;
$(“#添加#价格#列表#行”).tmpl(arr).appendTo(“#列表项tbody”);
addNewRow();
}
$(this.addClass('addDrow'))
});
}
});
(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);
});
您可以将它们放在类似于$('#按钮')的内容中
或$('#textbox1')。on('keyup',function(){/*$.ajax()或$.post()*/})代码>
在PHP中,您可以执行以下操作,将其作为任何其他帖子进行处理:
if(isset($_POST['post1'])) {
// $result = do stuff with $_POST['post1'];
echo $result; // The jQuery will grab this as "result" and alert it...
}
PHP脚本无法处理客户端变量,除非它通过HTTP请求接收到该变量。(POST
或GET
)这是否有助于为您指明正确的方向?每隔一天就会问一次这个问题或其变体。也许,我错了,但我可以想象,即使是在谷歌上粗略的搜索,也会给你一个虚拟的解决方案聚宝盆,所有这些都会告诉你使用AJAX。显然,我没有错。即使只是从字面上搜索问题标题,也会得到:。StackOverflow应该是您的最后手段,而不是第一个。
if(isset($_POST['post1'])) {
// $result = do stuff with $_POST['post1'];
echo $result; // The jQuery will grab this as "result" and alert it...
}