James Marshall-Osborne
James Marshall-Osborne

Reputation: 104

PHP post request from HTML page showing blank input fields

Currently looking to implement functionality to edit details in MySQL database via a HTML page. The page itself shows all data in the database which matches the unique id of the user who is is logged in via a PHP session and echos that data to input boxes in a while loop.

When the user makes changes to the input text and hits the save changes link it then calls the edit endpoint which in turn calls the edit SQL function in a functions file.

I'm using an anchor tag wrapped in a button to send the id of the row that is being edited and all this sits inside a POST action form.

However the input texts are only showing as blank as if the endpoint is not receiving the text in the input field, and despite trying quite a few different methods I can't seem to get a result.

Code for Web page (not whole page but only concerned code)

    <?php
    $connect =mysqli_connect('localhost','root','','micaddy');
    $id_query = mysqli_query($connect, "SELECT unique_id FROM users WHERE email = '{$_SESSION['login_user']}'");
    $id_array = mysqli_fetch_assoc($id_query);
    $uid = $id_array['unique_id'];

    $result = mysqli_query($connect, "SELECT * FROM clubs WHERE user_id = 
    '$uid'");

    ?>
    <div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading clearfix"><h3 class="panel-title"><strong>Your Golfbag</strong><button type="button" class="btn btn-info btn-lg pull-right" data-toggle="modal" data-target="#addModal">Add Club</button></h3></div>
                <?php while($row=mysqli_fetch_assoc($result)):?>
                &nbsp <span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
                    <div class="panel-body">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-5">
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><h3 class="panel-title"><strong><?php echo $row['club_type'];?></strong></h3></div>
                                            <div class="panel-body">
                                            <form id="" method="POST" action="editClub.php">
                                                <div class="form-group">
                                                <label for="clubType">Club ID</label>
                                                    <input type="text" readonly="" class="form-control" id="inputClubType" value="<?php echo $row['id'];?>" name="clubIdInput">
                                                </div>
                                                <div class="form-group">
                                                    <label for="clubBrand">Type</label>
                                                    <input type="text" class="form-control" id="inputclubBrand" value="<?php echo $row['club_type'];?>" name="clubTypeInput">
                                                </div>
                                                <div class="form-group">
                                                    <label for="clubBrand">Brand</label>
                                                    <input type="text" class="form-control" id="inputclubBrand" value="<?php echo $row['brand'];?>" name="clubBrandInput">
                                                </div>
                                                <div class="form-group">
                                                    <label for="clubNum">Number or Type</label>
                                                    <input type="text" class="form-control" id="inputclubNum" value="<?php echo $row['club_number'];?>" name="clubNumInput">
                                                </div>
                                                <div id="deleteClub">
                                                    <button id="submitChange" type="button" class="btn btn-danger btn-lg"><?php echo "<a href='deleteClub.php?id=".$row['id']."'>Delete</a>" ?></button>
                                                    <button type="button" class="btn btn-info btn-lg"><?php echo "<a href='editClub.php?id=".$row['id']."'>Save Changes</a>" ?></button>
                                                </div>
                                                <span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
                                                </form>
                                            </div>
                                    </div>
                                </div>
                                <div class="col-md-5">
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><h3 class="panel-title"><strong>Club Image</strong></h3></div>
                                            <div class="panel-body">
                                                    <div class="form-group">
                                                        <img src="club_images/<?php echo $row['clubImg']; ?>" class="img-rounded" width="250px" height="250px" alt="Image"/>
                                                    </div>
                                            </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
                    </div>
                <?php endwhile;?>

            </div>
        </div>
    </div>
</div>     

The edit endpoint:

<?php
session_start();
$error='';

require_once '../include/DB_Functions.php';
$db = new DB_Functions();

    if(empty($_POST['clubBrandInput']) || empty($_POST['clubNumInput'])){
        $_SESSION['message'] = "Warning: Some fields are blank! Please try again";
        header("Location: golfbag.php");
    } else{

        if(isset($_POST['clubBrandInput']) && isset($_POST['clubTypeInput']) && isset($_POST['clubNumInput'])){

        $brand = $_POST['clubBrandInput'];
        $type = $_POST['clubTypeInput'];
        $num = $_POST['clubNumInput'];

        $id = $_GET['id'];

        $club = $db->editclub($brand, $type, $num, $id);
            if($club) {
                    header("Location: golfbag.php");
                    $_SESSION['message'] = "Success! Details edited.";
                }else{
                    header("Location: golfbag.php");
                    echo $error;
                }           
        }
    }

?>

The function method:

public function editClub($brand, $type, $num, $id){

$stmt = $this->conn->prepare("UPDATE clubs SET brand = '$brand', club_type = '$type', club_number = '$num' WHERE id = '$id'");
$result = $stmt->execute();
$stmt->close();

if($result){
    $stmt = $this->conn->prepare("SELECT * FROM clubs WHERE user_id = ?");
    $stmt->bind_param("s", $uid);
    $stmt->execute();
    $club = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    return $club;
}else{
    return false;
}
}

Upvotes: 0

Views: 195

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94672

You do not have a <form> defined in this HTML.

You are also clicking an anchor link <button id="submitChange" type="button" class="btn btn-danger btn-lg"><?php echo "<a href='deleteClub.php?id=".$row['id']."'>Delete</a>" ?></button>

even though it is in a button.

Therefore you will only pass the id=".$row['id']." parameter to the endpoint and that will be passed in the $_GET array and not the $_POST array

Upvotes: 1

Related Questions