I have jQuery code that should load the contents of 'submittest.php' into the p#formMessage
element.
When the user clicks submit they are redirected to submittest.php and what they entered as username is printed which is not the desired result as the username should be outputted below the form and the user should never be redirected to submittest.php
I have tried a few things, like using classes instead of ids, which did not work. I have tried adding quotation marks in almost every possible permutation. I have directly copied code from online and only changed variable names. I am very confused about what the problem is.
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var name = $("#username").val();
var email = $("#password").val();
var message = $("#passwordConfirm").val();
var submit = $("#adminSubmit").val();
$("#formMessage").load("submittest.php", {
name: name,
email: email,
message: message,
submit: submit
});
});
});
<script src="https://code.jquery.com/jquery-3.6.2.min.js" integrity="sha256-2krYZKh//PcchRtd+H+VyyQoZ/e3EcrkxhM8ycwASPA=" crossorigin="anonymous"></script>
<form action="submittest.php" method="post">
<label for="username"> enter your new username: </label><br>
<input type="text" id="username" name="username"><br>
<label for="password">enter your new password:</label><br>
<input type="text" id="password" name="password"><br>
<label for="passwordConfirm"> confirm your password: </label><br>
<input type="text" id="passwordConfirm" name="passwordConfirm"><br>
<input type="submit" class="adminSubmit" name="adminSubmit" value="Create your account">
<p id="formMessage"></p>
</form>
submittest.php just looks like this:
<?php echo $_POST["username"] ?>
edit 2: i changed the way i get jquery by CDN, which DOES go before the other code, and the code goes before the form, and all console errors have gone but i still get the problem that unwanted redirection happens even with updated code