在codeigniter php中插入数据后显示成功弹出消息

时间:2017-08-01 07:11:25

标签: javascript php jquery codeigniter

需要在提交数据后弹出窗口中显示成功消息,但问题是我点击提交按钮后会显示警告消息,然后提交数据。

function makeGETRequest(urlValue) {
    try {
        $.ajax({
            url: urlValue,
            type: 'GET',
            dataType: 'text',
            headers: {
                'Accept': 'application/rdf+xml',
                'OSLC-Core-Version': '2.0'
            },
            success: function (result) {
               var data = result;

            },
            error: function (error) {
                console.log("Can not perform HTTP request because of error: " + error.message);
            }
        });
    }
    catch(err) {
        console.log("Can not perform HTTP request because of error: " + err.message);
    }
};

脚本:

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

提交数据后,它应显示弹出窗口,页面应重定向到另一个页面。

尝试使用Ajax:

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

立即申请控制器:

<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();

        }
    })

})
})

3 个答案:

答案 0 :(得分:2)

<!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');

你看到戒备&#34; aaa&#34 ;;所有作品

在您的真实代码中,您需要更换 $这 - &GT;急骤&GT;成功(&#39;测试&#39);回应&#39;测试&#39 ;; 并删除重定向(&#39; apply&#39;);来自php

在这个js中你需要改变php脚本的路径

答案 1 :(得分:0)

请在代码中进行一些更改,如下所示。

<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";
} 

我希望这对你有所帮助。谢谢!

答案 2 :(得分:0)

你可能想看看这个 http://t4t5.github.io/sweetalert/