如何使用PHP发布数据ajax

时间:2016-05-19 09:33:15

标签: php ajax

如何将数据发布到插入php文件中,它无法发布数据

<script src="jquery-1.12.3.js" type="text/javascript"></script>
<script type="text/javascript">
    $('#insert').click(function (event) {
        event.preventDefault();
        $.ajax({
            url: "insert.php",
            method: "post",
            data: $('form').serialize(),
            dataType: "text",
            success: function (strmessage) {
                $('#message').text(strmessage)
            }
        });
    });
</script>
<?php
?>
<p id="message"></p>
<form method="post">
    <input type="text" name="name" id="name" placeholder="Enter Name"><br>
    <input type="text" name="email" id="email" placeholder="email"><br>
    <input type="text" name="contact" id="contact"><br>
    <input type="submit" id="insert" name="insert" value="insert">
</form>

3 个答案:

答案 0 :(得分:2)

要从jquery提交表单,您应该在body元素中编写jquery代码,并且在form元素下将更容易处理...

<script src="jquery-1.12.3.js" type="text/javascript"></script>
</script>
<body>
<?php
?>
<p id="message"></p>
<form method="post">
    <input type="text" name="name" id="name" placeholder="Enter Name"><br>
    <input type="text" name="email" id="email" placeholder="email"><br>
    <input type="text" name="contact" id="contact"><br>
    <input type="submit" id="insert" name="insert" value="insert">
</form>
    <script type="text/javascript">
    $('#insert').click(function (event) {
        event.preventDefault();
        $.ajax({
            url: "insert.php",
            method: "post",
            data: $('form').serialize(),
            dataType: "text",
            success: function (strmessage) {
                $('#message').text(strmessage)
            }
        });
    });
   </script>
   </body>

答案 1 :(得分:0)

在insert.php文件中获取如下数据:

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
echo $name."--".$email."--".$contact;
?>

答案 2 :(得分:0)

在服务器端,即在php中你有回显或打印任何ajax返回的消息,否则ajax不会返回任何内容。

jQuery的:

@{
    var contentItems = new List<Object>();

    if (Model.Items != null)
    {
        contentItems = Model.Items;
    }

}

<div class="main-post">
    <div class="container">

        @foreach (var mainContentItem in contentItems)
        {

            var targetContentItem = (Orchard.DisplayManagement.Shapes.Shape)mainContentItem;
            var listOfShapes = targetContentItem.Items.ToList();

            var title = listOfShapes[0];
            var desc = listOfShapes[1];
            var mediaLib = listOfShapes[2];
            var actionLink = listOfShapes[3];

            <div class="clearfix hidden-xs" style="height:40px;"></div> <!-- spacer -->

            <row>

                <div class="col-sm-3 hidden-xs">
                    <figure>
                        @mediaLib.Item
                        @*
                            <img src="@mediaLib.Item" class="animation animated animation-fade-in-left" data-animation="animation-fade-in-left">
                        *@
                    </figure>
                </div>

                <div class="col-sm-7">
                    <div class="col-text">
                        <div class="post-heading-left">
                            <h2>@title.Item</h2>
                        </div>
                        <p>@desc.Item</p>
                        <button onclick="window.open('@actionLink.Item')" type="button" class="btn btn-primary">View App</button>
                    </div>
                </div>

            </row>

        }
    </div>

</div>

PHP:

<script type="text/javascript">
$('#insert').click(function (event) {
    event.preventDefault();
    $.ajax({
        url: "insert.php",
        method: "post",
        data: $('form').serialize(),
        dataType: "text",
        success: function (strmessage) {
            $('#message').text(strmessage);
        }
    });
});