Unable to keep data in form after submitting - php

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

Related

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

Wordpress form posting

I am facing a problem while self posting form data, when I hit submit button the page should display the data inserted in the input box,
but it does not show the data....
Here is my example form
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$mail = $_POST['mail'];
echo $mail;
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="myForm">
Mail : <input id="mail" name="mail" type="text"/>
<input type="submit" value="Submit"/>
</form>
This should obviously display the mail value from the input box. But it does not work. Then I tried to change the action attribute value to "mywordpress/index.php/customer-details-2/"
Since I am new with Wordpress, any help would be highly appreciated.
Add the posted value in your input text box as :
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="myForm">
Mail : <input id="mail" name="mail" type="text" value="<?php echo (isset($_POST['mail'])) ? $_POST['mail'] : '' ?>" />
<input type="submit" value="Submit" />
</form>
I am assuming that you want to show data inside input text box.
Hope it helps you.

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>

$_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.

Showing form data after submit

I have a form with a submit button and a handler that stores data in the database. Problem is when the form is submitted, all data is cleared from the input fields. Is there a way to still show them after submit? What changes do I need to make to my form_submit function?
function mymodule_form_submit($form, &$form_state) {
//how to retain the input in the form
}
I'm looking for the most "drupalish" way to get this done?
As indicated by this previous StackOverflow question you can accomplish this with $form_state['storage'] and $form_state['rebuild'].
You can access the data using $_REQUEST['form_variable_name'] where form_variable_name is the name of the html input tag.
You then need to render the page back putting this value into the input tags value field.
<form method="POST" action="/account/contactdetails/">
<div>
<label>First name:</label>
<input type="text" name="firstname" value="<?php echo $_REQUEST['firstname']; ?>" />
</div>
<div>
<label>Last name:</label>
<input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
</div>
<input type="submit" value="Save" />
</form>

Categories