开发者

Forms/PHP/Ajax load?

I'm currently learning PHP. I've made a simple script @ http://hash.techho.me, only thing is, I want the form to submit then load the 开发者_C百科results via AJAX, without the user leaving the page. Possible?


post the form using ajax

$.ajax({
url:'yoururl',
data:$("form").serialize(),
type:'POST',
success:function(data){
alert("success");
},
error:function(jxhr){
alert(jxhr.responseText);
}

});

jQuery.ajax() – jQuery API


Posting to the same page should do the trick. No need to use ajax for that

> <?php 
> 
> //do stuff with $_POST 
> ?>
> 
> <html> <body> <form method="post">
> 
> <?php echo $result ?>
> 
> </form> 
> </body>


Fike

use ajax for this, lets suppose try this one for your practice

var string = $("#string").val();
var dataString = 'string=' + string ;
if(string==''){
    alert('enter any string');
}
else{
    $.ajax({
        type: "POST",
        url: "path of php file",
        data: dataString,
        suceess: function(){
            //do something
        },
        error: function(){
            //do something
        }
    });
}


You can use jQuery or Prototype JS libraries to make an easy AJAX call. Example using jQuery would be:


$.ajax({
  url:'hashed.php',
  data:$("form").serialize(),
  type:'POST',
  success: function(data){
    $('hashmd5').html(data.md5);
    $('hashsha1').html(data.sha1);
  },
  error: function(jxhr){
    alert(jxhr.responseText);
  }
});

Don't use the same id value in HTML, never ever. They must be unique to correct perform JavaScript functions on elements.


yes it is possible. Write a javascript function that would trigger on submit, disable the submit button so user couldn't press it again, and finally request the server via ajax. on successful response update the content. Something like following in Jquery

    $('.form-submit').click(function(event)) {
         event.preventDefault();
         if(form is valid and not empty) {
              $.ajax({
                   type: "POST",
                   url: "path to script that will handle insetion",
                   data: "data from form", //like ({username : $('#username').val()}),
                   suceess: function(data){
                        //update the content or what. data is the response got from server. you can also do like this to show feedback etc...
                        $('.feedback').html("Data has been saved successfully");
                   },
                   error: function(){
                        $('.feedback').html("Data couldn't be saved");
                   }
               });
         }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜