首先,我很抱歉,因為我是 ajax 新手并且仍在學習它。我在我的網站頁面上使用谷歌翻譯,我想將 student_name 從原始文本/字串翻譯成阿拉伯語字串。它來自表,我想將它傳遞給 edit-student-data.php 頁面。我已成功獲取阿拉伯字串并將其宣告為變數。然后,當我想傳遞這個變數來編輯頁面時,我無法獲得 ajax 值。任何人都可以幫助我嗎?
PHP
<table>
<thead>
<th>Student name</th>
</thead>
<tbody>
<tr>
<td class="student_name"><?php echo $take['student_name'] ?></td>
<td>
<a class="btn btn-warning editButton" href="index.php?page=edit-student-data&student_id=<?=$take['student_id'] ?>"> <i class="fas fa-pencil-alt" style=""></i> Edit</a>
</td>
</tr>
</tbody>
</table>
<script>
$(document).on('click', '.editButton', function(e) {
var tr = $(this).closest("tr");
var student_name_arabic = tr.find(".student_name").text();
alert(student_name_arabic); //SUCCESS
$.ajax({
type: 'POST',
url: 'edit-student-data.php',
data: { ar_name: student_name_arabic },
success: function(data)
{
$('#form-control').html(data);
}
});
});
</script>
另一個 PHP 頁面(編輯學生資料頁面)
<div class="form-group">
<label for="exampleFormControlInput1">Student Name</label>
<input type="text" class="form-control" name="student_name" value="<?= $take['student_name'] ?>">
<?php
$ar_name = $_POST['ar_name'];
echo"<script>alert('$ar_name');</script>";
//I can't get arabic name value on alert. please help me:(
?>
</div>
uj5u.com熱心網友回復:
您需要更多地了解 GET 和 POST,我們需要如何將值從一個檔案傳遞到另一個檔案。
首先,從這段代碼中,您嘗試將值傳遞給 PHP 檔案“edit-student-data.php”,GET 以及 POST。
從這一行開始,您嘗試通過 URL 將值傳遞給檔案(稱為 GET 呼叫)
<td>
<a class="btn btn-warning editButton" href="index.php?page=edit-student-
data&student_id=<?=$take['student_id'] ?>"> <i class="fas fa-pencil-
alt" style=""></i> Edit</a>
</td>
從此 AJAX 代碼中,您嘗試通過 POST 呼叫將值傳遞給您在代碼中提到的型別
$.ajax({
type: 'POST',
url: 'edit-student-data.php',
data: { ar_name: student_name_arabic },
success: function(data)
{
$('#form-control').html(data);
}
});
- 無需在 href 中指定任何內容,只需
javascript:void(0). - 如果你想通過,page & student 到編輯頁面,那么你可以通過 post params
- 在檔案中
edit-student-data.php,您使用了變數$take['student_name']。這是該頁面的未定義索引。如果您想在該檔案中使用 student_name。您需要$_POST['ar_name'],這是您在 ajax 引數中指定的名稱

注意:您可以指定動態值,而不是 name 和 id 的靜態值
PHP代碼:
<table>
<thead>
<th>Student name</th>
</thead>
<tbody>
<tr>
<td class="student_name" student-id=<?php echo '5' ?>><?php echo "Test" ?></td>
<td>
<a class="btn btn-warning editButton" href="javascript:void(0)"> Edit</a>
</td>
</tr>
</tbody>
</table>
<!-- If necessary you can add this line -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).on('click', '.editButton', function(e) {
var student_name_arabic = $(".student_name").text();
var student_id_arabic = $(".student_name").attr("student-id");
// alert(student_name_arabic); //SUCCESS
$.ajax({
type: 'POST',
url: 'edit-student-data.php',
data: { ar_name: student_name_arabic, ar_id: student_id_arabic },
success: function(data)
{
alert(data);
$('#form-control').html(data);
}
});
});
</script>
檔案:edit-student-data.php
<div class="form-group">
<label for="exampleFormControlInput1">Student Name</label>
<input type="text" class="form-control" name="student_name" value="<?= $_POST['ar_name'] ?>">
<?php
$ar_name = $_POST['ar_name'];
echo"<script>alert('$ar_name');</script>";
//I can't get arabic name value on alert. please help me:(
?>
</div>
我希望這會有用。
uj5u.com熱心網友回復:
你的 jQuery 選擇器在這里:
success: function(data)
{
$('#form-control').html(data);
}
是為了一個id,所以地方
<div id='form-control'></div>
進入表格后的第一個代碼,它為 AJAX 回傳資料提供了某個地方。
如果您打算.form-control用于類,那將不起作用,因為您在元素存在之前參考了該元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/380674.html
標籤:javascript php 查询 阿贾克斯
上一篇:滑鼠懸停1秒后觸發事件
下一篇:以角度顯示按鈕
