1

好吧,当用户单击它时,我有一个按钮,它被禁用并淡出在一条消息中谢谢我们很快就会解决这个问题,但是现在我想做的是,当用户单击该按钮时,它将被禁用,然后它将发布到一个 php 文件(比如说:error.php)这个error.php会是这样的

<?php
if(isset($_POST['submit'])){
  $ip= $_SERVER['REMOTE_ADDR'];
  $page= $_SERVER['REQUEST_URI'];
// now update these to sql database ..... and if successfully posted return true 
}
else{
  return false;
}

现在,如果 jQuery 为 True,它将在一个带有成功消息的 div 中淡入,否则它将在另一个 div 中淡入并带有错误/抱歉消息

html 和 JS 看起来像这样

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#submit").click(function(){
 $('#submit').attr('disabled',true);
  $("#div1").fadeIn(1000); //if success
  $("#div2").fadeIn(1000); //if error
  });
});
</script>
</head>
<body>
<div id="div1" style="display:none;">Success</div>
<div id="div2" style="display:none;">Error</div>
<button id="submit">report The Page</button>
</body>
</html>

请帮忙

4

3 回答 3

0

Just do an ajax post....like so...

$("#submit").click(function(){
$('#submit').attr('disabled',true);
 $.post("example.php", function() {      
    $("#div1").fadeIn(1000); 
  }).fail(function() { 
    $("#div2").fadeIn(1000);});

But i dont know where your form is, so i dont know what data youre posting.

于 2013-07-05T18:59:57.923 回答
0

To see how to post via jQuery take a look at http://api.jquery.com/jQuery.post/

In your case your javascript code will look something like:

$("#submit").click(function(){
    $('#submit').attr('disabled',true);

    $.post('error.php', function(data) {
        if(data.success) {
            $("#div1").fadeIn(1000);
        } else {
            $("#div2").fadeIn(1000);
        }
    });
});

Do you need to submit the contents of a form along with this post? If so, change the above javascript to be like:

 $.post('error.php', $("#myform").serialize(), function(data) {

This assumes that you have a <form id="myform"> that you want to submit to error.php.

It also assumes that your PHP script is returning a 'success' attribute using json, something like (modified from your code):

<?php
if(isset($_POST['submit'])){
    $ip= $_SERVER['REMOTE_ADDR'];
    $page= $_SERVER['REQUEST_URI'];
    // now update these to sql database
    echo json_encode(array("success" => 1));
}
else{
    echo json_encode(array("success" => 1));
}
于 2013-07-05T19:00:14.910 回答
0

With jQuery you can use the .post() function: http://api.jquery.com/jQuery.post/

This function uses the HTTP POST Request and has a "success" function. The easiest way to use it is propably:

$.post('php/error.php', function(data) {
   console.log(data); // In your case "data" should be either "true" or "false"
});

You can pass other parameters as well (see docs). To fadeIn the correct div just check for the "data" parameter of the success function.

Hope this answers your question

于 2013-07-05T19:00:46.350 回答