0

Cannot seem to get this working - Will only work with styling gone. Is there any reason why I cannot get it to working whilst styling is left in? Are there any particular settings I need to change? I've tried most things suggested online, but it still wont work. I've been at this for hours now.

<?php

if(isset($_POST['register'])) {
    echo "Working";
}


?>

<form method="post" action="register.php">
    <div class="row mb-3">
        <div class="col-md-6">
            <div class="form-floating mb-3 mb-md-0">
                <input class="form-control" id="inputFirstName" type="text" placeholder="Enter your first name" />
                <label for="inputFirstName">First name</label>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-floating">
                <input class="form-control" id="inputLastName" type="text" placeholder="Enter your last name" />
                <label for="inputLastName">Last name</label>
            </div>
        </div>
    </div>
    <div class="form-floating mb-3">
        <input class="form-control" id="inputEmail" type="email" placeholder="name@example.com" />
        <label for="inputEmail">Email address</label>
    </div>
    <div class="row mb-3">
        <div class="col-md-6">
            <div class="form-floating mb-3 mb-md-0">
                <input class="form-control" id="inputPassword" type="password" placeholder="Create a password" />
                <label for="inputPassword">Password</label>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-floating mb-3 mb-md-0">
                <input class="form-control" id="inputPasswordConfirm" type="password" placeholder="Confirm password" />
                <label for="inputPasswordConfirm">Confirm Password</label>
            </div>
        </div>
    </div>
    <div class="mt-4 mb-0">
        <div class="d-grid">
            <a class="btn btn-primary btn-block" type="submit" name="register" id="register"> Create Account</a>
        </div>
    </div>
</form>

</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="js/scripts.js"></script>
</body>
</html>
Martin
  • 22,212
  • 11
  • 70
  • 132
yodo
  • 1
  • 4
  • is all the styling done with bootstrap? also, what's in scripts.js? – DCR Jul 20 '22 at 17:20
  • You do not have an HTML input field called `register`, instead you name an HTML anchor tag with the `name='register'` attribute. This is incorrect. This is why the PHP is never detecting a value when the form is submitted. – Martin Jul 20 '22 at 17:22
  • Your "button" is a `href` instead of an input, which will treat it as a regular GET link instead of a form submission. You need to change it to an actual button. – aynber Jul 20 '22 at 17:22
  • [Read more Here](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input) about Input HTML form elements. – Martin Jul 20 '22 at 17:23
  • 2
    Each of your ` – Martin Jul 20 '22 at 17:25
  • @Martin something tells me `js/scripts.js` is handling the form post – TCooper Jul 20 '22 at 17:41

2 Answers2

0

You have to fix small mistake to make it run. For example

Wrong:

<a class="btn btn-primary btn-block" type="submit" name="register" id="register"> Create Account</a>

Right:

<input class="btn btn-primary btn-block" type="submit" name="register" id="register" value="Create Account">

OR

<button class="btn btn-primary btn-block" type="submit" name="register" id="register"> Create Account</button>

You can use A Tag to submit a form but that is totally different thing. This post will help you in case you want it: a tag as a submit button?

Here is the complete working version of your code

<?php

if(isset($_POST["register"])) {
    echo "Working";
}


?>

<form method="post" action="">
    <div class="row mb-3">
        <div class="col-md-6">
            <div class="form-floating mb-3 mb-md-0">
                <input class="form-control" id="inputFirstName" type="text" placeholder="Enter your first name" />
                <label for="inputFirstName">First name</label>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-floating">
                <input class="form-control" id="inputLastName" type="text" placeholder="Enter your last name" />
                <label for="inputLastName">Last name</label>
            </div>
        </div>
    </div>
    <div class="form-floating mb-3">
        <input class="form-control" id="inputEmail" type="email" placeholder="name@example.com" />
        <label for="inputEmail">Email address</label>
    </div>
    <div class="row mb-3">
        <div class="col-md-6">
            <div class="form-floating mb-3 mb-md-0">
                <input class="form-control" id="inputPassword" type="password" placeholder="Create a password" />
                <label for="inputPassword">Password</label>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-floating mb-3 mb-md-0">
                <input class="form-control" id="inputPasswordConfirm" type="password" placeholder="Confirm password" />
                <label for="inputPasswordConfirm">Confirm Password</label>
            </div>
        </div>
    </div>
    <div class="mt-4 mb-0">
        <div class="d-grid">
            <button class="btn btn-primary btn-block" type="submit" name="register" id="register"> Create Account</button>
        </div>
    </div>
</form>

</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="js/scripts.js"></script>
</body>
</html>
S M Jobayer Alam
  • 241
  • 1
  • 10
0
  1. First of all, replace the label tags with input tags. These do not cause any problems, just the way you wrote is unusual and irregular.

  2. Second, you have to write the register button with the button or input tag, and this is your main problem.

or

<button class="btn btn-primary btn-block" type="submit" name="register" id="register"> Create Account</button>