I want to pass the $username value to doMember.php page from member.php with form :
member :
$username = $_GET['user'];
<form name="member" method="get" action="doMember.php?user=<?php echo $username;?>">
in the doMember.php page :
$username = $_GET["user"];
echo $username;
but the $username in doMember.php is empty. Is there something missing?
HTML and PHP are not the same
$username = $_GET['user'];
echo '<form name="member" method="get" action="doMember.php?user='.$username.'">';
You should not set the parameters in the action. Take a look at the generated HTML, you'll see your form will be sent to "doMember.php?user=", so you'll always send an empty user.
The browser will append all form variables to the action upon submit, so simply put doMember.php.
You should place the username in an
<input type="hidden" name ="username" val="usr">
instead
Form method='post'
The field name of that form must be named "user"
Can't you include the value of $user_name in a hidden variable in your so
$username = $_GET['user'];
<form name="member" method="get" action="doMember.php">
<input type="hidden" id="user_name" name="user_name" value="<?php echo $username; ?>"
.
.
.
</form>
Related
I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];
I can't seem to apply the $_POST function properly to retrieve the data from a simple HTML form. I'm new to PHP, so I may be overlooking something simple.
I have a form with action directing to the same page, but if the form is filled out, the value of $_POST['input_name'] will have changed, so I can trigger the php function using that condition. Is this the best way to structure this kind of action?
Here's my HTML code (thispage.php is the current/same page):
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" />
</form>
Here's my PHP code from the same page:
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
Thanks a lot in advance!!
First remove the action from form if your server side code is in the same page. And Use the empty() or isset() functions for checking the value.
For Example:
if(!empty(trim($_POST['userName']))){
echo $_POST['userName'];
}
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
This is actually checking if the value is empty and echoes it if it is.
You probably want to use !empty($_POST['userName']) to check if it's not empty and then echo it if it is not.
try this:
HTML code
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" name="submit" />
</form>
PHP code on the same page:
if(isset($_POST['submit']))
{
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
}
Try this...
if(trim($_POST['userName']) != ' '){
echo $_POST['userName'];
}
you can try it:
if(isset($_POST['userName'])){
$name = $_POST['userName'];
echo $name;
}
I am making account registeration page with php. My problem is taht, if somebody uses username which is already in use, page reloads and all inputs will empty.
I tried this:
<form action="register.php" method="post">
<input type="text" name="username" value="<?php if(isset($_POST['username'])) { echo $_POST['username']; } ?>" />
</br>
In my opinion, the best way is to send the check for a duplicate username via AJAX. Use JavaScript to send the request a second or two after the user is done typing.
If you don't want to use AJAX, store the field values in the session and spit them back out when the page reloads. This is your page:
<?php if(isset($_SESSION['username'])) { echo $_POST['username']; } ?>
Then, in the form submission code:
$_SESSION['username'] = $_POST['username'];
Your code could work if your form-handling code and you form markup are in the same PHP file, but you certainly aren't violating separation of concerns like that, are you? ;)
You have set the method of your form to POST, so you can't use $_GET, instead you have to use $_POST.
<form action="register.php" method="post">
<input type="text" name="username" value="<?php echo htmlspecialchars($_POST['username']); ?>" />
</br>
I have two pages page one and page_two, in page_one the user enter some information which will be inserted in the database and when the he press enter he should be directed to page_two and inside this page there are the same information that he entered in page_one. and the problem is every time the user refresh page_two the data is inserted in the database again. I tried to fix this issue by using header to a new page, it worked but in page_two the information that was entered in page_one is lost.
page_one
<form action="page_one.php" method="post" name="info" >
<input name="userName" type="text" />
<input name="userEmail" type="text" />
<input name="userPass" type="text" />
<input name="submit" type="submit" />
</form>
<?php
include('db.php');
if(isset($_POST['Login']))
{
$user_name = $_POST['userName'];
$user_email = $_POST['userEmail'];
$password = $_POST['userPass'];
mysql_query("INSERT INTO users VALUES ('$user_name',' $user_email',' $password')");
header("Location:page_two.php.php");
exit;
}
?>
page_two
<?php
$user_name = $_POST['userName'];
$user_email = $_POST['userEmail'];
$password = $_POST['userPass'];
echo 'your user name: '.$user_name;
echo 'your email: '.$user_email;
echo 'your password: '.$password;
<input name="userName" type="hidden" value="<?php echo $user_name; ?>" />
<input name="userEmail" type="hidden" value="<?php echo$user_email; ?>" />
<input name="userPass" type="hidden" value="<?php echo $password; ?>" />
when I try this code it gives me this error message from page_two:
notice undefined index userName
notice undefined index userEmail
notice undefined index userPass
Pass the variables via url to page_two.
So your header will be
header("Location:page_two.php.php?userName=user_name&userEmail=user_email&userPass=password");
Now catch these variables using $_GET on page_two
<?php
$user_name = $_GET ['userName'];
$user_email = $_GET ['userEmail'];
$password = $_GET ['userPass'];
echo 'your user name: '.$user_name;
echo 'your email: '.$user_email;
echo 'your password: '.$password;
You have the correct approach, but on page_2, instead of retrieving the values from the $_POST array, you should retrieve them from the database, as they now exist there. This will remove your undefined index problem.
Redirect using header to some safe page after inserting the data. You can rather use id of the inserted row to get data on page_2.
Hope this helps.
Since you're building a multi-page web-app. I suggest you have to use SESSION to save the posted information of the 1st page, then use the SESSION variable for the 2nd page.
I hope the link below helps.
http://www.html-form-guide.com/php-form/php-order-form.html
On page two you should include a Select statement which will select all the values that are stored in your table.
mysql_query("SELECT * FROM users ");
every time i am refreshing the page and i am getting the same value stored in the post array.
i want execution of echo statement only after submit and after refreshing no echo results..
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
From just a form, you won't be able to check if it was a refresh, or a first submit, regardless of using GET or POST method.
To ensure a single message, you need to:
a. redirect the user to somewhere else after you processed the request.
if(isset($_POST['submit'])) {
// process data
header("Location: new-url");
}
And display the message on the other URL.
b. set a cookie / session variable, which tells you the form was already processed.
if(isset($_POST['submit']) && !isset($_SESSION['form_processed'])) {
$_SESSION['form_processed'] = true;
}
This second approach will kill your form until the user closes the browser, so you should do something more complex - like storing another hidden field in the form, and storing that in the session.
If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.
An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.
Within the form:
<input type="hidden" name="time" value="<?php echo $time; ?>" />
In the PHP:
session_start();
if(isset($_POST['submit']))
{
if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
{
echo "User name : <b> $name </b>";
}
}
$time = $_SESSION['time'] = time();
Another option is to redirect after processing the post data:
if(isset($_POST['submit']))
{
...
...
header('Location: ' . basename($_SERVER['PHP_SELF']));
exit();
}
You need to maintain a state as to whether $name has already been displayed or not. The easiest way is probably to maintain that state in a browser cookie.
<?php
$nonce = $_COOKIE['nonce'];
$new_nonce = mt_rand();
setcookie('nonce', $new_nonce);
if(isset($_POST['submit']) && $_POST['nonce'] == $nonce)
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="nonce" value="<?php echo $new_nonce ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Problems
you are polluting the user “session” with stale variable.
this will break if your user opens several windows (or tabs) to the same page. To fix this you would have to change the nonce cookie into an array of nonces, and update it accordingly.
if you want refresh page after submit use
<form method="get"
sure if your form hasn't a lot of data and also need to use $_GET instead of $_POST variable:)
correct way for you, but this logic is not good, need to refactor this script:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
unset($_POST['submit']);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>