我无法将 ajax 变量获取到其他 PHP 页面
I cant get ajax variable to other PHP page
首先,我很抱歉,因为我是 ajax 的新手,并且还在学习。我在我的网站页面上使用 google 翻译,我想将 student_name 从原始 text/string 翻译成阿拉伯语字符串。它来自 table,我想将它传递给 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>
您的 jQuery 选择器在这里:
success: function(data)
{
$('#form-control').html(data);
}
用于id
,所以放置
<div id='form-control'></div>
在 table 之后的第一个代码中,它提供了 AJAX return 数据。
如果您打算将 .form-control
用于 class,那将不起作用,因为您在元素存在之前引用了它。
您需要更多地了解 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)
.
- 如果你想pass, page & student到编辑页那么你可以通过post params
- 在文件
edit-student-data.php
中,您使用了变量 $take['student_name']
。这是该页面的未定义索引。如果您想在该文件中使用 student_name。您需要 $_POST['ar_name']
,这是您在 ajax params 中指定的名称
注意:您可以指定动态值,而不是名称和 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>
文件:编辑-学生-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>
我希望这有用。
首先,我很抱歉,因为我是 ajax 的新手,并且还在学习。我在我的网站页面上使用 google 翻译,我想将 student_name 从原始 text/string 翻译成阿拉伯语字符串。它来自 table,我想将它传递给 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>
您的 jQuery 选择器在这里:
success: function(data)
{
$('#form-control').html(data);
}
用于id
,所以放置
<div id='form-control'></div>
在 table 之后的第一个代码中,它提供了 AJAX return 数据。
如果您打算将 .form-control
用于 class,那将不起作用,因为您在元素存在之前引用了它。
您需要更多地了解 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)
. - 如果你想pass, page & student到编辑页那么你可以通过post params
- 在文件
edit-student-data.php
中,您使用了变量$take['student_name']
。这是该页面的未定义索引。如果您想在该文件中使用 student_name。您需要$_POST['ar_name']
,这是您在 ajax params 中指定的名称
注意:您可以指定动态值,而不是名称和 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>
文件:编辑-学生-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>
我希望这有用。