我在表单提交方面遇到了令人沮丧的问题。我的所有函数都被调用(通过日志文件检查),但请求 - > post('..')不会返回任何内容。我会永远感谢任何帮助解决这个问题的人。
php文件加载模板,用数据填充表单。表单包含一个按钮,单击调用javascript函数addAllToCartProject()。这个函数调用初始php文件中的addAll函数,我尝试从表单中获取数据但没有成功。
我正在处理的代码是OpenCart模块。
模板文件---> project_edit.tpl
<div id="tab-products" class="tab-content">
<form method="post" enctype="multipart/form-data" id="form2">
<div class="buttons"><div class="left">
<input type="button" value="<?php echo $button_add_products; ?>" id="pselect" class="button" />
<input type="button" value="<?php echo $button_add_all_cart; ?>" id="addallltocart" onclick="addAllToCartProject();" class="button" />
<input type="button" value="<?php echo $button_print; ?>" id="printproject" class="button" />
</div></div>
<div class="cart-info">
<table>
.
.
.
</table>
<input type="hidden" name="akey" value="<?php echo $akey; ?>" />
<input type="hidden" name="eid" value="<?php echo $eid; ?>" />
</form>
</div>
javascript功能
function addAllToCartProject() {
$.ajax({
url: 'index.php?route=account/projects/addAll',
type: 'post',
data: $('#form2').serialize(),
dataType: 'json',
success: function(json) {
$('.success, .warning, .attention, .information, .error').remove();
if (json['error']) {
$('#notification').html('<div class="warning" style="display: none;">' + json['error'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.warning').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
} else {
$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
}
});
}
php文件 - &gt; projects.php
.
.
.
if (isset($this->request->get['project_id'])) {
$printlink = $this->url->link('projects/projects_print', 'project_id=' . $eid . '&akey=' . $akey);
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/projects_edit.tpl')) {
$this->template = $this->config->get('config_template') . '/template/account/projects_edit.tpl';
} else {
$this->template = 'default/template/account/projects_edit.tpl';
}
} else { ..............}
.
.
.
public function addAll() {
$this->language->load('checkout/cart');
$this->language->load('account/projects');
$this->load->model('account/projects');
if (isset($this->request->post['eid'])) {
$eid = $this->request->post['eid'];
} else {
$eid = '0';
$file="log.txt";
$log=fopen($file,'a');
fwrite($log,"\n eid is not set ");
fclose($log);
}
if (isset($this->request->post['akey'])) {
$akey = $this->request->post['akey'];
} else {
$akey = '0';
$file="log.txt";
$log=fopen($file,'a');
fwrite($log,"\n akey is not set ");
fclose($log);
}
.
.
.
}
答案 0 :(得分:0)
可以尝试为表单元素指定names属性:
<div id="tab-products" class="tab-content">
<form method="post" enctype="multipart/form-data" id="form2">
<div class="buttons"><div class="left">
<input name = "products" type="button" value="<?php echo $button_add_products; ?>" id="pselect" class="button" />
<input name = "cart" type="button" value="<?php echo $button_add_all_cart; ?>" id="addallltocart" onclick="addAllToCartProject();" class="button" />
<input name = "print" type="button" value="<?php echo $button_print; ?>" id="printproject" class="button" />
</div></div>
<div class="cart-info">
<table>
.
.
.
</table>
<input type="hidden" name="akey" value="<?php echo $akey; ?>" />
<input type="hidden" name="eid" value="<?php echo $eid; ?>" />
</form>
</div>