I have the following form on "test.php".
<?php
session_start();
if(isset($_POST['ph']))
if(isset($_POST['submit']))
$_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
The "order.php" looks like this:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
echo ($_SESSION['ph']);
?>
The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.
I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.
Change the new value to SESSION ON your order.php page like below:-
<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
$_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
echo ($_SESSION['ph']);
}
?>
Also change test.php code like this:-
<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
Related
I know there are other posts about this, but I still can't seem to see where the code is incorrect.
I'm trying to pass a variable from one page to another via session: Below are the two pages; excuse some of the variable names as I was just plotting them in quickly.
main.php
<?php
session_start();
session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
result.php
<?php
session_start();
echo $_SESSION['itemId'];
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Previously I didn't have the unset in there. However, it hasn't made a difference.
The value is getting stored in the session, its just not passing it through to the other page.
Main page should looks like this:
<?php
session_start();
//remove session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
remove session_unset(); line after start_session()
and why r u destroying session in result page.
if you want to destroy then
store session value in variable
$id = $_SESSION['itemId'];
echo $id;
this code will help u
I have a script of 128 barcode, and it is using random number to generate a barcode, how can I make this by using textbox instead of rand in php?
This my code
<!DOCTYPE html>
<html>
<head>
<title>asd</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$bcode = $_GET['id'];
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
include 'Code128.php';
$code = isset($_GET['code']) ? $_GET['code'] :$bcode;
header("Content-type: image/svg+xml");
echo draw($code);
}
?>
<form method="POST">
<input type="text" name="id">
<input type="submit" name="submit">
</form>
</body>
</html>
A textbox can't randomly generate a number...
I've solve the problem,
<form method="Get" action="barcode.php">
<input type="text" name="barcode">
<input type="submit">
</form>
barcode.php
I have a search functionality that it works as shown below, but I would like to be able to pass a variable from a php file to another file without having an input like I have in my search function.
This is search functionality that works:
I have the following index.html file
<html>
<body>
<p>Search</p>
<form name"form1" method="post" action="searchresults.php">
<input name="search" type="text" size="40" maxlength="50">
<input type="submit" name="submit" value="Search">
</form>
</body>
</html>
and I have my searchresults.php file
<?php
$nameofcity = $_POST['search'];
?>
Something like this is what I would like to have but not sure how to do it.
index.php file
<html>
<body>
<p>Search</p>
<?php
$variabletopass = "London";
echo '<form name"form1" method="post" action="searchresults.php">';
echo '<input type="submit" name="submit" value="Search">';
</form>
?>
</body>
</html>
my searchresults.php file where I want the $nameofcity to be equal to the value of $variabletopass, in the example = London.
<?php
$nameofcity = $_POST['search'];
?>
You could try session. For example,
// index.php
$_SESSION['variabletopass'] = "London";
//searchresults.php
echo $_SESSION['variabletopass']; // Prints London
Hope this helps.
I have the following code in two files separately
file one.php
<HTML>
<BODY>
<FORM ACTION="two.php" METHOD="POST">
Age: <INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="OK">
</FORM>
</BODY>
</HTML>
file dos.php
<HTML>
<BODY>
<?PHP
print ("The age is: $age");
?>
</BODY>
</HTML>
the age variable is not recognized, someone knows fix?
It's not recognized because you don't create it. Variables don't magically appear in PHP1. You need to get that value from the $_POST superglobal:
<HTML>
<BODY>
<?PHP
$age = $_POST['age'];
print ("The age is: $age");
?>
</BODY>
</HTML>
1 Anymore. They used to when register_globals existed. But that has been obsolete since long before you started coding.
Your trying to access the value of age from a page( dos.php) but you posting it to (two.php) and your missing $_POST['age'].
one.php
<HTML>
<BODY>
<FORM ACTION="two.php" METHOD="POST">
Age: <INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="OK">
</FORM>
</BODY>
</HTML>
two.php
<HTML>
<BODY>
<?PHP
$age = $_POST['age'];
print ("The age is: $age");
?>
</BODY>
</HTML>
I am currently learning the most basic PHP ever. I have 5 files.
index.php:
<html>
<head>
<title>Budget Calcule</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Put in your: - </h2>
<form action="functions.php" method="post">
<h3>Income</h3>
<label>Salary: <input name="salary" type="text" /></label><br />
<h3>Outgoings</h3>
<label>Living: <input name="living" type="text" /></label><br />
<label>Insurance: <input name="insurance" type="text" /></label><br />
<label>Communication: <input name="communication" type="text" /></label><br />
<label>Loan: <input name="loan" type="text" /></label><br />
<label>Food & Drink: <input name="foodAndDrink" type="text" /></label><br />
<label>Entertaintment / Shopping: <input name="entertainmentOrShopping" type="text" /></label><br />
<label>Transport: <input name="transport" type="text" /></label><br />
<label>Other: <input name="other" type="text" /></label><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
this is my functions.php:
<?php
include('variables.php');
if(!($_POST['Submit'])){
if(isset($_POST['salary'])){
header('Location: output.php');
return $_POST['lon'];
}else{
echo "All fields are required";
}
}
?>
this is my variables.php:
<?php
$salary= $_POST['salary'];
$living= $_POST['living'];
$insurance= $_POST['insurance'];
$communication = $_POST['communication'];
$loan = $_POST['loan'];
$food = $_POST['food'];
$entertaintmentOrShopping = $_POST['entertaintmentOrShopping'];
$transport = $_POST['transport'];
$other= $_POST['other'];
?>
this is my output.php file:
<?php
include('outputFunction.php');
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
and last but not least, this is my outputFunction.php file:
<?php
include('variables.php');
function myText(){
echo "Your salary per month is: " . $_POST['salary'];
}
?>
Now you're thinking "why have he split up his code in different files?" Well first of all, I split the variables from functions.php because I wanted outputFunctions.php to get the variables from variables.php so i could echo my `$_POST['salary']; . The function myText(); outputs the text just fine, but it doesnt output the $_POST['salary'];.
I do not know why it doesnt work, I just wonder if you could be my extra eyes and see if I've done some mistake.
PS! Don't down vote my question just because you think it's stupid. I am having problem with this issue and been working on it for hours without advancing anywhere.
A few things:
You don't need to include a variables.php file. The variables you're accessing are global and you're just creating duplicates that aren't being used. They also go away after the page changes since you're re-declaring them each page load.
You are also trying to call a variable that doesn't exist when you reference $_POST['lon'] instead of 'loan'.
And finally to actually answer your question:
Your myText() function is referencing a variable that is not there anymore.
You need to merge functions.php and outputFunction.php and output.php into one file so the variables aren't lost and all the processing is done without opening a new file each time. I can see your original concept for separated files but an output file is going to be the file to process the input data from the form.
Now in your newly merged output.php, you should have something resembling this:
<html>
<head>
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
echo "Your salary per month is: " . $_POST['salary'];
}
} else {
echo "All fields required.";
}
?>
</body>
</html>
This means only two files - your form page and this page.
A few more tips:
If you want to check if the form was submitted, it has look something like this:
if(isset($_POST['Submit'])){ ... }
Also, you should add a name="" attribute to your submit-Button:
<input type="submit" name="Submit" value="Submit" />
And what is the variables.php for? You don't use any of those variables.
When you redirect the user via header() the data that is stored in the $_POST array gets lost.
You could directly redirect to ouput.php
<form action="output.php" method="post">
And do something like this:
<?php
include('outputFunction.php');
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
<?php
} else {
echo "All field required";
}
}
?>
By the way you can always check what your $_POST contains with print_r($_POST);
This can be very useful for debugging.