英文:
if (isset($_POST['UpdateBtn'])){ } to stored my input in variable and call the function but when I clink the button name(UpdateBtn) nothing happened
问题
Here is the translated part of your provided content:
<?php
if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">Update list</button>';
}else{
echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">Add list</button>';
}
?>
if (isset($_POST['UpdateBtn'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$password = $_POST['password'];
$type = $_POST['type'];
updatelist($pdo,$id,$fname,$lname,$username,$password,$type);
}
if (isset($_POST['UpdateBtn'])){
echo "hello world";
}
And here's the translation of your function:
function updatelist($pdo, $id, $fname, $lname, $username, $password, $type){
try {
$sql ='UPDATE users set fname=:fname, lname=:lname,username=:username,password=:password,type=:type WHERE id=:id';
$statement = $pdo->prepare($sql);
$statement->bindValue(':id', $id);
$statement->bindValue(':fname', $fname);
$statement->bindValue(':lname', $lname);
$statement->bindValue(':username', $username);
$statement->bindValue(':password', $password);
$statement->bindValue(':type', $type);
$statement->execute();
echo '<script>alert("Order successfully updated")</script>';
echo '<script>window.location="page2.php"</script>';
} catch(Exception $e) {
echo 'Message: ' . $e->getMessage();
$pdo = null;
$sql = null;
}
}
And finally, the translated code for your input form:
<div class="row inputRow">
<div class="row inputRow">
<div class="col-2">
<h5>First name</h5>
<input type="text" class="form-control" placeholder="fname" name="fname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $fname;} ?>">
</div>
<div class="col-2">
<h5>Last name</h5>
<input type="text" class="form-control" placeholder="lname" name="lname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $lname;} ?>">
</div>
<div class="col-2">
<h5>Username</h5>
<input type="text" class="form-control" placeholder="username" name="username" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $username;} ?>">
</div>
<div class="col-2">
<h5>Password</h5>
<input type="text" class="form-control" placeholder="password" name="password" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $password;} ?>">
</div>
<div class="col-6">
<h5>User type</h5>
<select class="form-select" aria-label="Default select example" name="type">
<option value="administrator" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='administrator'){ echo 'selected';} ?>>Administrator</option>
<option value="customer" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='customer'){ echo 'selected';} ?>>Customer</option>
</select>
</div>
</div>
</div>
I've translated the relevant parts of your PHP code, as requested.
英文:
<?php
if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">Update list</button>';
}else{
echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">Add list</button>';
}
?>
what am trying to say is when i click the update list button from the code above i expecting if (isset($_POST['UpdateBtn'])) to work but everytime i click nothing happened
if (isset($_POST['UpdateBtn'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$password = $_POST['password'];
$type = $_POST['type'];
updatelist($pdo,$id,$fname,$lname,$username,$password,$type);
}
i try this to see if it echo the hello world but still not working
if (isset($_POST['UpdateBtn'])){
echo "hello world";
}
this is my code for my function that save in other php files the rest of the code save in same files
function updatelist($pdo,$id,$fname,$lname,$username,$password,$type){
try {
$sql ='UPDATE users set fname=:fname, lname=:lname,username=:username,password=:password,type=:type WHERE id=:id';
$statement = $pdo->prepare($sql);
$statement->bindValue(':id',$id);
$statement->bindValue(':fname',$fname);
$statement->bindValue(':lname',$lname);
$statement->bindValue(':username',$username);
$statement->bindValue(':password',$password);
$statement->bindValue(':type',$type);
$statement->execute();
echo '<script>alert(" Order successfully updated")</script>';
echo '<script>window.location="page2.php"</script>';
}catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
$pdo = null;
$sql = null;
}
}
this is my code where i can insert a input
<div class="row inputRow">
<div class="row inputRow">
<div class="col-2">
<h5> first name<h5>
<input type="text" class="form-control" placeholder="fname" name="fname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $fname;} ?>">
</div>
<div class="col-2">
<h5> last name<h5>
<input type="text" class="form-control" placeholder="lname" name="lname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $lname;} ?>">
</div>
<div class="col-2">
<h5> username<h5>
<input type="text" class="form-control" placeholder="username" name="username" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $username;} ?>">
</div>
<div class="col-2">
<h5> password<h5>
<input type="text" class="form-control" placeholder="password" name="password" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $password;} ?>">
</div>
<div class="col-6">
<h5> User type<h5>
<select class="form-select" aria-label="Default select example" name="type">
<option value="administrator" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='administrator'){ echo 'selected';} ?>>administrator</option>
<option value="customer" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id =='customer'){ echo 'selected';} ?>>customer</option>
</select>
</div>
</div>
i already ask this to my previous question but i think i didn't properly explain what happened also i use this method in my other project and its work i dont know why in this its not am new in php
答案1
得分: 1
你需要将按钮和其他输入元素放在一个带有 method="post" 的 form 标签内,然后你可以在 $_POST 中接收按钮点击的响应。
更新后的代码看起来像这样:
<form action="" method="post">
<div class="row inputRow">
<div class="col-2">
<h5>名字</h5>
<input type="text" class="form-control" placeholder="名字" name="名字" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $名字;} ?>">
</div>
<div class="col-2">
<h5>姓氏</h5>
<input type="text" class="form-control" placeholder="姓氏" name="姓氏" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $姓氏;} ?>">
</div>
<div class="col-2">
<h5>用户名</h5>
<input type="text" class="form-control" placeholder="用户名" name="用户名" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $用户名;} ?>">
</div>
<div class="col-2">
<h5>密码</h5>
<input type="text" class="form-control" placeholder="密码" name="密码" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $密码;} ?>">
</div>
<div class="col-6">
<h5>用户类型</h5>
<select class="form-select" aria-label="默认选择示例" name="类型">
<option value="管理员" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='管理员'){ echo 'selected';} ?>>管理员</option>
<option value="客户" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='客户'){ echo 'selected';} ?>>客户</option>
</select>
</div>
<?php
if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">更新列表</button>';
}else{
echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">添加列表</button>';
}
?>
</div>
</form>
英文:
You will need to add the buttons and other input elements inside a form tag with method="post", then you can receive the button click response in $_POST.
Updated code will look like this.
<form action="" method="post" >
<div class="row inputRow">
<div class="col-2">
<h5> first name<h5>
<input type="text" class="form-control" placeholder="fname" name="fname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $fname;} ?>">
</div>
<div class="col-2">
<h5> last name<h5>
<input type="text" class="form-control" placeholder="lname" name="lname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $lname;} ?>">
</div>
<div class="col-2">
<h5> username<h5>
<input type="text" class="form-control" placeholder="username" name="username" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $username;} ?>">
</div>
<div class="col-2">
<h5> password<h5>
<input type="text" class="form-control" placeholder="password" name="password" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $password;} ?>">
</div>
<div class="col-6">
<h5> User type<h5>
<select class="form-select" aria-label="Default select example" name="type">
<option value="administrator" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='administrator'){ echo 'selected';} ?>>administrator</option>
<option value="customer" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id =='customer'){ echo 'selected';} ?>>customer</option>
</select>
</div>
<?php
if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">Update list</button>';
}else{
echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">Add list</button>';
}
?>
</div>
</form>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论