开发者

Some ajax help?

开发者 https://www.devze.com 2023-03-31 10:03 出处:网络
I have a php script that takes some user form input and packs some files into a zip based on that input. The problem is that sometimes the server errors, so all the form data is lost. I was told I cou

I have a php script that takes some user form input and packs some files into a zip based on that input. The problem is that sometimes the server errors, so all the form data is lost. I was told I could开发者_开发问答 use ajax instead so that the user never even has to change the page. I've never used ajax, and looking at http://api.jquery.com/jQuery.ajax/ without any experience in ajax is quite difficult.

The page says that you can accept returns from an ajax call. How do you set up returns in the PHP file for an ajax call? If the server errors with the ajax call, how will I know?

edit: Also, is there a way to send an ajax request with javascript and jquery as if it were a submitted form?


How do you set up returns in the PHP file

just echo it in ajax page that will return as response

Simple Tutorial

client.php

$.post('server.php',({parm:"1"}) function(data) {
  $('.result').html(data);
});

server.php

<?php

echo $_POST['parm'];

?>

result will be 1

edit on OP comments Is there a way to use ajax as if you were submitting a form

Yes, there is

  • You can use plugins like jQuery form
  • Using submit
  • If you using jquery validation plugin, you can use submit handler option

using sumit

$('#form').submit(function() {
 //your ajax call
  return false;
});


every ajax function has a function param to deal with server returns.and most of them has the param msg,that is the message from server. server pages for example php pages you can just use echo something to return the infomation to the ajax funciton . below is an example

$.ajax({
  url:yoururl,
  type:post,
  data:yourdata,
  success:function(msg){
      //here is the function dealing with infomation form server.
  }
});


The easiest way to get information from PHP to JavaScript via AJAX is to encode any PHP data as JSON using json_encode().

Here's a brief example, assuming your server errors are catchable

<?php
try {
    // process $_POST data
    // zip files, etc
    echo json_encode(array('status' => true));
} catch (Exception $e) {
    $data = array(
        'status'  => false,
        'message' => $e->getMessage()
    );
    echo json_encode($data);
}

Then, your jQuery code might look something like this

$('form').submit(function() {
    var data = $(this).serialize();
    $.ajax(this.action, {
        data: data,
        type: 'POST',
        dataType: 'json',
        success: function(data, textStatus, jqXHR) {
            if (!data.status) {
                alert(data.message);
                return;
            }
            // otherwise, everything worked ok
        },
        error: error(jqXHR, textStatus, errorThrown) {
            // handle HTTP errors here
        }
    });
    return false;
});
0

精彩评论

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