pepega페페가
pepega페페가17mo ago

pepega1337. – 21-16 Aug 15

Hi, I have a form that calls my PHP script on the server How can I prevent the page from re-directing to mypage.com/login.php when the submit button is pressed? When I use e.preventdefault() it's not even called obviously, but I want to stay on the same page.. <form action="login.php" method="POST" onSubmit={(e) => handleSubmit(e)}>
6 Replies
ghardin137
ghardin13717mo ago
You’d need to call PreventDefault on the event object
pepega페페가
pepega페페가OP17mo ago
Yeah ofc im doing that but when I preventDefault then my form is not even submitted, there is no api call not even in Fiddler, but when I remove preventDefault then it works... But it redirects me to mypage.com/login.php which i dont want... If I try to remove the form and make it a div, and manually fetch the api on button click then again it doesnt even try fetching, I have no idea tbh
ghardin137
ghardin13717mo ago
You’d have to submit it manually Like with fetch/axios Without PreventDefault it’s going to do what a form does in html which is do a full request And your js isn’t going to have time to do anything
pepega페페가
pepega페페가OP17mo ago
Fixed it all with manual fetching, ty a lot
ScriptyChris
ScriptyChris17mo ago
And you should keep using <form> for forms - <div> is not the appropriate element to wrap a form (e.g it doesn't support built-in validation)
reactibot
reactibot17mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1141118240827916429

Did you find this page helpful?