使用jQuery将数据发布到自身 [英] Posting data to self using jquery

查看:78
本文介绍了使用jQuery将数据发布到自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些表单数据发布到自己身上,但没有成功.我的代码如下

I am trying to post some form data to self with no success.My code is as follows

<html>
    <title>Post to self</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">

    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.x-button').live("click", function () {
                $.ajax({
                    type: "POST",
                    url: "self.php",
                    data: $(".aj").serialize(),
                    success: function (data) {
                        alert("Data Loaded:");
                    }
                });
            });
        });
    </script>
    </head>

    <body>

               <?php
              if(isset($_POST['submit']))
              {
             echo $_POST['firstname'];
              }
              ?>
            <form name="input" action="" class="aj" method="post">
                <article>
                    <label>Firstname</label>
                    <input type="text" name="firstname" value="Ed" class="x-input"
                    />
                </article>
                <article>
                    <label>Lastname</label>
                    <input type="text" name="lastname" value="Doe" class="x-input"
                    />
                </article>
                <article>
                    <label>City</label>
                    <input type="text" name="city" value="London" class="x-input"
                    />
                </article>
                <input type="submit" value="Update Options" class="x-button" />
            </form>
    </body>

</html>

在纯php和html中使用<form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">时,它可以工作,但我无法使其与jquery一起工作.

When using <form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> in plain php and html it works but i can't get it to work with jquery.

推荐答案

当然,您不希望将整个页面都包含在响应文本中,因此如果需要ajax,则需要声明

Of course you do not want to include the whole page to the response text so you need a statement if ajax is requested

    <?php
/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

  if(isset($_POST))
  {
        print_r($_POST);
  }

}else{
?>   
<html>
    <title>Post to self</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">

    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.x-button').live("click", function () {
                $.ajax({
                    type: "POST",
                    url: "self.php",
                    data: $(".aj").serialize(),
                    success: function (data) {
                        alert("Data Loaded:");
                    }
                });
            });
        });
    </script>
    </head>

    <body>
       <form name="input" action="" class="aj" method="post">
                <article>
                    <label>Firstname</label>
                    <input type="text" name="firstname" value="Ed" class="x-input"
                    />
                </article>
                <article>
                    <label>Lastname</label>
                    <input type="text" name="lastname" value="Doe" class="x-input"
                    />
                </article>
                <article>
                    <label>City</label>
                    <input type="text" name="city" value="London" class="x-input"
                    />
                </article>
                <input type="submit" value="Update Options" class="x-button" />
            </form>
    </body>

</html>
<?php }?>

这篇关于使用jQuery将数据发布到自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆