1

Zend Framework と jQuery は初めてです。の戻り値に基づいて、データベースから行を削除しようとしていますconfirm()。しかし、私が抱えている問題は、はいまたはいいえの両方を押すと、選択した行が削除されることです。はいを押したときにのみ、選択した行のみが削除されることを確認する必要があります。Zend Framework を使用していることの 1 つ。

私のコードは以下のとおりです。

index.phtml

<html>
<head>
<style type="text/css" rel="stylesheet">
#simplemodal-overlay {background-color:#000;}
#simplemodal-container {background-color:#333; border:8px solid #444; padding:12px;}
#simplemodal-container a.modalCloseImg {
    background:url(x.png) no-repeat; /* adjust url as required */
    width:25px;
    height:29px;
    display:inline;
    z-index:3200;
    position:absolute;
    top:-15px;
    right:-18px;
    cursor:pointer;
}

</style> 
<script type="text/javascript" src="/jQuery.js"> </script>
<script type="text/javascript" src="/jquery.simplemodal-1.4.2.js"></script>
<script type="text/javascript">


$(document).ready(function(){
    //alert("test");
    $(".delete").click(function(e){
        //$('p').attrib('id');
        //e.preventDefault();
        //$("#deleteForm").modal();
        if(confirm("test"));

    });

    });
</script>
</head>
<center>
<?php         
echo "Student List";

?>
<p><a href="<?php echo $this->url(array('controller'=>'index', 
        'action'=>'add'));?>">Add new student</a></p>
<table border="1">
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Action</th>
</tr>
<?php foreach($this->students as $student) : ?>
<tr>

    <td><?php echo $student->name;?></td>
    <td><?php echo $student->email;?></td>
    <td><?php echo $student->phone;?></td>

    <td>
        <a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'edit', 'id'=>$student->id));?>">Edit</a>
        <a class="delete" href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'delete','id'=>$student->id));?>">Delete</a>
    </td>
</tr>

<?php endforeach; ?>
</table>

</center>

</html>

IndexController.php

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
      //  $test = new Application_Model_DbTable_Email();

    }

    public function indexAction()
    {
          $this->_helper->layout->disableLayout();
        $students = new Application_Model_DbTable_Students();
        $this->view->students = $students->fetchAll();
    }

    public function addAction()
    {
        $form = new Application_Form_Student();
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $name = $form->getValue('name');
                $email = $form->getValue('email');
                $phone = $form->getValue('phone');
               // $image = $form->getValue('image');
                $students = new Application_Model_DbTable_Students();
                $students->addStudent($name, $email,$phone);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }

    }

    public function editAction()
    {
        $modelStudents = new Application_Model_DbTable_Students();
        $id = (int) $this->_getParam('id');

        $student = $modelStudents->fetch($id);

        $form = new Application_Form_Student($student->email);
        $form->submit->setLabel('Save');
        $this->view->form = $form;
       if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $name = $form->getValue('name');
                $email = $form->getValue('email');
                $phone = $form->getValue('phone');
                $students = new Application_Model_DbTable_Students();
                $students->updateStudent($id, $name, $email, $phone);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {

                $form->populate($modelStudents->getStudent($id));
            }
        }

    }

    public function deleteAction()
    {

                //$id = $this->getRequest()->getPost('id');
            $students = new Application_Model_DbTable_Students();
            $id = $this->_getParam('id', 0);
            $students->deleteStudent($id);
            $this->_helper->redirector('index');

    }

    //echo $test;

}

前もってありがとう、エナマル

4

2 に答える 2

0

確認を誤って使用しており、投稿されたコードにも多くの疑問があります。
リンクを以下のようにフォーマットします

 <a class="delete" id="delete_link_<?php echo $student->id?>" href="jacascript:void(0)" onclick="deleteRecord('<?php echo $this->url(array('controller'=>'index', 
            'action'=>'delete','id'=>$student->id));?>',<?php echo $student->id?>)">Delete</a>

この関数をjsに追加します

   <script>
    function deleteRecord(url,id)
    {
      if(confirm('test')){ 
      $.ajax({url:url,success:function(data){alert('Deleted Record')
                                             $("#delete_link_"+id).hide();
                                               }});

      }    
   }
    </script>

編集済み:

<script type="text/javascript">

//
$(document).ready();

  function deleteRecord(url)
        {
          if(confirm('test')){ 
          $.ajax({url:url,success:function(data){alert('Deleted Record')}});
          }    
       }

</script>
于 2012-07-28T06:50:29.483 に答える
0
    $(document).ready(function(){
    //alert("test");
    $(".delete").click(function(e){
        //$('p').attrib('id');
        //e.preventDefault();
        //$("#deleteForm").modal();
        if(confirm("test"))
{
return true;
}
else
{
return false;
}

});

});
于 2012-07-28T07:05:08.040 に答える