简体   繁体   English

使用ajax提交的数据获得结果,但没有联系表单

[英]Data submit with ajax get result but not contact form

I have a online quiz by jQuery , I want once the user submit it, to send the results with the contact information's of the users to the moderator. 我有一个jQuery的在线测验,我希望一旦用户提交了测验,就将结果以及用户的联系信息发送给主持人。

I make it work, sending the results information correctly, the problem is it doesn't send the user information. 我可以正常工作,正确发送结果信息,问题是它不发送用户信息。

I've been playing around with different solution's, I can manage or to have the user information or the results of the quiz, but not both at the same time ! 我一直在研究不同的解决方案,我可以管理或拥有用户信息或测验的结果,但不能同时使用两者!

Here is the "contact form": 这是“联系表格”:

<form action="submit.php" method="POST" id="form">

<label>Name</label>
<input name="name" placeholder="Type Here">

<label>Email</label>
<input name="email" type="email" placeholder="Type Here">

<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>

<input id="button" type="submit" value="Send" class="btnShowResult">

Here is the jQuery part about the ajax to send the data result of the quiz, the div #resultKeeper being the result I want to receive 这是关于ajax的jQuery部分,用于发送测验的数据结果,div #resultKeeper是我想要接收的结果

$(function() {
  $('#form').on('submit',function(e) { e.preventDefault();
        $.ajax({
              url:'submit.php',
              type:'POST',
              data:{'results':$('#resultKeeper').html(),'subject':'Subject of your e-mail'},
              success:function() {
$('#responseMessage').html()
              }
        });
        return false;

  });
});

here is my PHP 这是我的PHP

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$results = $_POST['results'];
$formcontent="From: $name \n Message: $message \n Results: \n $results";
$recipient = "myemail@gmail.com";
$subject = "my subject";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Your email has been send, thank you";
?>

If in the Jquery I change the 如果在Jquery中,我更改了

$(function() {
  $('#form').on('submit',function(e) 

by #button instead of #form, I receive the informations from the contact form, but not anymore from the results. 通过#button而不是#form,我从联系表中收到信息,但不再从结果中接收信息。

Also by keepin the #form, I receive the result of the quiz, and few value as the form content, but not the informations from the placeholder !, here is what I receive by email: 同样通过保持#form格式,我收到测验的结果,但作为表单内容的值很少,但没有收到占位符!的信息,这是我通过电子邮件收到的内容:

"
From:
 Message:
 Results:
 <div> Question 1 is true</div><div> Question 2 is false\</div><div> Question 3 is true\</div>\<div> Question 4 is false\</div>\<div> Question 5 is false\</div>\<div class=\"totalScore\">\<br>Your total score is 2 / 5\</div>\<div class=\"totalScore\">\<br>CONGRATULATION, YOUR LEVEL IS A1.1\</div
"

As we can see I have the From: and Message: appearing, but not the proper name and message that the user are writing .. . 如我们所见,我出现了“发件人:”和“消息:”,但没有出现用户正在编写的正确名称和消息。

Any help will be adorable !! 任何帮助将是可爱的!

Here is the allcode of JSFiddle: 这是JSFiddle的全部代码:

http://jsfiddle.net/ccwJJ/ http://jsfiddle.net/ccwJJ/

You do not receive your form values in your php file when you make an ajax request, you have to explictly send them in the ajax request. 发出ajax请求时,您不会在php文件中收到表单值,您必须在ajax请求中明确发送它们。

Consider below form 考虑以下形式

 <form action="submit.php">
    <input type="email" name="email" id="email"/>
    <input type="submit" value="send"/>  
 </form> 

Case 1 Normal way 情况1正常方式

When you submit the form normally, say hitting enter or on click you would receive the form values implicitly in your php file as $_POST['email] variable 当您正常提交表单时,说按Enter或单击,您将在php文件中隐式接收$_POST['email]变量形式的表单值

 <?php $email = $_POST['email'] ; // valid ?>

Case 2 Ajax way 案例2 Ajax方式

When you submit the form using ajax, You do not receive the form values implicitly in your php file since your ajax request does not know about your form elements, You have to send the values explicitly in the ajax request. 当您使用ajax提交表单时,由于您的ajax请求不了解表单元素,因此您不会在php文件中隐式接收表单值,您必须在ajax请求中显式发送这些值。

  $.post('submit.php',function(result){

          $('#responseMessage').html(result);
    });

   <?php $email = $_POST['email'] // error ?>  
   why? because you have not set the post variable email 

Then how to do it? 那该怎么办呢?

   var uemail = $("#email").val(); // get the email 
   // Set email and send it through ajax   
   $.post('submit.php',email:uemail,function(result){

          $('#responseMessage').html(result);
    });

   <?php $email = $_POST['email'] // works perfect ?>

So now change your form to this 所以现在将您的表格更改为此

 <form action="submit.php" method="POST" id="quesForm">
     <label>Name</label>
     <input name="username" type="text" placeholder="Type Here">
     <label>Email</label>
     <input name="email" type="email" placeholder="Type Here">
     <label>Message</label>
     <textarea name="message" placeholder="Type Here"></textarea>
     <input id="button" type="submit" value="Send" class="btnShowResult">
 </form>

Notice I have changed name='name' to name='username' and I suggest you to change id=form to some other name say id=quesForm . 注意,我已将name='name'更改为name='username'并且建议您将id=form更改为其他名称,例如id=quesForm I have seen some browsers does not seem to work as expected when you use tags as id names and attributes as attribute values like you have done name=name and id=form 我已经看到,当您使用标签作为id名称和属性作为属性值时,某些浏览器似乎无法正常工作,就像您完成name=nameid=form

I suggest you to use $.post instead of $.ajax , It simplifies the things for you when you really require $.post 我建议您使用$.post代替$.ajax ,当您确实需要$.post时,它可以简化您的工作

jQuery jQuery的

$(function() {
   $('#quesForm').on('submit',function(e) { 
    e.preventDefault();
    // I am fetching the form values you could get them by other selectors too
    var uname = $("input[name=username]").val(); 
    var uemail = $("input[name=email]").val();
    var msg = $("textarea").val()
    $.post('submit.php',{username:uname,email:uemail,message:msg,results:$('#resultKeeper').html(),subject:'Subject of your e-mail'},function(result){
          // result variable contains your response text
          // I guess you trying to update your response 
         // notice I have used html(result) as you have just used html()
          $('#responseMessage').html(result);
    });
      // you dont require `return false`
      // you have already did it using e.preventDefault();
  });
}); 

php PHP

<?php $name = $_POST['username'];
       $email = $_POST['email'];
       $message = $_POST['message'];
       $results = $_POST['results'];
       $results = strip_tags($results); // add this to remove html tags and all
       $formcontent="From: $name \n Message: $message \n Results: \n $results";
       $recipient = "thibault.rolando@gmail.com";
       $subject = "my subject";
       $mailheader = "From: $email \r\n";
       mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
       echo "Your email has been send, thank you";
 ?>

To remove html tags in php you have function named strip_tags(); 要删除php中的html标签,您可以使用名为strip_tags();函数strip_tags();

You need to send the form data, plus your results string. 您需要发送表单数据以及结果字符串。

Try the following: 请尝试以下操作:

$(function() {


  $('#form').on('submit',function(e) { e.preventDefault();
        //grab the form data into array
        var formData = $('#form').serializeArray();
        //add your result and subject
        formData.push({ results:$('#resultKeeper').html(), subject:'Subject of your e-mail'});
        $.ajax({
              url:'submit.php',
              type:'POST',
              data:formData,
              success:function() {
                  $('#responseMessage').html()
              }
    });
    return false;

}); });

It is better to use following way for getting form input values: 最好使用以下方法获取表单输入值:

var data = $('#yourformid').serielize();

You will get the data in serielize format and put it on ajax data param. 您将获得序列化格式的数据并将其放在ajax数据参数中。

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

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