I have 2 php files in my folder. In page1.php, there's a textarea, user should enter some values in it. In page2.php, it will grab what is in the textarea and work with its program. But I can't find a command that grabs the value in textarea. Can someone help me?
page1.php:
<?
$hello = "hello";
?>
<html>
<input type = "text" name = "user_input">
</input>
</html>
page2.php
<?
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
?>
So, is there anyone that can solve this? =/
Use $_GET or $_POST in page2.php
page1.php
<?
$hello = "hello";
?>
<html>
<form method="get" action="page2.php" enctype="multipart/form-data">
<input type = "text" name = "user_input">
<input type="submit">
</form>
</html>
page2.php
<?
$text=$_GET['user_input'];
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
echo $text;
?>
You may use either $_GET['user_input'] or $_POST['user_input'].
The difference is, you can see the data in the url (visible to everyone) when using GET method and not in the other method.
Also, always use <input> elements (which you want to pass to another file) inside a <form> and specify action="file.php", to where you want to pass data, and the method, either method="get" or method="post", like;
<form method="get" action="page2.php">
also specify the method to grab data in the target file also, like;
$text=$_GET['user_input']; or $text=$_POST['user_input'];
And in your case, you may use;
Method 1
<?php
$hello = "hello";
?>
<html>
<form method="get" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_GET['user_input'];
echo $text;
?>
Method 2
<?php
$hello = "hello";
?>
<html>
<form method="post" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_POST['user_input'];
echo $text;
?>
If you want to share the data over a number of pages, you may do this using PHP Session or saving the data in a cookie.
1. Using Sessions
<?php
session_start();
$_SESSION['data'] = 1; // store session data
echo "Pageviews = ". $_SESSION['data']; //retrieve data
?>
Make sure you add session_start(); on every page you want to handle session
You can read more about php sessions here www.tizag.com/phpT/phpsessions.php/
2. Using Cookie
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
and retreive it using
echo $_COOKIE["user"];
You can read more about php sessions here http://www.w3schools.com/php/php_cookies.asp
hope this helps...:)
basically your page1.php is a page with some form in it with a text area. Now user will have to fill it and submit the form to page2.php. You can't echo it's content like that, because that will be on browser subject to user actions. Use a form and submit the data to page2.php. Like this:
page1.php
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="t1">
</textarea>
</form>
</body>
</html>
page2.php
<?php
$textAreaContents = isset($_POST['t1'])?$_POST['t1']:'';
echo "You submitted: ".$textAreaContents;
?>
if i were you i should use sessions for this. that is where they were made for..
example:when user clicks on submit.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['post'] = $_POST;
}
that is where every post variable will be put in a session.
and your inputbox will be something like this..
<textarea name="message" type="text" value="" rows="0" cols="0" placeholder="" ><?php if(isset($_SESSION['post'])){echo $_SESSION['post']['message'];} ?></textarea>
?>
note that you now can use every post variable that you used in your form by echo (example)
echo $_SESSION['post']['message']
where message is the name of the inputbox. in this case of the textarea
don't forget that at the end when you don't want to use the session anymore use session_destroy(); otherwise you will keep having it in your form. and don't forget session_start(); above every page where you are planning to use sessions ( it must be at 1st line of your document at all times)
Related
I have a PHP form that is located on file contact.html.
The form is processed from file processForm.php.
When a user fills out the form and clicks on submit,
processForm.php sends the email and direct the user to - processForm.php
with a message on that page "Success! Your message has been sent."
I do not know much about PHP, but I know that the action that is calling for this is:
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
How can I keep the message inside the form div without redirecting to the
processForm.php page?
I can post the entire processForm.php if needed, but it is long.
In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.
For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.
Like this:
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
The best way to stay on the same page is to post to the same page:
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
There are two ways of doing it:
Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the form action to the current page URL.)
if(isset($_POST['submit'])) {
// Enter the code you want to execute after the form has been submitted
// Display Success or Failure message (if any)
} else {
// Display the Form and the Submit Button
}
Using AJAX Form Submission which is a little more difficult for a beginner than method #1.
You can use the # action in a form action:
<?php
if(isset($_POST['SubmitButton'])){ // Check if form was submitted
$input = $_POST['inputText']; // Get input text
$message = "Success! You entered: " . $input;
}
?>
<html>
<body>
<form action="#" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Friend. Use this way, There will be no "Undefined variable message" and it will work fine.
<?php
if(isset($_POST['SubmitButton'])){
$price = $_POST["price"];
$qty = $_POST["qty"];
$message = $price*$qty;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="post">
<input type="number" name="price"> <br>
<input type="number" name="qty"><br>
<input type="submit" name="SubmitButton">
</form>
<?php echo "The Answer is" .$message; ?>
</body>
</html>
You have to use code similar to this:
echo "<div id='divwithform'>";
if(isset($_POST['submit'])) // if form was submitted (if you came here with form data)
{
echo "Success";
}
else // if form was not submitted (if you came here without form data)
{
echo "<form> ... </form>";
}
echo "</div>";
Code with if like this is typical for many pages, however this is very simplified.
Normally, you have to validate some data in first "if" (check if form fields were not empty etc).
Please visit www.thenewboston.org or phpacademy.org. There are very good PHP video tutorials, including forms.
You can see the following example for the Form action on the same page
<form action="" method="post">
<table border="1px">
<tr><td>Name: <input type="text" name="user_name" ></td></tr>
<tr><td align="right"> <input type="submit" value="submit" name="btn">
</td></tr>
</table>
</form>
<?php
if(isset($_POST['btn'])){
$name=$_POST['user_name'];
echo 'Welcome '. $name;
}
?>
simple just ignore the action attribute and use !empty (not empty) in php.
<form method="post">
<input type="name" name="name">
<input type="submit">
</form>
<?PHP
if(!empty($_POST['name']))
{
echo $_POST['name'];
}
?>
Try this... worked for me
<form action="submit.php" method="post">
<input type="text" name="input">
<input type="submit">
</form>
------ submit.php ------
<?php header("Location: ../index.php"); ?>
I know this is an old question but since it came up as the top answer on Google, it is worth an update.
You do not need to use jQuery or JavaScript to stay on the same page after form submission.
All you need to do is get PHP to return just a status code of 204 (No Content).
That tells the page to stay where it is. Of course, you will probably then want some JavaScript to empty the selected filename.
What I do is I want the page to stay after submit when there are errors...So I want the page to be reloaded :
($_SERVER["PHP_SELF"])
While I include the sript from a seperate file e.g
include_once "test.php";
I also read somewhere that
if(isset($_POST['submit']))
Is a beginners old fasion way of posting a form, and
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Should be used (Not my words, read it somewhere)
I'm a bit inexperienced so go easy on me.
I need to save the value from a form textarea before the form is submitted (I need it even after the page is reloaded).
After the reload, I need to redirect to a predefined page on the site that includes the textarea value on the very end on the URL.
I have something like this so far:
<php?
session_start();
$_SESSION['textarea_value'] = $_POST['textarea_name'];
?>
// below is called directly after a popup form submission
location.reload();
if ($_SESSION['textarea_value'] != null) {
header("Location: http://www.xxxxxxxx.com/?s=$_SESSION['textarea_value']");
unset($_SESSION['textarea_value']);
}
Okay, so here's what I would do:
Method #1
index.php
<?php
if($_POST)
{
session_start();
$_SESSION["someVar"] = $_POST["someVar"];
header("Location:otherPage.php");
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
session_start();
$someVar = $_SESSION["someVar"];
?>
Method #2: If you want to do it with a get request, you don't even need to use sessions:
index.php
<?php
if($_POST)
{
header("Location:otherPage.php?someVar=".$_POST["someVar"]);
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_GET["someVar"];
?>
Method #3: You could also even take out the redirection entirely:
index.php
<form action="otherPage.php" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_POST["someVar"];
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Warning: Headers already sent” in PHP
Why does this not work? I expect to see whatever is input into the text box in first.php displayed back out in test.php.
<html>
<body>
<form action="test.php">
Test: <input type="text" name="test" />
<br> <input type="submit" name="submit" />
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
</body>
</html>
Here is test.php
<?php
session_start();
$check = $_SESSION['test'];
echo $check;
?>
session_start() must be called before outputing anything(even not white space) to the browser.
you are starting session after html start session on the very first line of page like
<?php
session_start();
print_r($_SESSION);
if(isset($_POST['submit'])){
$_SESSION['test'] = $_POST['test'];
}
?>
<html>
<body>
<form method="post" action="addimageprocess.php">
Test: <input type="text" name="test" />
<br>
<input type="submit" name="submit" />
</form>
</body>
</html>
session_start() should be set before any headers are sent (i.e. at the very top of the page)
The PHP code is executed before the actual POST is done, because PHP is a server-side language. At that point, there is actually no $_POST array.
This code, from your eg.
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
The $_POST is posted (or exists), to the test.php page.
The PHP in your main script is not doing what you want it to do, in fact is just saving junk and or null in best of the cases. You need to inverse the PHP scripts, put
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
in test.php and
<?php
session_start();
$check = $_SESSION['test'];
echo $check;
?>
in you main php
If you are using a webserver, and you do have
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
but it still isn't storing the session then you will need to check the saved_path
<?php
phpinfo():
?>
check under the session header to see if the session:save_path is set and make sure that it exists on the server. if its a webserver then you will have to contact your host to set up the save path for you.
I can pass values form one page to another but I need to pass value like this,
Page 1:
Page4.php
Page3.php
I need to pass the value in a text field in the Page1.php to a text field in Page2.php, since the form is not directly redirectly to page2, I am unable to pass the value, I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
Edit..........
I found the answer, thanks for the help, it was actually a careless mistake on my part, I used $_post instead of $_session.
Its working now.
Thanks for the help.
Use something like this:
page1.php
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
Any other PHP page:
<?php
session_start();
echo $_SESSION['myValue'];
?>
A few notes to keep in mind though: You need to call session_start() BEFORE any output, HTML, echos - even whitespace.
You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.
The setting of the variable itself can be done in one of a number of ways:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
And if you want to check if the variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}
Solution using just POST - no $_SESSION
page1.php
<form action="page2.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
page2.php
<?php
// this page outputs the contents of the textarea if posted
$textarea1 = ""; // set var to avoid errors
if(isset($_POST['textarea1'])){
$textarea1 = $_POST['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
Solution using $_SESSION and POST
page1.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
$textarea1 = "";
if(isset($_POST['textarea1'])){
$_SESSION['textarea1'] = $_POST['textarea1'];
}
?>
<form action="page1.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
<br /><br />
Go to page2
page2.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
// this page outputs the textarea1 from the session IF it exists
$textarea1 = ""; // set var to avoid errors
if(isset($_SESSION['textarea1'])){
$textarea1 = $_SESSION['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
WARNING!!! - This contains no validation!!!
I saved inputs from this form to another php page as variables. How can I get those variables to into a different page with <title>$title</title>.
Basically transfer variables from one page to another with the same info.
INDEX.HTML
<!DOCTYPE html>
<html>
<head></head>
<body>
<form action="action.php" method="POST">
<p>Video Title</p> <input type="text" name="title"> <br>
<p>Video Link</p> <input type="text" name="Link"> <br>
<p>Description</p> <input type="text" name="desc"> <br>
<p>Page Name</p> <input type="text" name="pagename"> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
ACTION.PHP (Saved Variables)
$title = htmlspecialchars($_POST['title']);
$link = htmlspecialchars($_POST['link']);
$desc = htmlspecialchars($_POST['desc']);
$pgname = htmlspecialchars($_POST['pagename']);
VIDEO.PHP
<title><?php echo $_POST["$title"]; ?></title>
Use $_SESSION to use the variables to another page
action.php
session_start();
$_SESSION['title']= htmlspecialchars($_POST['title']);
// all the other variables
video.php
session_start();
echo $_SESSION['title'];
This is one of the way you can do it, But $_SESSION is the best for doing this.
Action.php
$title = htmlspecialchars($_POST['title']);
$_GET['title'] = $title;
Video.php
<title><?php echo $_GET["title"]; ?></title>
If you interested on $_SESSION then, initialize / start the session in each page, set the session in action page and get the session from video page. for more: visit
Use Session Variables. Making sure to start a new session on each page.For example, if you want to transfer variables from action.php to video.php, do the following:
/* In Action.php*/
session_start();
$_SESSION['title']= htmlspecialchars($_POST['title']);
/*Do the same for the rest of variables */
/*In Video.php*/
session_start();
echo $_SESSION['title'];
/*Do the same for the rest of the variables you saved in the Session super global array in 'action.php'