Wordpress form posting - php

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.

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 pass variable using post method?

There are a page page1.php below:-
<?php
$name = "xyz";
?>
<form action="page2.php" method="POST">
Age : <input type="number" name="age">
<button type="submit">SUBMIT</button>
</form>
after submitting the form at page1,
How to get value of $name of page1.php in page2.php
what should change in page-1?
If you ask also for a name on page1, then simply use another input:
<form action="page2.php" method="POST">
Name : <input type="text" name="name">
Age : <input type="number" name="age">
<button type="submit">SUBMIT</button>
</form>
If the name is fixed and you want it transmitted via POST, usa a hidden input:
<form action="page2.php" method="POST">
Name : <input type="hidden" name="name" value="<?php echo htmlspecialchars($name) ?>">
Age : <input type="number" name="age">
<button type="submit">SUBMIT</button>
</form>
Then in page2.php read the POST-ed value:
$name = $_POST['name'];
You can submit the parameters in page1 by setting the hidden mode in the form,like this:
<input type="hidden" name="field_name" value=$name >
You can get it in Page2
You can use a hidden input type.
Just be aware that users can edit these values so you should consider any vulnerability this may bring.

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 - HTML - Displaying input form even after submit

I have two input forms and would like the second one to stay on the page even when it is submitted.
<div id="first">
<form method="POST">
Number: <input type="text" name="number"><br>
<input type="submit" value="Submit">
</form><br>
</div>
<div id="second">
<?php
if (isset($_POST['number']) && !empty($_POST['number'])){
?>
<form method="POST">
Name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form><br>
<?php
}
?>
</div>
<div id="third">
<?php
if (isset($_POST['name']) && !empty($_POST['name'])){
echo "TEST";
}
?>
</div>
When I submit my first form, the second form appears correctly since $_POST['number'] is not empty. However, the content of 'number' disappears as soon as I submit it.
Then, when I submit the second form, the word "TEST" appears correctly but the form itself disappears since $_POST['number'] from the first form is now empty.
I need to find a way to somehow save the value of number in the first form so that the second form does not disappear.
Any suggestions?
You can add hidden field:
<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
Then your second form will be changed to:
<form method="POST">
Name: <input type="text" name="name"><br>
<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
<input type="submit" value="Submit">
</form

how to pass values from one page to another on jquery form submit

I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.

Categories