我有一個Ajax調用:在我的ajax調用之後,這個帖子怎麼會變得空白?
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";},
});
}
這是從這個獲取數據:
<?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 }?>
,它應該被髮布到:
<?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>
然而,$ id爲空,我相信這是因爲阿賈克斯打電話,但我不確定。如果任何人都可以提供幫助,那會很棒。
你只是發送一個數字,而不是一個對象,所以php代碼不知道你稱它爲id。 – some
會把它變成'data:{'id':id}'工作? –
也許,看我的答案。 – some