簡體   English   中英

在Codeigniter PHP中插入數據后顯示成功彈出消息

[英]Displaying success popup message after data insertion in codeigniter php

提交數據后,需要在彈出窗口中顯示成功消息,但問題是,一旦我單擊“提交”按鈕,它就會顯示警報消息,然后再提交數據。

<form name="applynow"  id="applynow" enctype="multipart/form-data" method="post"  action="<?php echo base_url();?>apply/applynow">

            <div class ="applyus">                  
                    <div class="applyfullname contactname">                         
                        <input type="text" class="form-control names namesss" name="fullname"  value="<?php echo set_value('fullname');?>" placeholder="Full Name" required><span class="required">*</span> 
                        <?php echo form_error('fullname', '<div class="error">', '</div>'); ?>
                    </div>  

                    <div class="applynowemail contactemail">
                           <input type="email" class="form-control emails" id="email" name="email" value="<?php echo set_value('email');?>" placeholder="Email" required >  <span class="requireds">*</span>                           
                        <?php echo form_error('email', '<div class="error">', '</div>'); ?>                         
                    </div>  

<button type="submit"  class="btn btn-success successss" id="submits" >Submit</button>  

腳本:

<script>
$('form').on('submit',function(){
       alert('submitted');
});

提交數據后,它將顯示彈出窗口,並且頁面應重定向到另一頁面。

嘗試過Ajax:

<span class="success">Thank's for submitting the form</span>
$(document).ready(function() {
$("form[name='applynow']").submit(function() {
 $.ajax({
        type: "POST",
        url: "career.php",
        data: $(this).serialize(),
        success: function() {
            $('.sucess').fadeIn(100).show();

        }
    })

})
})

立即申請控制器:

public function applynow($job_id)
{
    if ($this->input->post('email')) 
    {          
        $this->form_validation->set_error_delimiters('<br /><span class="error"> ', '</span>');
        $this->form_validation->set_rules('fullname', 'First Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

        $this->form_validation->set_rules('captcha', 'Captcha', 'required');            

        if ($this->form_validation->run() == FALSE) 
        {
            $data['records2']= $this->career_model->getcareerdatas($job_id);
            $data['mainpage']='apply';
            $this->load->view('templates/template',$data);               
        } 
        else 
        {                
            $inputCaptcha = $this->input->post('captcha');
            $sessCaptcha  = $this->session->userdata('captchaCode');               
            if ($inputCaptcha === $sessCaptcha) 
            {
                $result = $this->apply_model->apply($this->input->post('email'));
                $data['records2']= $this->career_model->getcareerdatas($job_id);
                return true;
                if ($result) 
                {
                    $this->flash->success('<h2 style="color:green">Thank You applying to this post!will get back you soon once shortlisted..</h2>');
                    redirect('apply');
                } else 
                {
                    $this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
                    redirect('apply');
                }
            } else {
                $this->flash->success('<h2 style="color:red">Captcha code was not match, please try again.</h2>');
                redirect('apply');
            }                
        }
    }
    $config  = array(
        'img_path' => 'captcha_images/',
        'img_url' => base_url() . 'captcha_images/',
        'img_width' => '150',
        'img_height' => 40,
        'word_length' => 6,
        'font_size' => 30
    );
    $captcha = create_captcha($config);
    $word    = $captcha['word'];
    $this->session->unset_userdata('captchaCode');
    $this->session->set_userdata('captchaCode', $word);
    $data['captchaImg'] = $captcha['image'];
    $data['mainpage']   = "apply";
}   
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div class ="applyus">                  
<div class="applyfullname contactname">                         
<input type="text" class="form-control names namesss" name="fullname"  value="" placeholder="Full Name" >
</div>  

<div class="applynowemail contactemail">
   <input type="email" class="form-control emails" id="email" name="email" value="" placeholder="Email"  >                                                 
</div>  
<button class="btn btn-success successss" id="sub" >Submit</button> 
<script>
$("#sub").click(function() {
    var url = 'applynow.php';

    var data = {
        'namesss' : $(".namesss").val(),
        'emails' : $(".emails").val()
    }

    alert('Data before send is:');
    alert(JSON.stringify(data));

    $.ajax({
        type: "POST",
        url: url,
        data: data
    }).done(function(str) {
    alert('Data from php is:');
        alert(JSON.stringify(str));
        var result = JSON.parse(str);
        alert(result['variable1']);
        alert(result['variable2']);
    });
});
</script>

</body>
</html>

applynow.php:

<?php
class test() {
    public function applynow($job_id)
    {
        $result = [];
        $result['variable1'] = 'name is: ' . $_POST['namesss'];
        $result['variable2'] = 'email is: ' . $_POST['emails'];
        echo json_encode($result);
    }
}

$test = new test();
$test->applynow('1');

您會看到警報“ aaa”; 所有作品


在您的真實代碼中,您需要替換$ this-> flash-> success('test'); 回聲“測試”; 並刪除redirect('apply'); 從PHP

在這個js中,您需要更改php腳本的路徑

請對您的代碼進行一些更改,例如下面的Give。

<form name="applynow"  id="applynow" enctype="multipart/form-data" method="post">

            <div class ="applyus">                  
                    <div class="applyfullname contactname">                         
                        <input type="text" class="form-control names namesss" name="fullname"  value="<?php echo set_value('fullname');?>" placeholder="Full Name" required><span class="required">*</span> 
                        <?php echo form_error('fullname', '<div class="error">', '</div>'); ?>
                    </div>  

                    <div class="applynowemail contactemail">
                           <input type="email" class="form-control emails" id="email" name="email" value="<?php echo set_value('email');?>" placeholder="Email" required >  <span class="requireds">*</span>                           
                        <?php echo form_error('email', '<div class="error">', '</div>'); ?>                         
                    </div>  

<button type="submit"  class="btn btn-success successss" id="submits" >Submit</button>  

您必須使用async: false ,它將指示瀏覽器先完成此請求,然后再輸入其他代碼

<script>
    $(document).ready(function() {
     $('#applynow').validate({

            rules: { 



            },

            messages: {

            },


            submitHandler: function (form) {
var url ="<?php echo base_url();?>apply/applynow";  
var reDirectUrl = "<?php echo base_url();?>career.php"; 

     $.ajax({
        type: "POST",
        url: url,
        data: $(this).serialize(),
        async : false,
        success: function(data){ 
         // console.log('data', data);
             if(data == 1){
              $('.sucess').fadeIn(100).show(); // Need to set the success msg in the popup " Thank You applying to this post!will get back you soon once shortlisted.. "

             }else if(data == 0){
              // Do Something like this>> $('.failed').fadeIn(100).show(); // Sorry ! Message sending failed
             }


        }
    })

})
});
 </script>

立即應用控制器中的更改:

 public function applynow($job_id)
{
    if ($this->input->post('email')) 
    {          
        $this->form_validation->set_error_delimiters('<br /><span class="error"> ', '</span>');
        $this->form_validation->set_rules('fullname', 'First Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

        $this->form_validation->set_rules('captcha', 'Captcha', 'required');            

        if ($this->form_validation->run() == FALSE) 
        {
            $data['records2']= $this->career_model->getcareerdatas($job_id);
            $data['mainpage']='apply';
            $this->load->view('templates/template',$data);               
        } 
        else 
        {                
            $inputCaptcha = $this->input->post('captcha');
            $sessCaptcha  = $this->session->userdata('captchaCode');               
            if ($inputCaptcha === $sessCaptcha) 
            {
                $result = $this->apply_model->apply($this->input->post('email'));
                $data['records2']= $this->career_model->getcareerdatas($job_id);
                return true;
                if ($result) 
                {
                   echo '1'; exit;
//                    $this->flash->success('<h2 style="color:green">Thank You applying to this post!will get back you soon once shortlisted..</h2>');
//                    redirect('apply');
                } else 
                {
                   echo '0'; exit;
//                    $this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
//                    redirect('apply');
                }
            } else {                 

                $this->flash->success('<h2 style="color:red">Captcha code was not match, please try again.</h2>');
                redirect('apply');
            }                
        }
    }
    $config  = array(
        'img_path' => 'captcha_images/',
        'img_url' => base_url() . 'captcha_images/',
        'img_width' => '150',
        'img_height' => 40,
        'word_length' => 6,
        'font_size' => 30
    );
    $captcha = create_captcha($config);
    $word    = $captcha['word'];
    $this->session->unset_userdata('captchaCode');
    $this->session->set_userdata('captchaCode', $word);
    $data['captchaImg'] = $captcha['image'];
    $data['mainpage']   = "apply";
} 

希望對您有幫助。 謝謝!

您可能要看一下http://t4t5.github.io/sweetalert/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM