【发布时间】:2020-07-24 00:05:40
【问题描述】:
我似乎无法从表单中检索 PHP POST 值。输入值正在从 Ajax 中检索并插入到下面的输入表单中。但是,表单提交测试无法从输入中检索值。我可以知道问题是什么以及如何解决吗?提前致谢。
UpdateProfile.php
<?php
session_start();
include("dbcon.php");
if(isset($_POST['updateProfile']))
{
$profileCode=$_POST['profileCode'];
$profileName=$_POST['profileName'];
$profileDesc=$_POST['profileDesc'];
echo "<script>alert('".$profileCode."')</script>";
$find_user="select * from profile where profileCode='$profileCode'";
$statement = $dbcon->prepare($find_user);
$statement->execute();
if($row = $statement->fetch())
{
echo "<script>alert('".$profileCode."')</script>";
}
else
{
echo "<script>alert('Failed to update profile!')</script>";
}
}
?>
<script>
function editBtn(profileCode) {
$.ajax({
type:"POST",
url: "test.php",
dataType: "html",
data: {profileCode:profileCode},
success: function(data){
$('#profileCode').val(data.split(",")[0]);
$('#profileName').val(data.split(",")[1]);
$('#profileDesc').val(data.split(",")[2]);
event.preventDefault();
}
});}
</script>
<?php
$username = $_SESSION["username"];
$query = "SELECT * FROM profile WHERE username='$username'";
$statement = $dbcon->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?>
<i class="ti-pencil" onclick="editBtn('<?php echo $row['profileCode']; ?>')"></i>
<?php } ?>
<form role="form" method="post" action="manageprofile.php">
<label>Profile Code</label>
<input type="text" class="form-control border-input" id="profileCode" name="profileCode" value="" disabled>
<label>Profile Name</label>
<input type="text" class="form-control border-input" id="profileName" name="profileName" value="" placeholder="Profile Name" required>
<label>Profile Description</label>
<textarea rows="5" class="form-control border-input" placeholder="About you" id="profileDesc" name="profileDesc" value=""></textarea>
</form>
test.php
<?php
include("dbcon.php");
$courseCode = $_POST['profileCode'];
$query = " SELECT * FROM profile WHERE profileCode='$profileCode' ";
$statement = $dbcon->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
echo $row['profileCode'].",".$row['profileName'].",".$row['profileDesc'];
} ?>
Image: Post return no value in alert
错误代码:第 7 行 ($profileCode=$_POST['profileCode'];) //点击提交后
【问题讨论】:
-
你的 PHP 错误日志是怎么说的?
-
能否请您提供一下现在输出的截图。
-
您需要向我们展示
test.php页面,这是 AJAX 发送已发布数据的位置,和/或 manageprofile.php 页面,该页面是表单发送已发布数据的位置...... -
data: {profileCode:profileCode}profileCode设置在哪里? -
您的 SQL 面临 SQL 注入攻击的风险,需要紧急进行改进。你需要use prepared statements with your variables in the SQL