weixin_33725807 2015-08-07 18:57 采纳率: 0%
浏览 9

Ajax未进行更改

I'm working through a book on PHP and jQuery and I've encountered a problem with Ajax, in that it will not post it's changes and display them.

    <head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
</head>
<body>
    <p>Hello World!</p>
    <p class="foo">Another paragraph, but this one ahs a class.</p>
    <p><span>This is a span in a paragraph</span></p>
    <p id="bar">Paragraph with an id.
        <span class="foo">And this is a sentence in a span</span>
    </p>
    <script src="jquery.validate.min.js"></script>
    <script src="jquery/ajax.js"></script>
    <script src="jquery/jquery-1.8.3.min.js"></script>

    <form action="#" method="post">
        <fieldset>
            <legend>Sign Up Form</legend>
            <label for="name">Name</label><br/>
            <input name="name" id="name" type="text" /><br/>
            <label for="password">Password</label><br/>
            <input name="password" id="password"
                   type="password" /><br/>
            <label>
                <input id="mycomp" type="radio" name="loc" />
                I'm on my computer.
            </label><br/>
            <label>
                <input id="shared" type="radio" name="loc" checked="checked" />
                I'm on my shared computer.
            </label>
            <input type="submit" value="Log In" /><br/>
            <label>
                <input id="signedin" type="checkbox" name="notify"
                       disabled="true"/>
                Keep me signed in on this computer.
            </label><br/>
        </fieldset>
    </form>

</body>
</html>

Here is the code for the test site. It's exceedingly simple yet this ajax query won't modify it:

    $.ajax({
  "type":"POST",
  "url":"ajax.php",
  "data":"var1=val1&var2=val2",
  "success":function(data){
    $("#bar")
    .css("background","yellow")
    .html(data);
  }
});

I'm running it through the firebug console, and it seems to be functioning correctly, but nothing is actually happening.

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2015-08-07 19:11
    关注

    Well, I try to explain all. First, when you want send a form with ajax, you don't use submit and attributes of tag . You assign function to button or element to submit the form, and this function do the call ajax, for example.

    With your html, this Jquery code:

    $('input[type=submit]').click(submitForm);
    function submitForm(){
    $.ajax({
       "type":"POST",
       "url":"ajax.php",
       "data":"var1=val1&var2=val2",
       "success":function(data){
           $("#bar")
           .css("background","yellow")
           .html(data);
       }
     });
    }
    

    And if you do POST, you must send a variables in array or like this:

    "data": {
       var1:val1,
       var2:val2
     }
    

    You receive in $_POST[var1] and $_POST[var2] in ajax.php.

    So your javascript/Jquery code would be something like this:

    $('input[type=submit]').click(submitForm);
    function submitForm(){
    $.ajax({
      "type":"POST",
      "url":"ajax.php",
      "data": {
           var1:val1,
           var2:val2
         },
      "success":function(data){
        $("#bar")
        .css("background","yellow")
        .html(data);
      }
    });
    }
    

    If you have any question, tell me. I hope this be useful for you.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效