我的ajax表格没有显示我的帖子回复

时间:2017-01-14 15:57:51

标签: php jquery ajax

我现在遇到了这个问题,我的ajax表单没有显示我在文本字段中输入的响应值。我似乎无法理解为什么它根本没有显示我的帖子价值。

reset.php

<html>  
<head>
<script
  src="https://code.jquery.com/jquery-3.1.1.js"
  integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
  crossorigin="anonymous"></script>
  <script>
function submitdata()
{
 var email=document.getElementById( "emailfield" );
 var datastring='email='+ emailfield;
{
    $.ajax({
        url: "work2.php",
        type:'POST',
        data:datastring
        cache:false
        success: function (html){
            $('#msg').html();
        }
    });
});

      </script>
</head>
<body>

<form method="POST">
E-mail: <input type="text" id="emailfield"><br>
<input value="submit" type="submit" onclick="return submitdata()">
</form>
<p id="msg"><p/>
</body>
</html>

work2.php

<?php
$email=$_POST['email'];
echo "response $email";
?>

3 个答案:

答案 0 :(得分:2)

这里缺少一些东西:

1)您没有从用户那里获得价值。

使用:

var emailfield = document.getElementById( "emailfield" ).value;

或只是

$("#emailfield").val();

2)您没有阻止默认的提交流程。

使用:

 e.preventDefault();

我继续为你写这篇文章。只需复制所有文件,您就会看到它正常工作。希望它有所帮助!

<?php

$data = array();
 if(isset($_POST['email'])){
 $data = $_POST['email'];       
 echo json_encode($data);  
 die();      
}
?>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>

<form>
    E-mail: <input type="text" id="emailfield"><br>
    <input value="submit" type="submit">
</form>
<p id="msg"><p/>

<script type = "text/javascript">

    $("form").on("submit", function(e){

        e.preventDefault();
        var emailfield = $("#emailfield").val();
        var email ='email='+ emailfield;

        $.ajax({
            url: "testing.php",
            method: "POST",
            dataType: "json",
            data: {email: email},
            success: function (result) {
                alert("result: " + result);
                console.log(result);
                $("#msg").html(result);
            }
        });
    });

</script>
</body>
</html>

答案 1 :(得分:0)

你没有获得价值,它的元素对象,在这里改变

  var emailfield = document.getElementById( "emailfield" ).value;

同时输入值为

的html
  $('#msg').html(html);

答案 2 :(得分:0)

试试这样。

using System;
using System.Linq;

namespace SO.MacakM.Answer
{
    class Program
    {
        static void Main(string[] args)
        {
            var range = Enumerable.Range(1, 1000);

            Console.WriteLine("Using LINQ...");
            range.ToList().ForEach(i => Console.WriteLine(i));

            Console.WriteLine("Using PLINQ...");
            range.AsParallel().ForAll(i => Console.WriteLine(i));

            Console.Read();
        }
    }
}