简体   繁体   English

通过AJAX和PHP提交表单

[英]Submitting the Form through AJAX and PHP

Scenario: 场景:

On clicking Submit Button, the alert box having following message is displayed: 单击提交按钮时,将显示具有以下消息的警报框:

You won't be able to make any changes after submitting the form. 提交表格后,您将无法进行任何更改。 Do you want to submit? 您要提交吗?

And when the user presses OK then the following message should be displayed: 当用户按“ 确定 ”时,将显示以下消息:

Submitted 提交

Here is my attempt: 这是我的尝试:

This is the part of the Form : 这是表格的一部分:

Note : I am not showing the whole data of the form to be submitted as it will take space.I am only showing the main part of the form submit button. 注意我不会显示要提交的表单的全部数据,因为它会占用空间。我仅显示表单提交按钮的主要部分。

<div id="container">

    <form action="<?php echo site_url('main/save_appraisal');?>"  
        method="post" id="appraisalForm" name="appraisalForm">

    <?php if($appr_status < '2'){?>
    <input type="button" name="submit" id="submitAppraisal" 
    value="Submit" /> 

    <?php }?>

<form>
</div>

This is the part of my controller : 这是我的控制器一部分

public function submitAppraisal(){
        $emp_id = $this->input->post('empid');
        echo $this->appraisal_model->setAppraisalStatus($emp_id);
        echo 'Submitted';
} 

This is the part of my Model : 这是我的模型一部分

function setAppraisalStatus($employee_id=''){
if($employee_id == ''){
            return;
        }

        $login =  $this->session->userdata('username');     
        $loginid = $this->get_empid_exuname($login);

       $sql = "INSERT INTO appraisal_status 
 (id,employee_id,status,updated_on,updated_by)
 VALUES(seq_appraisal_status.nextval,$employee_id,$status,sysdate,'$login')";       
 $query = $this->db->query ( $sql );
}

Here is the part of the Form : 这是表格一部分

    <script>
<script type="text/javascript">
$(function () {

   $( "#submitAppraisal" ).click(function() {        
        submitAppraisal();
    }); 
});


function submitAppraisal(){

    <?php if($this->session->userdata('userid') == $supervisor){?>
    var valid = 0;
    $( '.suvinput' ).each(function() {
        var vals = $(this).val();
        $(this).css('border-color','#ccc');
        if(vals == ''){         
            $(this).css('border-color','#f00');
            valid++;
        }

    }); 

    var confirmSubmit = confirm("You won't be able to make any 
    changes after submitting the form. Do you want to submit? ");
        if(confirmSubmit == false){ 
            return;
        }

        var params = $('form#appraisalForm').serialize()+ '&submit=Save';
        console.log(params);
        $.ajax({
              url: '<?php echo site_url('main/submitAppraisal');?>',
              type: 'post',
              data: params,
              success: function(data) {
              },
              error: function(err) {
              }
            });
    }
    </script>

Here is my database table named appraisal_status : 这是我的名为appraisal_status的数据库表:

ID   |   Employee_ID  | Status

Current Output: 电流输出:

When the user presses the Submit Button then following question is being asked: 当用户按下“ 提交”按钮时,将询问以下问题:

You won't be able to make any changes after submitting the form. Do
you want to submit?

When the user presses Yes then the form should be submitted and the record should be inserted in the database but neither the form is being submitted nor the message is being displayed. 当用户按“ 是”时 ,应提交表单并将记录插入数据库中,但既不提交表单也不显示消息。

Expected Output: 预期产量:

After the user presses Submit button then the following message is being displayed which is fine. 用户按下Submit按钮后,将显示以下消息,这很好。

You won't be able to make any changes after submitting the form. 提交表格后,您将无法进行任何更改。 Do you want to submit? 您要提交吗?

And when the user presses OK button then the following message should be displayed: 当用户按下“确定”按钮时,将显示以下消息:

Submitted

I am not being able to figure out where I went wrong? 我无法弄清楚哪里出了问题?

What correction should be done? 应该做哪些更正? Suggestions are highly appreciated. 建议非常赞赏。

The entire point of Ajax is to pass the response to JavaScript instead of simply displaying it. Ajax的全部目的是将响应传递给JavaScript,而不是简单地显示它。

This is your success function: 这是您的成功功能:

      success: function(data) {
      },

It accepts the response into the variable data and then does nothing with it. 它接受对变量data的响应,然后对其不执行任何操作。

You are ignoring the output of your PHP program. 您将忽略PHP程序的输出。

If you want to display the output, then you need to write code to display it. 如果要显示输出,则需要编写代码以显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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