<?php
session_start();
?>
<html>
<body>
<form action="process.php" method="post">
Name: <input type="text" name="username" value="<?php echo $_SESSION['u']; ?>" />
<input type="submit" value="Submit"/>
</form>
<form action="register.php" method="post">
<input type="submit" value="Register a new user" />
</form>
</body>
</html>
So guys I got this and my question is: I want to use sessions to assign the last registered user as the value of the login form. Everything I got runs ok the thing is if I log in for the first time and I have not started a session i get nothing for the U variable and i get an error message. How can I ... avoid that or something. I tried if statement but in the value section it does not accept {} brackets or something. Any suggestions?
Use isset and assign the session value to a variable instead of using it directly:
if(!isset($_SESSION['u'])){
$u = '';
} else {
$u = $_SESSION['u'];
}
<input type="text" name="username" value="<?php echo $u; ?>" />
An alternative solution - though less cleaner and maintainable - is to utilize PHP's ternary operator:
<input type="text" name="username" value="<?php echo (isset($_SESSION['u']) ? $_SESSION['u'] : ''); ?>" />
Simple explanation of the ternary operator
($statement ? [if $statement == true] : [$statement == false])
using ternary operator..
try this
Name: <input type="text" name="username" value="<?php echo ($_SESSION['u'])?$_SESSION['u']:''; ?>" />
value="<?php echo isset($_SESSION['u'])?$_SESSION['u']:''; ?>"
Related
I just want to ask what are the possible errors in SESSION... Because I've been suffering from my bugs! My codes are right but I don't know why it has happened that when I click the submit button it's supposed to pass the value that i declare but it always declare the last value I declared(it means I can't renew the value! once I declared my value that is permanent which is wrong because every time you click the submit it suppose to give new variable)
home.php
<form method="post" action="1home.php">
<label id="checkinD">
<h3>Day</h3>
<Input id="chiD" name="chiD" type="number" min="<?php echo $_SESSION["day_today"]; ?>" max="<?php echo $_SESSION["day_count"]; ?>" required />
</label>
</form>
$chiD = $_POST['chiD'];
$_SESSION["chiD"] = "$chiD";
1home.php
<form method="post" action="2home.php" onsubmit="return validate()">
<label id="checkinD">
<h3>Day</h3>
<Input id="chiD" name="chiD" type="text" value = " <?php echo $_SESSION["chiD"]; ?>" readonly />
</label>
</form>
BTW There is also a crazy one that occurs on my codes it's working very smoothly without logical errors but every 4 hours my codes will have logical errors without my fault!!! it's like automated bugs appeared every hour.
and sometimes to make it work I need to erase the name of my form then replace it again and typed the word that I erased. What kind of shit is this?
PHP, you need to session_start() before assigning values to your
session variables. The date_today variable holds the present datetime, and date_count variable holds a random number.
Though i can not see your full code but here's the working solution.
home.php
<?php
session_start();
$_SESSION["day_today"] = date("Y-m-d H:i:s");
$_SESSION["day_count"] = rand();
?>
<form method="post" action="1home.php">
<label id="checkinD"><h3>Day</h3></label>
<Input id="chiD" name="chiD" type="number" min="<?php echo $_SESSION["day_today"]; ?>" max="<?php echo $_SESSION["day_count"]; ?>" required />
<input type="submit" value="FORM 2" name="btn_form2" >
</form>
home1.php
<?php
session_start();
if(isset($_POST['chiD'])):
$chiD = $_POST['chiD'];
$_SESSION["chiD"] = $chiD;
?>
<form method="post" action="2home.php" onsubmit="return validate()">
<label id="checkinD"><h3>Day</h3></label>
<input id="chiD" name="chiD" type="text" value = "<?php echo $_SESSION["chiD"]; ?>" readonly />
</form>
<?php echo "Day: ". $_SESSION['day_today']; ?>
<br>
<?php echo "Day Count: ". $_SESSION['day_count']; ?>
<?php else: ?>
<h4> Sorry! Somethinh went wrong. </h4>
<?php endif; ?>
Here's a screenshot of the result
Hope this helps
Here is a code that I've made:
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
When I enter some text in the form and click Validate, the page display sum=1, but after this, when I enter nothing in the form and click Validate, the page STILL displays sum=1.
Why does the variable $sum is not reloaded between the two Validate ? Is there a way to escape it ?
Thanks
This will solve the issue
<?php
$sum=0;
if(isset($_POST['name']) && $_POST['name'] != ''){
$sum+=1;
}
echo "sum = $sum";
?>
This is because isset() checks for the existence of the $_POST variable. In your case, the $_POST variable exists and has an empty string value.
Your code will work if you change isset() to !empty() like so;
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(!empty($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
More about the empty() function here.
Another way would be to check the request that your client has made on your page. So that if it is a simple refresh (not with a form refresh), it is a GET request and so, the variable should not be incremented and if the form has been sent, then you can do whatever you want such as incrementing the data.
So if the client is sending the form with an input text filled, then you can increment the value. In all other cases, the value should remain a zero.
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name']) && !empty($_POST['name']))
{
$sum++; /* strictly equivalent to: $sum += 1; */
}
?>
<samp>sum = <?php echo $sum; ?></samp>
Try this
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" name="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['submit'])){
$sum+=1;
}
echo "sum = $sum";
?>
You can try below:
if(isset($_POST['name']) && strlen($_POST['name'])>0){
$sum+=1;
You have the code appending 1 to variable $sum
but your if statement is based on the name field being passed.
Not if the name field has any data in it.
So... you have made your code add 1 as long as name field is passed,
regardless if it has text input or not.
Also, you should reassign the varible to reset it.
+= should just be =
<form method="post" action="test.php">
//----------------------------- add empty value to input ------------
<input type="text" name="name" value="" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum=1;
}
echo "sum = $sum";
?>
I dont know what im doing wrong. But i get this
Notice: Undefined variable: num1 in D:\Programs\XAMPP\htdocs\homework\addsub.php on line 15`
<?php
if(isset($_POST['sub']))
{
$num1=$_POST['t1'];
$num2=$_POST['t2'];
if ($_POST['sub']=="+") {
$res= $num1 + $num2;
}
elseif($_POST['sub']=="-"){
$res = $num1-$num2;
}
}
?>
<form action="addsub.php" method="POST">
<input type="text" name="t1" value="<?php echo $num1;?>"><br>
<input type="text" name="t2" value="<?php echo $num2;?>"><br>
<input type="text" name="res" value="<?php echo $res;?>"><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
</form>
When I use $num1 or $num2 in textbox values, it shows error. One of my friends used this same code on his laptop but he is using much older version of Xampp. It works fine but later versions of Xampp gives this error. I am using Xampp v3.2.1.
Or initialize the variable if it doesn't exist, like so:
<?php
if (!isset($num1)) {
$num1 = '';
}
Then your HTML could remain unchanged.
The reason I recommend this approach is that it creates clean code - the HTML will always display the value of $num1 and if you choose to initialize it to a different value later, it should be easier to find in the PHP.
use isset to check variable exist or not.
example
<input type="text" name="t1" value="<?php echo isset($num1)?$num1:""; ?>"><br>
You set the $num1 variable only when,
if(isset($_POST['sub']))
So, if the $_POST['sub'] is not there the variable is undefined!
Here is the code, which may help you:
<?php
var $res="";
var $num1="";
var $num2="";
if(isset($_POST['sub']))
{
$num1=$_POST['t1'];
$num2=$_POST['t2'];
if ($_POST['sub']=="+") {
$res= $num1 + $num2;
}
elseif($_POST['sub']=="-"){
$res = $num1-$num2;
}
}
?>
<form action="addsub.php" method="POST">
<input type="text" name="t1" value="<?php echo $num1;?>"><br>
<input type="text" name="t2" value="<?php echo $num2;?>"><br>
<input type="text" name="res" value="<?php echo $res;?>"><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
</form>
On my first page: page1.php I have an input where you type your desired name which I then want to be carried over to more than one page, so far I can get it to page2.php but on page3.php the code fails here is my code
page1.php:
<form action="page2.php" method="post">
Name: <input type="text" name="username" />
<input type="submit" name="submit" value="Submit" />
\
page2.php: (after 5 seconds the page redirects to page3.php)
<?php
echo $_SESSION['username'] = $_POST['username'];
?>
<form action="page3.php" method="post">
<input type="hidden" name="username" />
page3.php:
<?php
echo $_SESSION['username'] = $_POST['username'];
?>
These lines work on page2.php but not here which is what I can't seem to fix
Instead of giving you a fish, I'll teach you to fish:
echo $_SESSION['username'] = $_POST['username'];
This statement is echoing the value ASSIGNED to $_SESSION['username']
= Assignment
== Comparison
=== Comparison (Identical)
On page3.php is not working because you pass no value. So:
Instead of:
<input type="hidden" name="username" />
Go with:
<input type="hidden" name="username" value="".$_SESSION['username']."">
Or:
<input type="hidden" name="username" value="".$_POST['username']."">
Now it passes the value and you should get the value on page3.php. One warning is that users can edit your value with DEV tools so I suggest to pass values differently.
I am using php_self to submit a form. Once the data has been posted, I want to pass a calculated value to another form field on the same page, original form.
The $title_insurance field stays blank. Any ideas on why? Thanks!
<?php
if(isset($_POST['submit']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
?>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>';
</script>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="submit" type="submit" class="bordered" id="submit" value="Calculate" />
</form>
The submit button is called button, also if you are outputting a javascript to amend the value it need to be run after the DOM has created the element title_insurance.
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>';
</script>
A better way in this case would be to forget about the javascript as it is unnecessary and do this
// I am assuming you have initialized $title_insurance
// somewhere above here to its default value!!!!
$title_insurance = isset($_POST['button']) ? ($_POST['sale_price'] * 0.00575) + 200 : $title_insurance;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
You have an extra space in your getElementById parameter:
// VV
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>';
What you want to do is best done by AJAX. The <form> construction is outdated and not useful unless you are transferring the user to another page and sending some data along with it - or, if you are finished getting user data and just want to process what was entered and display a completion message.
If you wish to continue processing on the same page, then AJAX is the way to go. And the best way to use AJAX is to have a separate processor file (PHP) that receives the data, processes it, and sends it back.
To convert a <form> construct to AJAX, you really just need to remove the <form></form> tags and convert the submit button from type="submit" to type="button" id="mybutton", and use the IDs on the button and on the other elements to grab the data they contain and feed them to the AJAX code block. The examples in the link at bottom shows what you need to know - they are simple, helpful examples.
To conserve resources, you can use the same PHP processor page for multiple AJAX requests -- just send a variable (eg. 'request=save_to_db&first_name=bob&last_name=jones', ) and test for what "request" is received, that will determine what your PHP processor file does and echoes back.
This post, and the examples it contains, will help.
try this first
In you coding you missed this $_POST['button']
and
<?php
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
?>
<script type="text/javascript">
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>';
</script>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
and also refer this FIDDLE it will more helpful to you..