PHP echo not posting - php

It's only a simple file but the PHP echo is not showing up.
This is the code:
<!DOCTYPE HTML>
<html>
<body>
Welcome <?php echo $_GET['firstname']; ?><br>
Your new Account is: <?php echo $_GET['accountname']; ?>
<h4>Please clarify that the information below is correct</h4>
Account Name: <?php echo $_GET['accountname']; ?>
Contact Name: <?php echo $_GET['firstname']; ?> <?php echo $_GET["lastname"]; ?>
Address: <?php echo $_GET['address']; ?> <?php echo $_GET["street"]; ?> <?php echo $_GET["direction"]; ?> <?php echo $_GET["state"]; ?> <?php echo $_GET["zip"]; ?> <?php echo $_GET["pobox"]; ?>
</body>
</html>
And all it displays is "Welcome_______" etc. It shows all the words but not the echo outputs even when there are inputs in the form. It's like they are all being recognized as blanks.

You need two files in order to better understand it.
index.html
Set of data from this file will be sent for processing.
<form action="account.php" method="GET">
<input name="firstname" type="text" placeholder="First name here...">
<input name="lastname" type="text" placeholder="Last name here...">
<input type="submit" value="Process data">
</form>
account.php - Here will be data processed
<?php
if(isset($_GET['firstname']) && isset($_GET['lastname')){
//if all data are set, say hello
echo "Welcome ".$firstname." ".$lastname.",";
}else{
//if first name or last name is not set, redirect to form
header('Location: index.html');
exit;
}
?>
You are sending data from simple HTML form to PHP script by GET. Difference between GET and POST method is that, GET is posted in URL: http://www.example.com/index.php?firstname=kamil and POST data are sent inside of request body (url is not changed). Difference is better described in this thread: What is the difference between POST and GET?

It will work when:
http://your_url.com?firstname=xyz&accountname=abc
$_GET['firstname'],$_GET['accountname']
Or use:
var_dump($_GET);

Related

How can i remember data given by user

I want to write a code which would be remember name given by user and on next visit will welcome him with this given name. I don't really understand cookies and session yet so I would be thankful for any help. I wrote something like this:
File: 1.php
<?php
session_start();
?>
<html>
<form action="2.php" method="post">
Name:<input type="text" name="name"/></br>
<input type="submit" value="send"/>
</form>
</html>
<?php
$name=$_POST['name'];
setcookie('name',$name,time()+3600*24);
$_SESSION['name']=$name;
?>
File: 2.php
//2.php
<?php
session_start();
if(isset($_COOKIE['name']))
echo "Hello".$_SESSION['name'];
else
echo "Cookie doesnt exist";
?>
In the example below, 1.php is just used for submitting to 2.php so no PHP code is being used.
1.php
<html>
<form action="2.php" method="post">
Name:<input type="text" name="name"/><br>
<input type="submit" name="submit" value="send"/>
</form>
</html>
I've shown both instances of this below on how to set a cookie and session. You can refresh just 2.php and the cookie output should still show the value of $_COOKIE['name'].
2.php
<?php
session_start();
if (isset($_POST['name'])) {
$_SESSION['username'] = $_POST['name'];
setcookie('name',$_SESSION['username'],time()+3600*24);
}
//Session Value will show in first instance
echo "Session Name: " . $_SESSION['username'] . "<br>";
//Cookie Value will not how until you refresh page
echo "Cookie Name: " . $_COOKIE['name'] . "<br>";
?>
Edit: Variables will not be overwritten when the page is refreshed.

How to get value to work in php and html tag?

The first file (which is html) has two inputs and the second file (php) is the form to display the data when submitted. I want the php file to print whatever message is typed into the input and submitted.
html file:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome_get.php file:
<html>
<body>
Welcome <?php echo $_GET["name"];?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
I know it is probably very simple but for some reason, whenever I type something into the input and submit, it doesn't display the values I inputted. It just displays only the message in the php file:
Welcome
Your email address is:
Use isset to check the variables exist before you use them. i.e.
<html>
<body>
<?php
if(isset($_GET['name'])){
echo 'Welcome '. $_GET["name"] .'<br>';
}
if(isset($_GET['email'])){
echo 'Your email address is: '. $_GET["email"] .'<br>';
}
?>
</body>
</html>
additionally, it's worth ensuring you have all errors displayed by putting this at the top of your file:
<?php
display_errors(E_ALL);
ini_set('display_errors', 'on');
?>
finally, check you're php install is working correctly, by creating a new file (phpinfo.php) with the following contents:
<?php
phpinfo();
?>
open this in the browser, and check you get the about php page.

$_GET not working properly

i have a code in which i received two variable($isbn,$eno) from former page via form GET method but these two variables are not working if i am not echo out it on my page the code for the same is given below.
<?php
error_reporting(E_ALL);
require 'db/connect.php';
if(isset($_POST['generatereport']))
{
$isbn=$_GET['isbn'];
$eno=$_GET['eno'];
echo $eno; //if this is not done then i am not receiving data from database
echo $isbn; //if this is not done then i am not receiving data from database
$studentdata="select * from users where eno='$eno'";
if($studentresult=$db->query($studentdata))
{
$studentrow = $studentresult->fetch_assoc();
}
else
{
echo"fetching error";
}
$bookdata="select Lpad(isbn,'10','0') as isbn,book_name from book_data where isbn='$isbn'";
if($bookresult=$db->query($bookdata))
{
$bookrow = $bookresult->fetch_assoc();
}
else
{
echo"fetching error";
}
}
?>
<!doctype html>
<html lang='en'>
<head>
</head>
<body>
<div id='report'>
<table>
<tr><td><h3>Issue Report</h3></td></tr>
<tr><td><h4>Student details</h4></td></tr>
<tr><td>UNIQUE ID:<?php //random number here ?></td></tr>
<tr><td>Enrollment:<?php echo $eno; ?></td></tr>
<tr><td>Name:<?php echo strtoupper($studentrow['fname']);echo strtoupper( $studentrow['lname']); ?></td></tr>
<tr><td>Branch:<?php echo strtoupper($studentrow['branch']); ?></td></tr>
<tr><td>Semester:<?php echo $studentrow['sem']; ?></td></tr>
</table>
<hr/>
<table>
<tr><td><h4>Book details</h4></td></tr>
<tr><td>isbn:<?php echo $bookrow['isbn']; ?></td></tr>
<tr><td>Book Name:<?php echo strtoupper($bookrow['book_name']);?></td></tr>
</table>
<hr/>
<form action="script/issue.php?isbn=<?php echo $isbn;?>" method='post' id='report'>
<input id="btn_issue" type="button" value="Issue this Book"/>
<input id="btn_close" type="button" value="cancel"/>
</form>
</div>
</body>
</html>
You need to set attribute name to inputs in your form, after that you can access to value by GET or POST
Change form method:
<form action="script/issue.php?isbn=<?php echo $isbn;?>" method='get' id='report'>
<input name ='isbn' id="btn_issue" type="button" value="Issue this Book"/>
<input name ='eno' id="btn_close" type="button" value="cancel"/>
</form>
Or get your variables from post, like:
$isbn=$_POST['isbn'];
$eno=$_POST['eno'];
change your from method to get
<form action="script/issue.php?isbn=<?php echo $isbn;?>" method='get' id='report'>
or you can use $_REQUEST in php which can read either get or post
If you use method="POST", it meant you should use $_POST for your next process. Can use $_REQUEST also. But I think $_POST is more specifics for method POST. Please read the documentation of PHP about form method again.

Pass form data to another page with php

I have a form on my homepage and when it is submitted, it takes users to another page on my site. I want to pass the form data entered to the next page, with something like:
<?php echo $email; ?>
Where $email is the email address the user entered into the form. How exactly do I accomplish this?
The best way to accomplish that is to use POST which is a method of Hypertext Transfer Protocol https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
index.php
<html>
<body>
<form action="site2.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>
site2.php
<html>
<body>
Hello <?php echo $_POST["name"]; ?>!<br>
Your mail is <?php echo $_POST["mail"]; ?>.
</body>
</html>
output
Hello "name" !
Your email is "whatyou#addedonindex.com" .

ECHO page1.php textarea values into page2.php

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)

Categories