I am creating a set of 3 pages, each with a dropdown menu. The values from the previous dropdown menus will populate the next dropdown menu. I understand that I must use session variables to hold the value of each dropdown menu into later pages. As a simple test, I echo the variables to see if they have been carried over -- and that's where the problem is.
I have three different files: choose_cc.php, choose_uni.php, and choose_major.php
The value from the dropdowm menu in choose_cc.php does get echoed in choose_uni.php -- however the value from choose_cc.php does NOT get carried over into choose_major.php -- despite me storing it into a session variable.
the flow of pages is like this;
choose_cc.php --> choose_uni.php --> choose_major.php
Each php file has it's own form. The problem lies in when I try to call the value from choose_cc.php into choose_major.php, i have issues.
The name of the form on choose_cc.php is choose_cc.
The name of the form on choose_uni.php is choose_uni.
So for example, in choose_uni.php, I retrieve the value from the dropdown menu on the previous page (choose_cc.php) like this:
$_SESSION['choose_cc'] = trim($_GET['choose_cc']); //fetches cc from previous page
$user_cc = $_SESSION['choose_cc'];
echo $user_cc;
and when I echo it as I did above, it works! Okay perfect!
But when I head onto choose_major.php, I try retrieving the value again from the form, but to no avail like this;
echo $_SESSION['choose_uni']; //this works
echo $_SESSION['choose_cc']; //this doesn't work
I have made sure to store to do session_start() on the beginning of each page as well.
Please help me out! this is driving me insane!
Create a new "see.php" with this:
session_start();
print_r($_SESSION);
and execute it after each choose_cc.php, choose_uni.php and choose_major.php to take a look the session you have after you run your programs.
Simple as this:
a) add session_start(); at the beginning of ALL the involved pages. Some weird stuff happens sometimes if you put a space before it, or even a new line. So be sure it is the very first thing in your script.
<?php
session_start();
?>
b) if needed, check the variable to save into session for not empty(); That way you can be sure the session variable contains something, unless of course you want explicitly to be empty.
c) then load the session variable. You can temporary check it with a var_dump();
<?php
session_start();
if (!empty(trim($_GET['choose_cc'])))
{
$_SESSION['choose_cc'] = $_GET['choose_cc'];
}
var_dump($_SESSION['choose_cc']);
?>
Related
I hope this isn't stupidly simple. I am completely new to web dev.
I have list items that I styled as buttons.
I want to be able to link to a new page as well as store some information when the list items are clicked. I want to be able to store which list item was clicked in a Session variable.
How do I accomplish this/ is there a better way to accomplish the same thing?
Sessions Step By Step
1- Defining session before everything, Ideally do it on very top of the document so no output is being exposed etc like this
<?php
session_start();
?>
2 - Set your session inside a page and then you have access in that page. For example this is page 1.php
<?php
//This is page 1 and then we will use session that defined from this page:
session_start();
$_SESSION['danish']='danish';
?>
3- Using and Getting session in 2.php
<?php
//In this page I am going to use session:
session_start();
if($_SESSION['name']){
echo 'Your name variable Is Here! :) ';
}
?>
In short its like you assign session variable in a page and then using same declarative syntax instead of assigning you call the variable and PHP do the magic to check if that session variable was created and hold the value so, in short i can write my code like this
First Page
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Second page
<?php
session_start();
echo $_SESSION['myvar'];
?>
Is it possible to remove a value whilst the page is loading?
I'm using OSClass, and on one of the pages it's by default adding a value for region (cambridshire):
I need to clear this value since it's causing problems, everytime I type something else in, by default, it registers it as Cambridgeshire...
If I look at code for it:
It's being generated by a function (can see my JQuery attempt to clear it which hasn't worked).
Then if I search the function is splits up in many different parts, so I don't know where to go from there.
Basically, is there a way to remove the value when page loads and save the new value when the user submits?
JSFiddle - Note it won't display anything due to the way code is generated
Have you tried removing <?php ItemForm::region_text(); ?> on it's own? Or passing an empty string <?php ItemForm::region_text(''); ?>
I'm not familiar with OSClass though I'm afraid.
Paste this code below the jQuery initialization:
$(document).ready(function(){
$("#region").val('');
})
I need some help on passing a url php variable onto the next page. I've tried searching throughout the site for help and I've spent a lot of time trying to figure this out with no luck. Basically I need to be able to change the paypal link button id on page 2 with the url variable from page 1.
The variable is initially passed along with the URL: http://www.example.com?p=paypalbuttonid
I would like to store and pass that "p" variable on to the next page. I don't want to pass the variable onto page 2 with a link. I would prefer to store the variable and recall it on page 2.
Page 1 code (above html):
<?php
session_start();
$_SESSION['paypal'] = $_GET['p'];
?>
Page 2 code (above html):
<?php
session_start();
$p = $_SESSION['paypal'];
?>
I'm calling the variable in a link on page 2 (body):
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=<?php echo $p ;?>" target="_blank" class="btn">
I'm not sure what I'm dong wrong but I'm a complete newbie to PHP so please help! The variable shows up blank in the URL on page 2. Thank you! - Chad
First, you should make sure you dont have any output before session_start(), it is safe to put session_start () at the top of the page , especially if you use php code in .html, since you may have output without awareness, and session wont work if you have anything output to the browser before session_start()
according to php.net:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
and you should check if you cookie is enabled.
Secondly, var_dump ($_SESSION); to see if you can get anything
I have a drop down menu, after selecting a value and clicking submit you're sent to another page with a table displaying query results retrieved using the value. The table uses pagination to split the results after 5 records with next and previous links, but when you click on the next page the value is lost and with it the results.
I tried using sessions,
In page.php
session_start();
$id = $_GET['id'];
$_SESSION["selectedID"] = $id;
include "table.php"
And in table.php
$selectedUserID = $_SESSION["selectedID"];
But it's not working, any idea what I'm doing wrong?
You also have to put a session_start(); in your table.php
first, I imagine you are using post method for your search/filter, that's not recommended (my opinion), use always get method, next, maybe this pagination php class can help you.
Regards
this is the continuation of my previous question. Since I just logged in that day without an Open Id, I don't know how to login to that account and edit further. So I created a new Open Id enabled account and posted this as a new question.
Now I have the code like this. Inside the onclick event, the value is stored in the $selectedId correctly. But When I try to pass that value in the url, I do not get the correct value. The last value in the for loop is passed.
<script type="text/javascript">
$(document).ready(function(){
<?php foreach ($Forms as $r): ?>
$("<li><a id='<?=$r['Form']['id'];?>' data-attr='Formentries' href='#'><?=$r['Form']['name']?></a></li>").appendTo("#headers").click(function () {
<?php $selectedFormId=$r['Form']['id'];?>
alert("selId: "+<?php echo $selectedFormId;?>); //here the selected id is alerted
});
alert("outside the loop"+<?php echo $selectedFormId;?>); //here the last value in the loop is alerted
});
Once out of the click function, the value of $selectedFormId changes to the last value in the array. Can someone help me with this?
Actually what I am trying to achieve is, I list a set of Forms as links, and when I select the links I want its id to be saved in a php variable. I want it particularly be saved in a php variable coz after I select a Form I have an option to export the entries in the form through another link
<a href="localhost/FormBuilder/reports/export/<?php echo $selectedFormId;?>" class="thickbox button" title= "Export" >Export</a> .
So I want the id in there,so that I could pass it to the export function in the controller.
I also get the selected id in a javascript variable as
formid=$(this).attr("id");
but I do not know how to pass this value to the export function in the controller.
I don't know if i've understood your question very well but the $selectedFormId value is set inside the loop, so everytime the loop is executed the variable is set and when the loop finishes $selectedFormId gets the last processed value. I think you should set it outside the loop.
as you are declaring it inside the block so it is not available outside;
either you declare it before the $(document).ready block or finish its usage it inside this block itself.
As far as I can see you are setting $selectedFormId inside each loop in the iteration, so the assignment code will only be set when the click event is fired.
Therefore, logically, when you exit the loop, $selectedFormId will be the last item in the array. Are you meaning to put a condition around the assignment? Don't forget, the server side code will execute regardless! It won't care about client side conditions or closures!
if (something){
<?php $selectedFormId=$r['Form']['id'];?>
}
And, again echoing the above comments, you should really be trying to achieve code separation. The above really is tag soup!
You are mixing client and server-side code -- the code you wrote seems like you're expecting a JavaScript function on the client-side to magically set a PHP variable on the server-side, when in reality that type of operation is not possible.
What you should maybe do is have your click event set a variable on the client-side. Then set an onclick on your export link to construct a URL and redirect to it based on the locally stored variable value.
That's probably not the best solution, but it would be one option.