Trouble with PHP sessions - php

I have a html page with a few buttons which I would like to take me to another PHP page, this page will handle all logic but I'm having trouble setting sessions.
I could be going about this the wrong way so i'd appreciate if someone pointed me in the right direction.
This is the HTML page code I'm working with:
<form action="reportPage.php">
<center><input type="submit" value="View customers"></center>
<?php
$_SESSION['customer'] = "checkCusTrains";
?>
</form>
<form action="reportPage.php" mehtod="post">
<center><input type="submit" value="View admins"></center>
<?php
$_SESSION['admins'] = "checkCusAdmin";
?>
</form>
I would like each button to go to the reportUser.php page, but with a different session, as I have if/else statements set up in the report page that will display the information corresponding to that session.
How can I achieve this? As it stands, both session variables are being set

I suggest you use query but not session to achieve this:
<form action="reportPage.php?customer=checkCusTrains">
<center><input type="submit" value="View customers"></center>
</form>
<form action="reportPage.php?admins=checkCusAdmin" mehtod="post">
<center><input type="submit" value="View admins"></center>
</form>
And in your reportPage.php:
<?php
if(!empty($_GET['customer']) && $_GET['customer'] == 'checkCusTrains'){
//do something
}
if(!empty($_GET['admins']) && $_GET['admins'] == 'checkCusAdmin'){
//do something else
}
?>

Include a variable in your form that you can check server-side:
<form action="reportPage.php" method="post">
<input type="hidden" name="session" value="checkCusAdmin">
<input type="submit" ...>
</form>
In this way, each form can contribute a special field variable which is hidden to the user, but usable to the server. It can identify what form was submitted or what action to take with the data.
In your case, the "hidden" field might contain the name of the session.

You need to add hidden field in your form that will hold the value of your sessions, if the values of your sessions are already given like 'checkCusTrains' and 'checkCusAdmin', you don't need to use session, just put the value in the hidden input box.
<form action="reportPage.php">
<input type="hidden" name="session" value="checkCusTrains">
<center><input type="submit" value="Check customers"></center>
</form>
<form action="reportPage.php">
<input type="hidden" name="session" value="checkCusAdmin">
<center><input type="submit" value="Check admins"></center>
</form>
And in your reportPage.php
you can get the value by $_POST['session']; , then do whatever you want using if else statement that you have.

I advise you to either use a query for this or to use jQuery cookie and bind the cookie set to the button click action. The code you posted does not do what you think. In that code both cookies are set when the page loads and that is when the PHP sets the cookies.

Related

Getting input value and put it in a variable PHP

I have a problem of getting the value of an input element. Thanks in advance for help! This is the sample code:
<form action="#" method="POST">
<input name="X" value="3" />
<?php
//question: how can I put the value of input in a variable
//$_POST['X']; isn't applicable since I am in the same form.
?>
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input name="X" value="3" />
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['X']) == true){
echo $_POST['X'];
}
?>
First of all submitting a form on its own page is a bad practice because whenever you reload the page the form will submit everytime.
Best approach is that you set the action attribute of form to another file or link.
fileOne.php (where your form is located):
<form action="anotherfile.php" method="POST">
<input name="X" value="3" />
<button type="submit" name="submitForm">Submit</button>
</form>
Then in anotherfile.php you will do this:
$check = $_POST["submitForm"];
If(isset($check))
{
$myInputValue = $_POST["X"];
header("Location: fileOne.php/?val".$myInputValue);
}
Then in fileOne.php you can get the variable and its value which is sent via link in the browser like this:
$getInputValue = $_GET["val"];
Now you can echo out the $getInputValue variable wherever you want.
If the the information is not sensitive this is the best approach you got. But if it is, try saving that value in the session and retrieving in the file where you want. I hope it helps. Typed code via app so it might throw an error. But i think this code will work fine. Good luck!

HTML form does not submit data

I have one page with a simple post form
<form name="click" action="UserOrders.php" method="post">
<input type="hidden" name="amount" value="<?php echo $total ?>">
<input type="image" src="/Templates/images/UpdateButton.png" name="submit">
</form>
and the only thing I want to check is if it submits.. In the other page "UserOrders.php" I just wrote
<?php
if ($_POST['submit']){
echo $_SESSION['ID'];
}
It seems to me irregular that it doesn't work and I would like another set of eyes to check it out.
(if i put the echo $_SESSION['ID'] outside the brackets, it works.)
An input type="image" only defines that image as the submit button and not as an input that can carry over a value to the server. source
An alternative way to see if the form was submitted is to check $_SERVER['REQUEST_METHOD']
if ('POST' === $_SERVER['REQUEST_METHOD']) {
// submitted
}
I agree with the previous answer. You are not actually putting any information to the form and therefore there is nothing for the new form to react to. $_POST['submit"] is looking for data that was sent and not for the method of sending.
There's no actual submit button. You need a
<form action="UsersOrders.php" method="POST">
<input type="whatever you want" name="input">
<input type="submit" name="submit">
</form>
Now if you go into UsersOrders.php and do what you did before:
if($_POST['submit']) {
echo $_POST['input'];
}
You will actually get some input.

Redirect User based on link they've clicked

Is there a way to specify the header location based on which link the user has clicked? If possible, I'd prefer doing this in PHP.
I'd like to submit forms when the user clicks on any of a few buttons (i.e. Next, Back, Save, etc.), and they each need to redirect the user differently once the form is submitted.
Example
HTML - form2.html
<form name="form2" id="form2" method="post" action="form2-exec.php">
<!-- Form Elements -->
<input type="submit" value="BACK" />
<input type="submit" value="SUBMIT" />
</form>
PHP - form2-exec.php
// Connect to database
// Insert & ON DUPLICATE KEY UPDATE Statements
header("location: form3.php");
You do not need the anchor tags. Can you try something like:
<input type="submit" name="whereto" value="BACK" />
<input type="submit" name="whereto" value="SUBMIT" />
PHP - form2-exec.php
// Connect to database
// Insert & ON DUPLICATE KEY UPDATE Statements
if ($_GET['whereto'] == 'BACK') {
header("location: form1.php");
} elseif ($_GET['whereto'] == 'SUBMIT') {
header("location: form3.php");
}
You can find out more about PHP predefined variables at http://php.net/manual/en/reserved.variables.php
you can simply use $_POST['submit'] which will contain the value as a value. Use the name attribute on your submit buttons as well to make it sure.

A second button to start php script, how?

I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.

How to post back to same page without a form using PHP?

Hi I would like to know how to first post to a current page using the following code:
<?php
if(isset($_GET['button']) === true){
echo 'Albums';
}
?>
<form action="" method="post">
<input type="button" name="button" value="Albums">
</form>
I can't seem to get the button when clicked to echo out Albums on current page?? But my main question was how can I achieve what I am trying to do without using a form just a link or button that can return an action using PHP? Is this possible with PHP alone? If not then how can I use whatever scripting is needed to return the PHP function?
If you don't specify the action attribute, the form will post back to your current script.
If you want that $_GET variable to have any of your form fields, set your form's method to GET, or don't set it at all. (It defauls to GET).
You submit the data as post, so you have to check for POST.
<?php
if(isset($_POST['button'])){
echo 'Albums';
}
?>
<form action="" method="post">
<input type="submit" name="button" value="Albums" />
</form>
Additionally I would either use <input type="submit" or <button type="submit" name="button">Albums</button>.

Categories