0

How can I post data in html without open the php-file?

I tried to write an own php script which gets a string over the post method in html forms, but when I run it with action="file.php", the localhost displays the file.php file. Why?

21st Jun 2018, 6:20 PM
Raphael Burkardt
Raphael Burkardt - avatar
9 Réponses
+ 14
An XHR request. It's a background form that does not redirect. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest Note that you would have to resolve the data manually like: http://server.com/page.php?user=abc&pass=def
21st Jun 2018, 6:30 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 15
There's probably something equivalent for php but i think you'd need to do it with js on this case. (the only way to grab data and send to server are html forms and js xhrs, php can do requests but cant get the data by itself)
22nd Jun 2018, 11:03 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 13
This would just redirect you back, might not be the best experience for the user but works fine.
21st Jun 2018, 6:27 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
Forms are supposed to redirect the data you input to the action page, alternatively, you could cast an XHR or change the redirection target of the form.
21st Jun 2018, 6:23 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
it should as it passes the data to it. you can put a redirect to redirect to the page you were on
21st Jun 2018, 6:23 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 3
normally nice as you get to make a file do a piece of logic else, you can post to the file itself https://stackoverflow.com/questions/5826784/how-do-i-make-a-php-form-that-submits-to-self
21st Jun 2018, 6:32 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 2
Thank you for the quick answers. I googled it and came to header("Location: file.html);
21st Jun 2018, 6:26 PM
Raphael Burkardt
Raphael Burkardt - avatar
+ 1
What could be a better solution? Or what exactly is an XHR? Because when I change the redirection target, the data isn't sent to the php-script
21st Jun 2018, 6:29 PM
Raphael Burkardt
Raphael Burkardt - avatar
0
Can I somehow use XMLhttpRequest directly in php or do I have to use js?
22nd Jun 2018, 5:12 AM
Raphael Burkardt
Raphael Burkardt - avatar