简体   繁体   中英

php - form doesn't submit data

I'm having a frustrating problem with a form submit. My functions are all called (checked through log file) but request-->post('..') doesn't return anything. I would be eternally thankfull to anyone who helps solve this headache.

php file loads the template, fills the form with data. form contains a button which on click calls javascript function addAllToCartProject(). this function calls the addAll function in the initial php file where I try to get the data from the form with no success.

The code I'm working on is for an OpenCart module.

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

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 file-->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);
    }
    .
    .  
    .
}

May be try to give names attribute to your form elements:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM