关于Codeigniter上jquery ajax表单的问题

时间:2022-11-24 14:27:00

Everytime I test the email is send correctly. (I have tested in PC: IE6, IE7, IE8, Safari, Firefox, Chrome. MAC: Safari, Firefox, Chrome.)

每次我测试邮件都发送正确。(我测试过PC: IE6, IE7, IE8, Safari, Firefox, Chrome。麦克:Safari浏览器,火狐,Chrome)。

The _POST done in jquery (javascript). Then when I turn off javascript in my browser nothing happens, because nothing is _POSTed.

用jquery (javascript)完成的_POST。然后,当我在浏览器中关闭javascript时,什么都不会发生,因为没有任何东西是_post。

Nome: Jon Doe
Empresa: Star 
Cargo: Developer
Email:  jondoe@test.com
Telefone: 090909222988
Assunto:  Subject here..

But I keep recieving emails like this from costumers:

但我经常收到客户发来的这样的邮件:

Nome:
Empresa:
Cargo:
Email:
Telefone:
Assunto:

CONTACT_FORM.PHP

CONTACT_FORM.PHP

    <form name="frm" id="frm">                
    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >Nome<font style="color:#EE3063;">*</font></div>
    <div  class="campoFormulario inputDeCampo" ><input class="texto textocinzaescuro"  size="31" name="Cnome" id="Cnome" value=""/></div>


    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >Empresa<font style="color:#EE3063;">*</font></div>
    <div  class="campoFormulario inputDeCampo" ><input class="texto textocinzaescuro"  size="31"  name="CEmpresa"  id="CEmpresa" value=""/></div>

    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >Cargo</div>
    <div  class="campoFormulario inputDeCampo" ><input class="texto textocinzaescuro"  size="31"  name="CCargo" id="CCargo" value=""/></div>

    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >Email<font style="color:#EE3063;">*</font></div>
    <div  class="campoFormulario inputDeCampo" ><input class="texto textocinzaescuro"  size="31"  name="CEmail" id="CEmail" value=""/></div>


    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >Telefone</div>
    <div  class="campoFormulario inputDeCampo" ><input class="texto textocinzaescuro"  size="31"  name="CTelefone" id="CTelefone" value=""/></div>

    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >Assunto<font style="color:#EE3063;">*</font></div>
    <div  class="campoFormulario inputDeCampo" ><textarea class="texto textocinzaescuro"  name="CAssunto" id="CAssunto" rows="2" cols="28"></textarea></div>

    <div  class="campoFormulario nomeDeCampo texto textocinzaescuro" >&nbsp;</div>
    <div  class="campoFormulario inputDeCampo" style="text-align:right;" ><input id="Cbutton" class="texto textocinzaescuro"  type="submit" name="submit" value="Enviar" /></div>
    </form>





<script type="text/javascript">

$(function() {
    $("#Cbutton").click(function() {

      if(validarForm()){
           var Cnome = $("input#Cnome").val(); 
           var CEmpresa = $("input#CEmpresa").val(); 
           var CEmail = $("input#CEmail").val();
           var CCargo = $("input#CCargo").val(); 
           var CTelefone = $("input#CTelefone").val(); 
           var CAssunto = $("textarea#CAssunto").val(); 


          var dataString = 'nome='+ Cnome + '&Empresa=' + CEmpresa + '&Email=' + CEmail + '&Cargo=' + CCargo + '&Telefone=' + CTelefone + '&Assunto=' + CAssunto;
          //alert (dataString);return false;

          $.ajax({
        type: "POST",
        url: "http://www.myserver.com/index.php/pt/envia", 
        data: dataString,
        success: function() {
          $('#frm').remove();
          $('#blocoform').append("<br />Obrigado. <img id='checkmark' src='http://www.myserver.com/public/images/estrutura/ok.gif' /><br />Será contactado brevemente.<br /><br /><br /><br /><br /><br />")
          .hide()
          .fadeIn(1500);

        }
          });
     } 
     return false; 

    });


  });

function validarForm(){
    var error = 0;

    if(!validateNome(document.getElementById("Cnome"))){ error = 1 ;}            
    if(!validateNome(document.getElementById("CEmpresa"))){ error = 1 ;}           
    if(!validateEmail(document.getElementById("CEmail"))){ error = 1 ;}            
        if(!validateNome(document.getElementById("CAssunto"))){ error = 1 ;}            

     if(error == 0){
        //frm.submit();
            return true;
     }else{
            alert('Preencha os campos correctamente.');
            return false;
         }
    }

function validateNome(fld){
        if( fld.value.length == 0  ){
        fld.style.backgroundColor = '#FFFFCC';
        //alert('Descrição é um campo obrigatório.');
        return false;
        }else {
           fld.style.background = 'White';
       return true;
        }
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var tfld = trim(fld.value);                       
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = '#FFFFCC';
            //alert('Email é um campo obrigatório.');
        return false;
    } else if (!emailFilter.test(tfld)) {
            //alert('Email inválido.');
        fld.style.background = '#FFFFCC';
        return false;
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFFFCC';
            //alert('Email inválido.');
        return false;
    } else {
        fld.style.background = 'White';
       return true;
    }
}
</script>

FUNCTION ENVIA (email sender):

函数ENVIA(邮件发送):

function envia()
    {
        $this->load->helper(array('form', 'url'));

        $nome = $_POST['nome'];
        $empresa = $_POST['Empresa'];
        $cargo = $_POST['Cargo'];
        $email = $_POST['Email'];
        $telefone = $_POST['Telefone'];
        $assunto = $_POST['Assunto'];

        $mensagem =     "   Nome:".$nome."
    Empresa:".$empresa."
    Cargo:".$cargo."
    Email:".$email."
    Telefone:".$telefone."
    Assunto:".$assunto."";

    $headers = 'From: site@test.com' . "\r\n" .
'Reply-To: no-reply' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

        mail('email@test.com', $mensagem, $headers);

    }

2 个解决方案

#1


3  

You need to specify ACTION attribute to your form action="http://www.myserver.com/index.php/pt/envia" , because you are posting to your contact_form.php which I guess is not where you want to post looking at your AJAX code.

您需要为您的表单动作=“http://www.myserver.com/index.php/pt/envia”指定动作属性,因为您正在向contact_form发布内容。php我猜这不是你想要发布的,看看你的AJAX代码。

2nd edit:

编辑2:

You also need to specify the METHOD attribute of the form. Currently with disabled JavaScript the form is sent via GET and you are looking for POST variables.

您还需要指定表单的方法属性。目前禁用的JavaScript通过GET发送表单,您正在查找POST变量。

<form name="frm" id="frm" method="post">

#2


1  

Try this code:

试试这段代码:

var add_member_form=document.getElementById('frm'); 
$.ajax({type:'POST', url: add_member_form.action, data:$('#frm').serialize(),
        success: function(response) {           
            alert(response);
        }
});

#1


3  

You need to specify ACTION attribute to your form action="http://www.myserver.com/index.php/pt/envia" , because you are posting to your contact_form.php which I guess is not where you want to post looking at your AJAX code.

您需要为您的表单动作=“http://www.myserver.com/index.php/pt/envia”指定动作属性,因为您正在向contact_form发布内容。php我猜这不是你想要发布的,看看你的AJAX代码。

2nd edit:

编辑2:

You also need to specify the METHOD attribute of the form. Currently with disabled JavaScript the form is sent via GET and you are looking for POST variables.

您还需要指定表单的方法属性。目前禁用的JavaScript通过GET发送表单,您正在查找POST变量。

<form name="frm" id="frm" method="post">

#2


1  

Try this code:

试试这段代码:

var add_member_form=document.getElementById('frm'); 
$.ajax({type:'POST', url: add_member_form.action, data:$('#frm').serialize(),
        success: function(response) {           
            alert(response);
        }
});