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)}>
G
ghardin137247d ago
You’d need to call PreventDefault on the event object
P
pepega페페가247d 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
G
ghardin137247d 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
P
pepega페페가247d ago
Fixed it all with manual fetching, ty a lot
S
ScriptyChris246d 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)
UU
Unknown User245d ago