如何将带有图像src的ajax发送到zend控制器并将其保存在数据库中而不使用send表单

时间:2022-05-19 22:10:35

i would like to know how to get data from ajax call from view to zend controller and save it using zend table function. so now below is my code.

我想知道如何从ajax调用从视图到zend控制器获取数据并使用zend表函数保存它。所以下面是我的代码。

this is my controller

这是我的控制器

    public function addAction(){    

    $image_uri =  $this->getRequest()->getPost('image_uri');
    $personalmug = new PersonalMug();
    $personalmug->exchangeArray($image_uri);
    $this->getPersonalMugTable()->save($personalmug);



}

this is my ajax code.

这是我的ajax代码。

    $('#buttonSave').click(function (){
        var image_uri = $("mug1").attr("src");
        $.ajax({
                type: 'POST',
                dataType: 'json',
                url: 'http://toxicfox.com/personal-mug/add',
                async: false,

                // you can use an object here
                data: { image_uri: image_uri},
                success: function(json) {
                    console.log(json.image_uri);
                }
            });
        // you might need to do this, to prevent anchors from following
        // or form controls from submitting

    });

this is my save function from zend table class.

这是我从zend表类中保存的函数。

    public function save(PersonalMug $personalmug){
    $data = array(
        'image_uri'  => $personalmug->image_uri,
    );

    $image_id = (int) $$personalmug->image_id;
    if($image_id == 0){
        $this->tableGateway->insert($data);
    }else{
        if($this->getImage($image_id)){
            $this->tableGateway->update($data, array('image_id' => $image_id));
        }else{
            throw new \Exception('Mug id does not exist');
        }
    }
}

this is my model class

这是我的模特课

class PersonalMug{
public $image_id;

public $image_uri;

public function exchangeArray($data){
    $this->image_id = (!empty($data['image_id'])) ? $data['image_id'] : null;
    $this->image_uri  = (!empty($data['image_uri'])) ? $data['image_uri'] : null;
}

}

1 个解决方案

#1


0  

I have found the solution, i had some small mistakes and i was inserting the string instead of obeject. i hope it will help someone else also . below is the controller code with small mistakes corrected, it works fine now . Mistakes.

我找到了解决方案,我有一些小错误,我插入字符串而不是obeject。我希望它也能帮助别人。下面是纠正了小错误的控制器代码,现在工作正常。误区。

  1. I have used refrenced variable in the personalmugtable,

    我在personalmugtable中使用了refrenced变量,

    $image_id = (int) $$personalmug->image_id;

    $ image_id =(int)$$ personalmug-> image_id;

  2. i have not used # sybmol in the jquery,

    我没有在jquery中使用#sybmol,

    var image_uri = $("mug1").attr("src");

    var image_uri = $(“mug1”)。attr(“src”);

  3. I was insert string instead of object in the controller.

    我在控制器中插入字符串而不是对象。

    public function addAction(){

    public function addAction(){

    $data['image_uri'] = $this->getRequest()->getPost('image_uri'); $personalmug = new PersonalMug(); $personalmug->exchangeArray($data); $this->getPersonalMugTable()->save($personalmug); return $this->redirect()->toRoute('personal-mug');

    $ data ['image_uri'] = $ this-> getRequest() - > getPost('image_uri'); $ personalmug = new PersonalMug(); $ personalmug-> exchangeArray($数据); $这个 - > getPersonalMugTable() - >保存($ personalmug); return $ this-> redirect() - > toRoute('personal-mug');

    }

#1


0  

I have found the solution, i had some small mistakes and i was inserting the string instead of obeject. i hope it will help someone else also . below is the controller code with small mistakes corrected, it works fine now . Mistakes.

我找到了解决方案,我有一些小错误,我插入字符串而不是obeject。我希望它也能帮助别人。下面是纠正了小错误的控制器代码,现在工作正常。误区。

  1. I have used refrenced variable in the personalmugtable,

    我在personalmugtable中使用了refrenced变量,

    $image_id = (int) $$personalmug->image_id;

    $ image_id =(int)$$ personalmug-> image_id;

  2. i have not used # sybmol in the jquery,

    我没有在jquery中使用#sybmol,

    var image_uri = $("mug1").attr("src");

    var image_uri = $(“mug1”)。attr(“src”);

  3. I was insert string instead of object in the controller.

    我在控制器中插入字符串而不是对象。

    public function addAction(){

    public function addAction(){

    $data['image_uri'] = $this->getRequest()->getPost('image_uri'); $personalmug = new PersonalMug(); $personalmug->exchangeArray($data); $this->getPersonalMugTable()->save($personalmug); return $this->redirect()->toRoute('personal-mug');

    $ data ['image_uri'] = $ this-> getRequest() - > getPost('image_uri'); $ personalmug = new PersonalMug(); $ personalmug-> exchangeArray($数据); $这个 - > getPersonalMugTable() - >保存($ personalmug); return $ this-> redirect() - > toRoute('personal-mug');

    }