PHP POST does not redirect properly - php

I have two different sites and want to pass data via a form from the first to the second page.
The form looks as follows (simplified):
<form method="POST" action="https://othersite.me/login.php">
<label class="title">
E-Mail
</label>
<input name="luser" type="email">
<label class="title">
Password
</label>
<input name="lpasswd" type="password">
<input type="submit" value="Login">
</form>
The php code othersite.me/login.php looks as follows:
<?php
$email=Format::input($_POST['luser']?:$_GET['e']);
$passwd=Format::input($_POST['lpasswd']?:$_GET['t']);
?>
<form method="POST" action="login.php">
<label class="title">
E-Mail
</label>
<input name="luser" type="email" value="<?php echo $email; ?>">
<label class="title">
Password
</label>
<input type="password" value="<?php echo $passwd; ?>">
<input type="submit" value="Submit">
</form>
Now I expect that when I enter some data on the first page, the data is transferred to otherpage.me/login.php and displayed in the appropriate fields.
Curiously, after pressing the submit button, the website is redirected to othersite.me/login.php for less than a second and then automatically to othersite.me/index.php.
If I use GET instead of POST, the form works as expected and stays on othersite.me/login.php.
How is this possible and how can I fix that. Do you have some ideas?

Why do you need two php file to do this? You can use action="<?php echo $_SERVER['PHP_SELF']; ?>" in your login.php and start processing the submitted data there. You can follow this instruction

I don't have reputation to comment yet so I will answer.
In the othersite.me/login.php if you have permission paste the code below
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
This will return all warnings and fatal errors

Related

Unable to keep data in form after submitting

I've been searching online on how to keep data in the form after submitting it. But after trying for awhile it still doesn't work as expected
This is the code that I tried:
<form action="process_login" method="post" target="_self">
<div class="login-field">
<input type="text" id="login-email-field" name="login-email-field" value="<?php echo isset($_POST["login-email-field"]) ? $_POST["login-email-field"] : ''; ?>" required />
<label class="login-email-label" for="login-email-field">Email/Username</label>
</div>
<div>
<button class="submit-button" type="submit">
Login
</button>
</div>
</form>
I also tried replacing the input (with a 'TEST' string in the value field if the POST is empty) but the 'TEST' string did not appear after submitting the form.
<input type="text" id="login-email-field" name="login-email-field" value="<?php echo isset($_POST["login-email-field"]) ? $_POST["login-email-field"] : 'TEST'; ?>" required />
Any help would be appreciated thanks!
Action specifies a URL to which the form’s data is sent when submitted.
If you want to stay on the same page you can leave it out
<form method="post" target="_self">
or set the name of the actual page.
<form action="actualPage.php" method="post" target="_self">

How to display text from current web address after submitting php form

How to display text from current web address after submitting form
www.example.com/?n=Example-Text
<form method="post" action="/submit.php">
<div class="enter-name">
<input class="animated pulse infinite" type="name" required="" maxlength="50" name="n" placeholder="Enter Your Text Here">
<button class="btn" type="submit"><span>></span> Go</button>
</div>
</form>
If you want to show the text from input name="n" ,
make the form method GET <form method="get" action="/submit.php">
and in your submit.php
<?php
echo $_GET['n'];
?>
You are asking about a GET request when you code has a response that is a POST response as indicated in the form tag <form method="post" action="/submit.php">
For the example you gave, the submit.php page would need to have the following code
<?php echo $_POST['n']; ?>
This will display inline the value that is in the name="n" input once submitted via the form.
if you want to see the information that is being passed to the submit.php file in the url then you need to change the HTML to use a get method for the form instead of the post that it currently uses
<form method="post" action="/submit.php">
<div class="enter-name">
<input class="animated pulse infinite" type="name" required="" maxlength="50" name="n" placeholder="Enter Your Text Here">
<button class="btn" type="submit"><span>></span> Go</button>
</div>
</form>
You have to set get method instead of post,
get passes parameters to url, is called urlencodded,
<form method="get" action="/submit.php">

Empty $_POST in Wordpress Archive Page

I have a custom form in my wordpress theme archive.php file but I can't get the post of that form. It's empty.
I have these:
<?php print_r($_POST); ?>
<div class="filtros">
<h3>Búsqueda de documentos</h3>
<form action="#" method="post">
<input type="text" id="name" name="f_name" placeholder="Buscar" value="<?php echo $_POST['f_name']; ?>" />
<div class="dates">
<input type="text" id="inicio" class="date" name="f_inicio" placeholder="Fecha de inicio" value="<?=$_POST['f_inicio']?>" /> /
<input type="text" id="final" class="date" name="f_final" placeholder="Fecha final" value="<?=$_POST['f_final']?>" />
</div>
<div class="submit"><input type="submit" value="Buscar" /></div>
</form>
</div>
Even if I pass the variables throw the URL I cant get that vars with $_GET.
¿Any idea?
While it's not advisable from a security point of view, you can change the method on your form from method="post" to method="get" to have variables posted as $_GET variables instead.
Change action="#" to action="<? echo $_SERVER['REQUEST_URI']?>"
Try change <?= to <? echo
You may need to point the action="myfile.php" to specific file.. See the solutions here:
$_POST returns empty on form submit in wordpress

$_SERVER['PHP_SELF'] wiping form, not posting

I'm trying to post a form to a URL as below, however it simply just wipes the form and doesn't post anything when clicking submit.
Any ideas? I can't see anything wrong so need another set of eyes.
<form class="form-inline" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="code" class="input" placeholder="Enter Passcode Here..">
<button type="submit" class="btn">Submit</button>
</form>
If I put 12345667890 in the Code input and click submit, I would expect the URL to show
www.domain.com/index.php?code=1234567890. This does not happen, it simply wipes the form and loads www.domain.com/index.php
Your form is method="post".
POST data is encoded into the body of the HTTP request, not into the URL. It will still be accessible to the server side script that the form is being submitted to.
If you want the data to show up in the URL, use method="GET".
(But see the specification for reasons why you should use POST or GET and use those to decide which you should be using).
In using passcode.php as a filename, with an example input of 12345, will output:
http://www.example.com/passcode.php?code=12345 as intended.
Here is the fully tested and working code:
<form class="form-inline" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="code" class="input" placeholder="Enter Passcode Here..">
<button type="submit" class="btn">Submit</button>
</form>
You must use the GET method to achieve this, not POST.
To echo the URL's full string, use the following:
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
<form class="form-inline" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="code" class="input" placeholder="Enter Passcode Here..">
<button type="submit" class="btn">Submit</button>
</form>
In using 12345 as input, will output (echo): http://www.example.com/passcode.php?code=12345
At this moment the form does exactly what you wrote it to do.
It submits the form to the same page. You then don't do anything with the post data. And the script simply returns the same html again.

my form failed to be submitted

I have the following code which works by itself but when I try it on wordpress, it shows the login but when I click on submit, it does not submit the form, and goes to following address and shows a white page.
http://localhost:8080/wordpress/wp-admin/admin.php
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Username <input type="text" name="username" />
<br />
Password <input type="password" name="password" />
<br />
<input type="submit" value="Login" />
<?php
if(isset($_POST["username"])
....
You should perhaps go into more detail of what you are trying to achieve.
Any reason you are not using wp_login_form()?
As for the white screen, I would also recommend to turn on debugging, inside your wp-config.php file is the line define('WP_DEBUG', false); . Change that to true. ( I can only also assume the login showed a white screen because of missing nonce fields, which would go away with using wp_login_form() )
Add name attribute in the form field
<form method="post" name="form_name" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Categories