0
<?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

5
  • Are you not using a <form>? (if you do please add it to your question)
    – brombeer
    Commented Jun 13, 2023 at 8:44
  • How are javascript or css related?
    – brombeer
    Commented Jun 13, 2023 at 8:45
  • Never store plain text passwords. PHP has password_​hash
    – brombeer
    Commented Jun 13, 2023 at 8:47
  • @brombeer nope am not using <form> also the reason why i stored password in plain text is i dont know how to encrypt it as i said am new in php
    – roiden
    Commented Jun 13, 2023 at 9:04
  • "also i use this method in my other project and its work" If it works you're most likely using a <form> there and overlooked that
    – brombeer
    Commented Jun 13, 2023 at 9:11

1 Answer 1

1

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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.