In a page containing a form with select fields, I need to remember the previuos choice (if any) of, for example, the selected state. I want to do it via PHP embedded in JS, where I'm wrong?
<script>
<?php
if(isset( $_GET["state"] )){
$selected_state = $_GET["state"];
?>
$("#state option[<?php echo $selected_state ?>]").attr("selected", "selected");
<?php } ?>
</script>
Thank you in advance!
Replace:
$("#state option[<?php echo $selected_state ?>]").attr("selected", "selected");
With:
$("#state option[value='<?php echo $selected_state ?>']").attr("selected", "selected");
The problem is with the jQuery selector #state option[SOMETHING] which checks if the element option has an attribute called SOMETHING, where you want to see if the value of an option is equal to SOMETHING so we use #state option[value='SOMETHING'].
You can find the jQuery selectors docs here: http://api.jquery.com/category/selectors/
You can use sessions in PHP to remember certain data.
For example:
session_start();
$foo = 'John';
$_SESSION['user'] = $foo;
// Refresh the browser.
echo $_SESSION['user'];
// Outout: 'John'.
Check out http://php.net/manual/en/ref.session.php if you want to learn more.
Be careful of what you store in sessions though, don't store critical data in them.
Related
I want to enable or disable a div according to the session if it starts with an user or a guest. I want to do something like this:
First, i will evaluate if it is user or not by doing this:
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
}
?>
then in jquery, i would like to say:
$('.box').click(function(){ // labBox appears when box is clicked
if(<?php $guest?>)
$("#LabBox").hide();
else
$("#LabBox").show();
});
Question: how can i use my php boolean var $guest to disable or hide some elements of my website?
Do i have to do two distinct php files? one for users and other for guest (e.g, home.php and home_guest.php)?
you could do the alternative such as
<script>
var guest = '<?php echo $guest; ?>';
$('.box').click(function(){ // labBox appears when box is clicked
if(guest === "true") {
$("#LabBox").hide();
} else {
$("#LabBox").show();
}
});
</script>
This would simply allow you to pass the PHP value to a Javascript variable, in order for you to use it within the onClick.
Remember: everything that reaches the client can be manipulated. Therefore, if you send an hidden element (say, an hidden <div>) any tech-savvy user can, and will, easily make them visible.
You MUST perform the checks about the login/guest status in your PHP script, and don't rely on jQuery to assemble the page at client side (hey, after all, the user may have disabled javascript altogether!)
You don't need two pages (eg: home.php and home_guest.php) to render different content based on the user level. Just use appropriately session/cookies and different echos.
Use a hidden input, populated by PHP, which jQuery can grab:
<?php
echo "<input type=hidden id=guestcheck value=$guest/>"
?>
if ("#guestcheck").val()) {
}
I personally like this method because it allows me to check the source when debugging to find out where any errors may be (for instance you can plainly see in the source when viewing the page whether or not GUEST is true)
It depends on contents of those files. If the only difference is visibility of the block, it's more reasonable to do the check inline.
<?php if (isset($_SESSION['idUser'])) { ?>
$('.box').click(function() { $("#LabBox").show(); }
<?php } ?>
Personally I would do it in the HTML rather than the JS file...
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$loggedin=true;
} else {
$loggedin=false;
}
?>
Then later on..
<?php if($loggedin===true){?>
<div>User is logged in</div>
<?php }else{?>
<div>Guest is viewing page</div>
<?php }?>
This means that the div for the user is not shown to the guest, whereas your currently solution only hides it from view (user could just use firebug/viewsource!
Why don't you just show/hide your div in the php depended on if they are a guest or not...
So...
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
}
if($guest===true){
echo "<div></div>";
}
else{
//dont echo div
}
?>
PHP / server-side:
<?php
if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
$guest=true;
} else {
$guest=false;
// add #LabBox element from here to avoid junk/hidden elements for guests
}
?>
JQuery / client-side:
$('.box').click(function(){ // labBox appears when box is clicked
if (!<?php echo $guest?> && $('#LabBox').length > 0) {
$('#LabBox').show();
}
});
Then it is critical that any action requested by the user pass the "guest or not?" test before being granted from the server-side.
I know I this is simple, but i just can;t find the resource that tells me how to do it.
SO my code is as follows:
session_start();
$wquery=
"select week_num,week_name
from stats_week
where season=$this_season
order by week_num";
$wresult=mysql_query($wquery);
print'<form action="changeplayer_2.php" method="post">';
print"<select name='Week_select'> <br>";
while ($wrow=mysql_fetch_array($wresult))
{
print '<option value="'.$wrow['week_num'].'">'.'week '.$wrow['week_num'].' '.$wrow['week_name'].'</option><br>\n';
}
print "</select><br><br>";#
print'<button type="submit" >Next</button>';
print"</form>";
So I am making a selection:
I want that selection to end up in: $_SESSION['week']
$_SESSION['week']=$_POST['Week_select'];
you can do this either sending data by form on submit or if you want to do without refreshing page you can do this by ajax
and than set like this
$_SESSION['week']=isset($_POST['Week_select'])?$_POST['Week_select']:someDefault;
and to do this by ajax check this answer
Inside file "changeplayer_2.php" you'll want to load the POSTed value to the session:
session_start();
$_SESSION['week'] = $_POST['Week_select'];
Whenever you want to get the value of the session at key 'week', simply do:
$weekValue = isset($_SESSION['week']) ? $_SESSION['week'] : false;
But if you do use the session variable on any script, make sure you start the session using session_start().
To destroy the session variable, you can do:
session_start();
unset($_SESSION['week']);
hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.
I got a listbox in my php page that generate from another listbox, the code is as follows
<?php $myArray1 = $_POST['countryRF'];?>
<select name='countryRF[]' id='countryRF' size='10' multiple style='width:180px' class='selfont'>
<?php
foreach($myArray1 as $value){ // Loop through each element
print "<option value=\"".$value."\">".$value."</option>";
}
?>
</option></select>
While refreshing the form listbox get empty, how can I keep added values even after form reload ?
Is it possible to keep the mysql result table stable even after form reloading by session ? If so please give me a help ?
i see you get the 'countryRF' from post.
you can store it in a cookie/session, of store it on the server.
use
if (isset($_POST['countryRF'])) {
$_COOKIE['countryRF'] = $_POST['countryRF'];
}
$myArray = $_COOKIE['countryRF'];
same goes with session
save them into Sessions
<?php session_start();?>
send the color
<br><br>
<?php
#$_SESSION['color'] = $_GET['color'];
echo $_SESSION['color'];
?>
<br>
<br>
check the session variable
hi,
i need help for the above code.
i want to pass a variable to session.
with above code im doing this but the session variable dissappears when i refresh the page or when i click the bottom link. i want the echo $_SESSION['color']; sticky
what should i do?
with regards
You are assigning value of $_GET['color'] no matter if there is such GET variable or not. Because of this, when there is no $_GET['color'] you are loosing the session variable.
it have to be:
if (isset($_GET['color'])) {
$_SESSION['color'] = $_GET['color'];
}
echo isset($_SESSION['color']) ? $_SESSION['color'] : '';
When $_GET['color'] is empty because color is not in the query string of the URL, you still assign that empty value to $_SESSION['color'].
Don't do that and the value you set won't be overwritten. Nothing was disappearing on its own.