How to pass a value and echo it within a form - php

hello i have a page where users can register, and so after registeration i want to pass the username and password from my registeration form to another form on a next page, mean while this form on the next page should hold username and password values that are passed from the first page.
the form is echoed correctly but the values dont come correctly.
this is what i ahve tried
echo '<form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="one" id="one">
<input type="text" placeholder="Username" name="Username" value="<?php echo $_POST["Username"]; ?>" >
<p> </p>
<input type="password" placeholder="Password" name="Password" value="<?php echo $_POST["Password"]; ?>">
<br>
<input name="Submit" type="submit" class="btn" value="Sign In" />
</form>';
but the values dont come out, is there a right way to echoe this thanks

echo '<form ACTION="'.$loginFormAction.'" METHOD="POST" name="one" id="one">
<input type="text" placeholder="Username" name="Username" value="'.$_POST["Username"].'" >
<p> </p>
<input type="password" placeholder="Password" name="Password" value=".'$_POST["Password"].'">
<br>
<input name="Submit" type="submit" class="btn" value="Sign In" />
</form>';
Try This And Reply

You don't do this with echo, because the echo is just some HTML on the other page. You can also save them.
What you want is a form most likely:
Page 1
<form action="page2.php" method="post">
Name: <input type="text" name="username" /><br />
<input type="submit" name="submit" value="Send" />
</form>
Page2
<?php
echo "Your sent username: ", $_POST['username'];
?>

Related

$_POST variable contains no data

I'm having a basic form like this:
<form method="post" action="register.php" class="form">
<input id="a" type="text" placeholder="Cod acces" name="access-code" size="20" required /><br>
<input id="b" type="password" placeholder="Parola" name="password" autocomplete="new-password" size="20" required /><br>
<input id="c" type="password" placeholder="Confirma parola" name="re-password" autocomplete="new-password" size="20" required /> <br><br>
<input type="submit" value="Register" name="register" />
</form>
In register.php i have the following 3 lines of code:
$password = $_POST["password"];
$repassword = $_POST["re-password"];
$acces_code = $_POST["access-code"];
Even if this code is as simple as it looks, my $_POST variable is empty. Even weirder, if I press F12 to see the request data, all variables and it's values are there.
I'm using XAMPP on Windows.
Here is the code I'm also with Windows version 10 and Xampp Server.
In your index.php paste this code below
<html>
<body>
<form method="post" action="register.php" class="form">
<input id="a" type="text" placeholder="Cod acces" name="access-code" size="20" required /><br>
<input id="b" type="password" placeholder="Parola" name="password" autocomplete="new-password" size="20" required /><br>
<input id="c" type="password" placeholder="Confirma parola" name="re-password" autocomplete="new-password" size="20" required /> <br><br>
<input type="submit" value="Register" name="register" />
</form>
</body>
</html>
and then in your register.php paste this code below
<?php
if(isset($_POST['register'])){
echo $password = $_POST["password"]." ";
echo $repassword = $_POST["re-password"]." ";
echo $acces_code = $_POST["access-code"]." ";
}
Successfully getting and outputting your input on the form. Hope this will help you

How to keep form data even after submiting not to dispear

In this
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
After I submit the form, the page refreshes and the data inside the input texts gets blank
Is it possible to keep the data even after submit?
Regards.
You can simply use ajax for submitting the form.
Or use following
<form method="POST" action=""><input type="text" class="field small-field" name="tex1" value="<?php (isset($_POST['text1]))? echo $_POST['text1] : '';" /><input type="submit" value="search" name="search"/><input type="submit" value="print" name="print"/></form>
try to echo, what ever is the variable named for your input.
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1'];?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
With php for example:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1']; ?>"/>
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
If you are handling the post on the same page you could just do like this on the fields where you want the posted value to be shown:
<input type="submit" value="search" name="search" <?php if( isset( $_POST['search'] ) ){ echo "value=\"". $_POST['search'] ."\"; } ?>/>
Use this:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php if(isset($_POST['tex1'])) echo $_POST['tex1'] ?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
Bascially http is statelessprotocol , hence you need to save the data some where
The simplest way in this case would be to use a conditional operator
<input type="text" class="field small-field" name="tex1" value="<?php echo (isset($_POST['search'] || $_POST['search'] )?$_POST['tex1']:''); ?>" />

WordPress login form code

I have the following in a WordPress sidebar widget that serves as an online community for my niche. Right now I am able to display the form when the user is not logged in and to display the "Welcome to the Community!" message when the user is logged out -- which is great. However, the login functionality doesn't seem to be working. Could I please get some help?
<form name="loginform" id="loginform" action="https://domain.org/wp-login.php"
method="post">
<p class="login-username">
<label for="user_login">Username</label>
<input type="text" name="log" id="user_login" class="input" value=""
size="20" />
</p>
<p class="login-password">
<label for="user_pass">Password</label>
<input type="password" name="pwd" id="user_pass" class="input"
value="" size="20" />
</p>
<p class="login-submit">
<input type="submit" name="wp-submit" id="wp-submit" class="button-
primary" value="Log In" />
<input type="hidden" name="redirect_to"
value="https://domain.org/" />
</p>
</form>
<?php } else { ?>
<h4>Welcome to the Community!<h4>
<?php } ?>
try this...
if (!is_user_logged_in()) {
wp_login_form();
} else {
echo '<h4>Welcome to the Community!</h4>';
}
You can also pass a configuration array to wp_login_form() See http://codex.wordpress.org/Function_Reference/wp_login_form

HTML input field jumps

I have 2 text fields, user and password. When I enter the password, it jumps to the first one. I have no idea why that is happening. I couldn't find why it is jumping. How do I change it so the password form doesn't jump? Here is the code:
<?php
session_start();
require_once 'database.php';
if (isset($_SESSION['user'])){
echo "Welcome ".$_SESSION['user'];
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
<br /><form name="news" method="post" action="news.php">
<input type="submit" name="news" id="news" value="News">
</form>
<?php
}
elseif(isset($_SESSION['admin'])){
echo"Welcome ".$_SESSION['admin'];
echo"<br><br>You are logged in as an Admin";
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
</form>
<?php
}else{
?>
<form name="login_form" method="post" action="login2.php">
<label>
<input name="user" type="text" id="user">ID<br />
<input name="pass" type="password" id="pass">Password<br />
</label>
<input type="submit" name="login" id="login" action="index.php" value="Login">
</label>
</p>
</form>
<form name="Register" method="post" action="reg.php">
<input type="submit" name="register" id="register" value="Register">
</form><br />
<form name="news" method="post" action="news.php">
<input type="submit" name="news" id="news" value="News">
</form>
<?php
}
?>
You have both of the inputs wrapped in one label. The browser is getting confused and it thinks that that entire content is a label for the first input (that's how labels work, apparently). You should only use <label> to wrap text for input.
This:
<label>
<input name="user" type="text" id="user">ID<br />
<input name="pass" type="password" id="pass">Password<br />
</label>
Should be this:
<input name="user" type="text" id="user">
<label for="user">ID</label><br />
<input name="pass" type="password" id="pass">
<label for="pass">Password</label><br />

How to process multiple forms using one php script

I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?
NB. I hope this makes much sense?
html code
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
php script
<?php
if (isset($_POST['submitForm'])) {
echo('<script>alert("Form Submitted")</script>');
}
?>
when I click on submit for any particular form, it submits all the forms.
this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)
however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.
Also note that none of your text fields being sent to the server as they have no names.
I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!
well, it seems you want to ask how to distinguish these forms.
add a hidden field into each
<input type="hidden" name="step" value="1" />
and then in PHP
if ($_POST['step'] == 1) {
//first form
}
if ($_POST['step'] == 2) {
//second
}
This submits one form of many to php. Copy, paste, test, and study.
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:
Array
(
[A] => e
[B] => f
[C] => g
[D] => h
[submitForm] => Submit Form
)
#Jay
Actually its not hard.
Once you supply form names, your work is done. the DOM does the rest.
write one php block to do your functions (create/update/retrieve/delete)
Whichever button is clicked, by default it submits only the elements enclosed together with it.
if(!empty($_POST)){
if(isset($_POST['submit'])){
print "<pre>";
var_dump($_POST); // write your code here as you would
print "<pre>";
}
}
try this with your form above.
I know this is an old post but here's how I solve this very problem.
All you need to do is make sure the submit buttons in each form have different names. Eg:
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>
Then, you simply check which form's submit button was pressed.
<?php
if (isset($_POST['submitForm1'])) {
echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) {
echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) {
echo('<script>alert("Form 3 Submitted")</script>');
}
?>
If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this.
Here, I used while($n<10) for 10 dynamic forms.
You can also use tag as below if you need separate form names.
<form action="" name="form<?=$n?>" method="post">
This will create separate form names such as form1, form2, etc but not necessary here.
<?php
if (isset($_POST['submitForm'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
$n=0;
while($n<10) {
$n++;
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<?php
}
?>
Sample page with output when I click row 5..

Categories