表单不提交数据

form doesn't submit data

本文关键字:数据 提交 表单      更新时间:2023-09-26

我在提交表单时遇到了令人沮丧的问题。我的函数都被调用(通过日志文件检查),但请求 ->post('..') 不返回任何内容。我将永远感谢任何帮助解决这个头痛的人。

表单包含一个按钮,单击时调用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 文件-->项目.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);
    }
    .
    .  
    .
}

可以尝试为您的表单元素赋予名称属性:

<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>