我的ajax电话后,帖子怎么会出现空白?

I have an ajax call:

function change(x){
    var id=x;
    alert(id);
    $.ajax({
        url: "http://localhost/kidsKnitsDB/edit.php",
        type: "post",
        data: JSON.stringify(id),
        beforeSend: function(response){alert('Sending');},
            success: function(response){ alert('success');},
            error: function(response){alert('failed');},
            complete: function(response){window.location="http://localhost/kidsKnitsDB/edit.php";},
    });
}

Which is getting the data from this:

<?php for($r=0; $r<count($result); $r++){?>
    <tr>
        <?php for($c=0; $c<9; $c++){?>
            <td><?php echo $result[$r][$c];?></td>
        <?php }?>
        <td><button name="edit<?php echo $r;?>" onclick="change(<?php echo $result[$r][0];?>)">edit</button></td>
    </tr>
<?php }?>

and it should be posting to:

<?php 
    $db= new PDO("mysql:host=example;dbname=example", "root", "example");
    $id=$_POST['id'];
    $query= $db->prepare("SELECT * FROM table WHERE Id = :parameter");
    $query->bindParam(':parameter', $id, PDO::PARAM_STR);
    $query->execute();
    $result=$query->fetch();
    $name=$result['Name'];
?>
<script type="text/javascript">alert(<?php echo $id;?>);</script>
<h1>Edit for <?php echo $name;?></h1>
</br>
<form id="editForm" action="sucess.php" method="POST">
    <lable>sale price</lable>
    <input type="number" id="salePrice" name="salePrice">
    </br>
    <lable>cost</lable>
    <input type="number" id="cost" name="cost">
    </br>
    <lable>contents</lable>
    <input type="text" id="contents" name="contents">
    </br>
    <lable>on sale</lable>
    <input type="checkbox" id="onSale" name="onSale">
    </br>
    <lable>image</lable>
    <input type="image" id="image" name="image">
    </br>
    <lable>active</lable>
    <input type="checkbox" id="active" name="active">
    </br>
</form>

However, $id is blank, I believe this is because of the ajax call somehow, but I'm unsure. If anyone can help, it'd be great.

First problem

You are just sending a number, not an object, so the php-code don't know that you call it id.

Change

function change(x){
  var id=x;

To

function change(x){
  var id={id:x};

Second problem

With this piece of code:

complete: function(response){window.location="http://localhost/kidsKnitsDB/edit.php";}

you do a redirect after the ajax-call was completed.

This is what happens:

  1. The first ajax-request is sent to the server. Since you fixed the first problem it now gets the number in the post-variable named id.
  2. The server generates the page and gives it as a reply to your ajax-call.
  3. When the ajax-call is completed you do a manual redirect to the same page that you just sent an ajax-request from, but you do so without supplying any variables.
  4. The browser loads the page, but since no id was given in the request, the id is blank.

From your log-files you can see that it makes two request, the first with and id, and the second without any variables:

[Sun Jan 26 19:08:29 2014] Array
(
 [id] => 2
)

[Sun Jan 26 19:08:30 2014] Array
(
)

I think the AJAX call is making it more complicated than needed in this case. It is simpler to make a link to http://localhost/kidsKnitsDB/edit.php?id=x where x is the number. You should then also change $_POST to $_GET in the php-code.

If you still want to use AJAX you should insert the response you get from the request somewhere on the page, and not do a redirect like you do now.