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

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.

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">

PHP POST does not redirect properly

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

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">

Old data gets wiped from POST when I use GET

So I am having an issue. I used POST to send data to a new page. I use get to send data to a function but it seems the POST data get wiped. Here some code to help explain.
POST CODE to send to form vieworder (works perfect!)
<form method="post" action="vieworder.php">
<input type="hidden" name ="user_id" value="<?php echo $_SESSION['user_id']; ?>">
<input type="hidden" name ="id" value="<?php echo $data1[$x]['id']; ?>">
<input type="submit" name="submit" value="View"> </td>
</form>
So on the vieworder page I want used to be able to update the data using this form.
This form works as well except i need that value "id" from the orginal post. It works and the "id"has the data until I use this form.
<form name="approveform" method="get" action="">
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
I would also prefer to use the POST method but using GET was my first solution to no deleting the data from POST.
Anyways I then just send the data to a function to update two fields.
Any way to get correct the code?
<?php
$id=$_POST['user_id'];
?>
<form name="approveform" method="get" action="">
Index Number*: <input type="text" name="IndexNum">
<input type='hidden' value='<?php echo $id;?>'>
<input type="submit" value="Approve" action="">
</form>

PHP Form to redirect with form results

I have a form that when they submit it, it stores the info in the database. I need to be able to get the form data to come up on redirect page.
It does not need to fetch the database as I would love to do this PHP style. So lets say they enter there name and city. When they click submit it redirects them to a thank-you page with the results from the form on that page.
In a form, you have each element have a name, lets say name="username", in the php, yould get the value of this as either a get, or a post response, depending on the method of the form.
HTML Form
<form action="process.php" method="get">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
or
<form action="process.php" method="post">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
process.php
$someusername = $_GET['username'];
$someusername = $_POST['username'];
Form page:
<form action="thanks.php" method="post"><input type="text" name="myname" placeholder="Name" /><input type="text" name="mycity" placeholder="City" /><input type="submit" value="submit"></form>
PHP Page
print "Thanks, ".$_POST['myname']." who lives in ".$_POST['mycity']."!";

Categories