I tell you, I have a form where only email addresses are loaded
The idea is that when a user enters for example pepito#pepito.com the page says, "you enter pepito#pepito.com" the problem I have with the echo is that when you enter the page, you show me input "you income ", how can I eliminate that?
<?php echo "you income $email"; ?>
I created a simple example for it
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<?php echo !empty($_POST['email']) ? 'you enter ' . $_POST['email'] : ''; ?>
<form action="" method="post">
<input name="email" placeholder="somebody#example.com" />
<input type="submit">
</form>
</body>
</html>
I am using the ternary operator after check the email parameter with empty()
it will check if form is submitted or not and if submitted it will echo the email.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
echo "you entered ". $_POST['email'];
}
?>
<form action="" method="post">
<input name="email" type="email" placeholder="somebody#example.com" required
placeholder="Enter Email">
<input type="submit" name="submit">
</form>
</body>
</html>
Related
i am trying to display variable via session to the another page to whom i send it via action in form. i am using session inside if(isset($_POST['submit']))
seems its not working.
here is my form.php code
<!DOCTYPE html>
<html >
<head>
<meta content="text/html; charset=utf-8" />
<title>form</title>
</head>
<body><form action="submit.php" method="post">
<input type="text" name="username" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
session_start();
if(isset($_POST['submit'])){
$username=$_POST['username'];
if(!empty($username)){
//echo $username;
$_SESSION['username']=$username;
echo 'session is set';
}
}
?>
here is my submit.php code
<?php
session_start();
//var_dump($_SESSION);
echo $_SESSION['username'];
?>
i tried all possible way, not working. i want this way for submitting.
thankyou in advance.
Brother your Data Submit code is written in form.php and you're passing "submit.php" as action. So pass action="form.php"
<!DOCTYPE html>
<html >
<head>
<meta content="text/html; charset=utf-8" />
<title>form</title>
</head>
<body>
<form action="form.php" method="post">
<input type="text" name="username" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
session_start();
if(isset($_POST['submit'])){
$username=$_POST['username'];
if(!empty($username)){
$_SESSION['username']=$username;
echo 'session is set';
//It will redirect to submit.php
header("Location:submit.php");
}
}
?>
I'm trying to make a form that acts as a search engine and returns results. However, the data from the user's entry is either unable to save to a session or the session cannot be passed to another file. Here is the code for the "home" search page and the "Search-Engine" results page.
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
<?php session_register(); session_start(); ?>
<?php $_GET['query'] = $_SESSION['Query']; ?>
</body>
</html>
Search-Engine.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
<?php session_start(); ?>
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I don't know the exact purpose of using Sessions in your form. But you are doing in a wrong way by starting Session in middle of page and using Sessions within the form. You can add value in Sessions in another page after submitting the form.
You can update your files in the below way:
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" name="query" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
</body>
</html>
Search-Engine.php
<?php session_start();
$_SESSION['Query'] = $_GET['query']; ?>
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I tried to execute the following html code:
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="listing.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>
And this is the code of the associated php file (listing.php):
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php echo $_POST["user"]; ?><br>
Your email address is: <?php echo $_POST["address"]; ?>
</body>
</html>
I was able to get the form and enter values as shown below:
Form Input
But, when I clicked 'Send Message', it displays only:
Welcome
Your email address is:
Without the values that I entered through the form.
When I tried to run the php file directly from the local host (http://localhost/listing.php), I received these error messages:
Welcome
Notice: Undefined index: user in C:\xampp\htdocs\listing.php on line 7
Your email address is:
Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8
I even modified the php code as follows, but still got the same output:
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php
if(isset($_POST['submit'];)) {
session_start();
$user = $_POST['user'];
echo "$text";}else {echo 'Could not load text!';}?><br>
Your email address is: <?php echo $_POST["address"]; ?>
</body>
</html>
I would really appreciate it if you could give some advice to make it work.
Thanks
if(isset($_POST['submit'];)) should be changed,so you check if address and user is isset. Furthermore you normally don't want ; in your if statements.
Here i have a optimized version for you. the !empty is added so we also check if the inputs are not empty.
if (isset($_POST["name"] && isset($_POST["address"])) {
if (!empty($_POST["name"] && !empty($_POST["address"]) {
// execute code as u would
}
}
Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8
This is caused by the use of the ';' in the if condition.
Remove that and you should be good. for now.
Try this code:
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php
if(isset($_POST['submit']))
{
session_start();
$user = $_POST['user'];
echo $user;
echo "<br><br>Your Email Address is: ".$_POST['address'];
}
else
{
echo 'Could not load text!';
}
?>
<br>
<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send" name="submit"></p>
</form>
</body>
</html>
In your second code block $text isn't defined anywhere. Do you mean to have $user?
<html>
<head>
<title>Listing Reading input from a form</title>
</head>
<body>
Welcome
<?php
if(isset($_POST['submit'])) {
session_start();
echo isset($_POST['user']) ? $_POST['user'] : "User";
} else {
echo "User";
}
?>
<br>
Your email address is:
<?php
echo isset($_POST["address"]) ? $_POST["address"] : "Address"
?>
</body>
</html>
Try this and let me know:
<?php
session_start();
if(isset($_POST['submit'])) {
$user = $_POST['user'];
echo "Welcome ".$user;
echo "<br>";
echo "Your email address is: ".$_POST["address"];
}else {
// you may get rid of this block if you like
echo 'Could not load text!';
}
?>
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" name='submit' value="send"></p>
</form>
</body>
</html>
I am having an issue with storing $_SESSION variables, i am a little new at PHP and I like it to the extent of my current knowledge.
first my creds:
Win 7 Pro 64
PHP 5
Remote Server(not sure of its config)
So what I am trying to do is set up a Login Page that contains a hardcoded username and password for testing purposes.
I want to display the username on a successor page to confirm it.
Login.php
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Login</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-gb" />
<script type="text/javascript">
// Perform Client side validation of Username and Password
function redirect() {
var username = document.getElementById("username");
var password = (document.getElementById("password"));
if (!username.value == " ") {
if (!password.value == " ") {
if (username.value == "aknight") {
if (password.value == "00226291") {
window.location = "Home.php";
exit();
} else
alert("Username or Password Invalid");
}
} else
alert("Password is required");
} else
alert("Username is required");
}
</script>
</head>
<body>
<div id="header">
<h1>Product Order System</h1>
<br/>
<h2>The most expensive crap youll ever find...</h2>
</div>
<div id="content">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['username'] = $_POST['username'];
}else{
?>
<form action="" method="POST">
<fieldset>
<legend>Login Information</legend>
<p>
<label for="Username">Username:</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="Password">Password:</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="button" name="login" value="Login" onclick="redirect();"/>
<input type="button" name="cancel" value="Cancel" />
</fieldset>
</form>
<?php }
include ('includes/footer.html');
?>
home.php
<?php
session_start();
$page_title = 'Home';
include ('includes/header.php');
if(isset($_SESSION['username'])) { $name = $_SESSION['username'];}else{echo "<p>error</p>";}
echo "<h3>Welcome ".$name."!</h3>" ;
?>
I have tried printing out the $_SESSION values and they are coming up empty.
producing this result
<?php
echo "<pre>".print_r($_SESSION)."</pre>";
?>
Array {
[username] =>
}
Please do not mind the JS, it is preschool client validation just to move forward.
Any help would be greatly appreciated.
Give your login form a name, and then in your JavaScript validation function, instead of window.location, do a document.forms['yourformname'].submit().
I think your Javascript validation is actually what's causing the issue.
You're setting the session variable in Login.php using the POST request, but no POST is actually being sent to Login.php since your Javascript is redirecting to Home.php once the form is considered valid. You need to submit the form at some point in the process, so you actually get some POST values which you'll use to populate your SESSION variables.
If you want to keep your Javascript validation, maybe have a look at submit() : http://www.w3schools.com/jsref/met_form_submit.asp
You'll have to set a correct action property on your form before submitting it.
So heres a fix that seems to be working
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Login</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-gb" />
<script type="text/javascript">
// Perform Client side validation of Username and Password
function redirect() {
var username = document.getElementById("username");
var password = (document.getElementById("password"));
if (!username.value == " ") {
if (!password.value == " ") {
if (username.value == "aknight") {
if (password.value == "00226291") {
document.forms[0].submit();
exit();
} else
alert("Username or Password Invalid");
}
} else
alert("Password is required");
} else
alert("Username is required");
}
</script>
</head>
<body>
<div id="header">
<h1>Product Order System</h1>
<br/>
<h2>The most expensive crap youll ever find...</h2>
</div>
<div id="content">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['username'] = $_POST['username'];
header('Location : ../Home.php');
}else{
?>
<form action="Home.php" name="login" method="POST">
<fieldset>
<legend>Login Information</legend>
<p>
<label for="Username">Username:</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="Password">Password:</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="button" name="login" value="Login" onclick=" redirect();"/>
<input type="button" name="cancel" value="Cancel" />
</fieldset>
</form>
<?php }
include ('includes/footer.html');
?>
Will post again if the nex series of pages has a similiar issue
Thanks
user doesn't need to submit a number etc. Just think one in their mind.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>guess_game</title>
</head>
<body>
>if guess is too low, user presses 'low button' (same with high button)
<p>think of a number, 1-100.</p>
<form method="post">
<button type="submit" name="low">too low</button>
<button type="submit" name="high">too high</button>
<button type="submit" name="match">it's a match</button>
</form>
<?php
if (isset($_POST['low']))
{
>store $guess to be lowest number range of guess
$low = $guess;
}
else{$low=1;}
if (isset($_POST['high']))
{
$high = $guess;
}
else{$high=100;}
if (isset($_POST['match']))
{
print "thank you for playing";
}
?>
<form method="post">
<input type="hidden" name="hLow" id="hLow" value="<?php $low ?>">
>store $low,High vars to hidden field
<input type="hidden" name="hHigh" id="hHigh" value="<?php $high ?>">
</form>
<?php
>place values stored in hidden back to php var.
$hlow=$_POST['hLow'];
$hhigh=$_POST['hHigh'];
$guess=rand($hlow,$hhigh);
print "is the number $guess";
?>
</body>
</html>
so far this code won't pass any value to hidden field.($guess is always 0)
I want $guess to be random numbers between $low and $high(that are stored in hidden)
You aren't putting $guess into the form. Put this in after you have calculated $guess.
<input type="hidden" name="hGuess" id="hGuess" value="<?php $guess ?>">
Try:
<?php
$guess = 'Think of a number between 1 and 100.';
if(isset($_POST['low']) || isset($_POST['high'])){
$guess = 'The Computer will have to guess again.'
}
elseif(isset($_POST['match'])){
$guess = 'The Computer just read your mind....Not.';
}
?>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<title>GUESSING GAME</title>
<style type='text/css'>
#import 'common.css'; #import 'game.css';
</style>
</head>
<body class='njs'>
<p><?php echo $guess;?></p>
<form method='post' action='<?php echo $_SERVER['PHP_SELF'];?>'>
<input type='submit' name='low' value='too low' />
<input type='submit' name='high' value='too high' />
<input type='submit' name='match' value='it's a match' />
</form>
<script type='text/javascript' src='common.js'></script>
</body>
</html>