comunication between pages in php - php

HI,
I wrote this code in php.
<head>
<title>listent</title>
</head>
<body>
<form action="untitled 3.php">
<input type = "text" name = "user">
<br>
<textarea name = "address" rows = "10" cols = "40">
</textarea>
<br>
<input type = "submit" value = "heat it">
<br>
<select name="combobox" multiple[]>
<option>mehdi
<option>nine
</select>
</form>
</body>
</html>
now when i click on submit button untitled 3.php is run.
in untitled 3.php i wrote
<?php
print "welcome $user";
?>
but it has error.
Notice: Undefined variable: user in C:\xampp\htdocs\me\Untitled 3.php on line 4
welcome
what is problem?how can i solve it?

Form values don't just magically appear as variables anymore - at least not in any decently modern and properly configured PHP installation. You need to do $_GET["user"] to access the value which is sent by the form (into the URL - you might want to read about the difference between GET and POST)
And please, please use more descriptive names for your files...

PHP Globals wont survive the new page.
In you case you must use the POST variables sent by your form.
So in untitled3.php you should have
echo "welcome ".$_POST['user'];
PS : I would avoid spaces in PHP filenames.

First you should specify a Form submission method in your first page:
<form action="untitled 3.php" method="post">
Then you have access to all posted values in the $_POST array in untitled 3.php:
$user = $_POST['user'];

Related

HTML5 & PHP Contact Form

I am assigned with creating an HTML page that accepts user input and then verifying the user input on a php script (upon clicking submit on the HTML page). Thus far, I am only trying to verify the first name variable of the HTML page onto the PHP page.
My issue is that the PHP page is not displaying the HTML variables, 'fname' I've looked around for a good solution but I am unable to find anything yet
I am using Aptana Studio 3 and I am running XAMPP. I have posted my HTML and PHP code below for you to view. Any help would be greatly appreciated :)
<!DOCTYPE html>
<html>
<head>
<title>
Assignment Four, HMTL/PHP Form
</title>
<meta charset = "UTF-8">
</head>
<body>
<!--Begin the body code-->
<h1>Assignment Four: HTML/PHP Form</h1>
<p>Watch the following video, write a brief review and then click submit!</p>
<!--Post the Scholartica Video-->
<iframe width="560" height="315" src="//www.youtube.com/embed/d_2_8n86jA8" frameborder="0"
allowfullscreen></iframe>
<h3>Complete the following information:</h3>
<form method = "post" action = "form.php">
<p><label>First Name</label>
<input type = "text" name = "fname"></p>
<p><label>Last Name</label>
<input type = "text" name = "lname"></p>
<p><label>Email:</label>
<input type = "text" name = "email"></p>
<p><label>Phone:</label>
<input type = "text" name = "phone"
placeholder = "(123) 456-7890"></p>
<h3>Write a brief review about the video here</h3>
<p><textarea rows = "4" cols="50" name = "review">What did you think?
</textarea></p>
<h3>Rate the quality of the video</h3>
<select name = "Rating">
<option>Poor</option>
<option>Average</option>
<option>Good</option>
<option>Excellent</option>
</select>
<p><input type = "submit" name ="submit" value ="Register"></p>
</form>
</body>
</html>
My PHP form looks like this
<!DOCTYPE html>
<html>
<body>
Hi <?php echo $_POST["fname"];?>
Thank you for finishing the survey
Your responses have been recorded.
</body>
</html>
I ran the code and it seems to work.
Entering name = "Bob" and clicking submit returns page with
Hi Bob Thank you for finishing the survey Your responses have been recorded.
Make sure that:
Your second page that displays the result is called "form.php"
And also have both files in the same directory

Why am I getting this PHP error?

So here's my full code
<!DOCTYPE html>
<html>
<body>
<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>
And here's the error I get
Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20
Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?
As a general practice for forms in php, always check if the submit button has been clicked.
First name your submit button:
<input type="submit" name="submit">
then further in your php:
if (isset($_POST['submit'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).
If you really need 2 forms, name your submit buttons differently and then you can call them separately.
<input type="submit" name="submit-in">
<!-- ... -->
<input type="submit" name="submit-out">
<?php // ...
if (isset($_POST['submit-in'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
if (isset($_POST['submit-out'])) {
// do your stuff, eg...
$dencrypt = $_POST['out'];
}
EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).
you need to first check if the form has been sent, if it hasn't then $_POST['in'] does not yet exist thus throwing the error
May be nothing but you called a php script after closing the form /form, the body /body and then then the HTML /html
replace this code $encrypt = $_POST['in']; by this $encrypt = #$_POST['in'];
this is an error on client server when you upload this file on remote server you will not saw this. use # sign on the client server when you saw this error in future.

Can't seem to pass SESSION variables of POST

Sorry if this is a duplicate, but I really cant find anything that could solve my problem. I can pass numbers and strings like $_SESSION['blabla']="123'; but I can't pass this $_POST value from the textfield and submit button.
Page 1 (sessions.php)
<?php session_start(); ?>
!doctype stuff here
<body>
<form id="form1" name="form1" method="post" action="sessions2.php">
<label>
<input type="text" name="damn" id="damn" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
<?php
$omg = $_POST['damn'];
$_SESSION['damn'] = $omg;
echo $_SESSION['damn'] ;
?>
Page 2 (sessions2.php)
<?php
session_start();
$fires = $_SESSION['damn'];
echo "wth";
echo $_SESSION['damn'];
?>
PS. Sorry for the names.. I'm truly stumped.
You need to put the code that reads from $_POST in the file that you submit the form to.
Currently your process is:
Get request for sessions.php
Send form to browser
Assign $_POST['damn'] (which is undefined) to the session.
User submits form
Get request for sessions2.php
Ignore $_POST (which is now populated)
Read from the session (where the variable is still undefined).
damn is populated in the form submission request (step 4/5) not the request where you are trying to read it (step 1).
In sessions2.php
// you POST "damn" variable via form, using post method, so:
$fires = $_POST['damn'];
// and:
$_SESSION['damn'] = $fier;
// or
$_SESSION['damn'] = $_POST['damn'];
PHP code in file sessions.php doesn't work, because in form action you have session2.php.

how to pass value from one php page to another using session

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

Beginner Help: Creating and Storing Text Using PHP

I'm trying to write a program where the basic idea is I ask the user for input in a textarea, and then the text gets stored into a word file. Here is the code I'm trying to use:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
action = "mysite.php">
<textarea name = "text"
rows = "10"
cols = "20">Write Here</textarea>
<input type = "submit"
value = "Submit Comment">
</form>
<?
if($_POST['text'] !== NULL){
$comment = $_POST['text'];
$file = fopen("texttest.txt", "a");
fputs($file, "<br>\n$comment");
fclose($file);
}
?>
</body>
</html>
I can't seem to get this to work properly. I was also thinking about somehow making the form action store the text and then reload the site, but I haven't gotten that to work (the original file is mysite.php, so the action is to just reload the page).
If anyone has any better ideas of an algorithm to use/different syntax to use, please let me know, as I just started learning basic PHP syntax.
Thanks
Check the following:
Does php have the permission to write files in that directory?
Is that php file called "myfile.php"?
Anyway, when something does not work and you want to know what's causing the arror, place error_reporting(-1); at the beginning of your php - it will output any error or warning, including the ones trown by fopen().
Also, you might want to check whether the variable has been correctly submitted: echo $comment right after you assign it.
Something like this might work.
You might want to do more with the values they are entering and all, but this will basically do what you are asking.
You will also want to make sure that you have the correct path of the file you are trying to write to and that that file has the correct permissions to allow it to be written to:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1><br>
<?php
if (isset($_POST['submit'])) {
if (strlen(trim($_POST['comment']))) {
$file = fopen("texttest.txt", "a");
fputs($file, "$_POST['comment'])\n");
fclose($file);
}
} else {
?>
<form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<label>Leave your comment
<textarea name="comment" rows="10" cols="20"></textarea>
</label>
<input type="submit" name="submit" value="Submit Comment" />
</form>
<?php
}
?>
</body>
Also, since you are returning to the same page you may want to put some kind of message letting the person know that they succeeded in entering something into your address book.

Categories