So, I have a signup form. In it, are the fields (like first name, last name, email, password, retype password, etc.), and the signup button. So, from the signup page, the user can actually just change the url from login.php to home.php and access the homepage the wrong way. So, for that, I used a small PHP logic to prevent that. Here's that PHP code:
if(isset($_POST['sub'])) {
header("location:home.php");
} else {
header("location:login.php");
}
So, I kept this PHP code on the home.php page, because, that's the page the user is accessing wrongly. So, 'sub', is just the name for the signup button. Here's the HTML code for that button:
<input type="button" type="submit" value="Create An Account" name="sub" class="but" id="press" style="margin-top:-1px; font-family:'Rajdhani'; border: none; border-radius:25px; outline: none" onclick="checkEmail()"></input>
So, with that PHP code, it's working. Like, I can't change the URL to home.php when on the signup page. If I try to do that, it will just send me back to the signup page, the same page, so I can't access the homepage, unless I submit the signup form correctly. So, here's where the issue arises. If I submit the signup form correctly, and everything is validated correctly as well, when I click on the signup button, it should redirect to the homepage, because I accessed it correctly, which it did previously, without this PHP code. But now, with this PHP code, when I fill in all the fields for the signup form correctly, and click the signup button, it just sends me back to the same page (signup.php), and doesn't send me to the homepage (home.php). Here is the action to my form (where all the fields and submit button is):
<form method="post" action="javascript:void(0)" id="mainForm">
It is javascript:void(0), because on another file, I have some AJAX running for error messages, so it's not home.php, but I think this is the reason it isn’t working, because the action is not home.php, but if I keep it as home.php, the AJAX won’t work, so I need to keep it as javascript:void(0) for the AJAX to work. So, I need to keep the action as javascript:void(0), but at the same time, ensure that the PHP url thing is working as well.
So, in conclusion, the logic where you can't change the URL is working. If they try to change the URL to home.php, it will just redirect back to the same page, login.php, which is good. Now, previously without that PHP code, when you signup, you would be taken to the homepage (where the data is stored) and the data would be represented using sessions. This would happen, you would get redirected, only if every field was filled and validated correctly. Even now, with this PHP code, it's only occurring when each field is validated, but when correct, it's just redirecting to the same page, not the home page. So, I need to resolve this particular issue.
So, any help? Please. Thank you.
My Answer:
It seems like the issue is with the logic in your PHP code. When the form is submitted correctly, you want to redirect the user to the homepage. However, your current logic is redirecting the user to the login page if the form is submitted, regardless of whether it was submitted correctly or not.
You can update your PHP code to check if the form was submitted correctly before redirecting the user to the homepage. You can do this by adding a condition to check if all the form fields are filled and validated correctly before redirecting. Here's an example of how you can update your PHP code:
php
if(isset($_POST['sub'])) {
// Check if all form fields are filled and validated correctly
if(/* Add your validation logic here */) {
header("location:home.php");
} else {
header("location:login.php");
}
}
You will need to replace `/* Add your validation logic here */` with your actual validation logic to check if all form fields are filled and validated correctly. Once the validation is successful, the user will be redirected to the homepage.
Additionally, you may need to update your form action to point to the correct PHP file that handles the form submission. If you are using AJAX for form submission, you can keep the action as `javascript:void(0)` and handle the form submission using AJAX.
I hope this helps resolve the issue you are facing. Let me know if you need further assistance.
Rate this post
3 of 5 based on 4600 votesComments