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.
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
There's probably something pretty easy here that I'm missing but I can't seem to figure it out.
I'm trying to pass a PHP variable "$session" from one PHP page to another PHP page using GET. The GET shows in the new URL but the value is not passed to the page so that I can retrieve it. Below is the relevant code.
On the main Index.php page.
<div class="mapgroup">
<table>
<tr>
<td></td>
<td id="refreshKML" ></td>
<?php echo "<td><a href='3D-Earth.php?session=$session' title='Full Size Google Earth' target='_blank' >Click here</a> for full size Google Earth</td>"; ?>
</tr>
</table>
<div id="map3d"></div>
</div>
The "$session" variable is passed to 3D-Earth.php using GET and the correct value shows in the URL. However, when I try to retrieve the variable with:
<?php
$session = $_GET["session"];
?>
and then try to create a javascript variable:
<script>
var session = <?php echo $session; ?>;
</script>
that I will use to concatenate the below string:
var href = 'http://localhost/satellites/' + session + '/master.kml';
nothing is being passed.
I appreciate any help anyone can provide. Thanks in advance.
Additionally, I've inserted the below code at the bottom of my 3D-Earth.php page to verify that the GET is being passed.
<div>
<table><p style='color:white;' >Hello " <?php echo $session; ?> " world!</p></table>
</div>
The result is an empty string that shows: Hello " " World! Anyone know what I might be doing wrong?
Try like this:
var session = '<?php echo $session; ?>';
I suggest stop using names like 'session' that might conflict with php reserved words. Maybe that's not the issue here, but avoids many other problems and confusions.
Also in javascript put the echo in quotes:
var session = '<?php echo $session; ?>';
You should put a single quotes around your variable like this
var session = '<?php echo $session; ?>';
As above answers say, you have to have single quotes around a php statement when you want to use it in javascript. So var session = 'php statement'; is the right way to go.
have you tried to put this code
<script>
var session = <?php echo $session; ?>;
</script>
under this code ??
<?php
$session = $_GET["session"];
?>
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.
My problem is I have a php variable $ba with a value of 147 retrieved from a DB the below snipets of my code does not work, no image is shown. If I add $ba=147; before the fill_div call it works perfectly. I am stumped as to why it dos'nt work when $ba populated from the DB.
In both cases a check of the source code for the page shows the call being filled correctly fill_div("147");
<script type='text/javascript'>
function fill_div(ba)
{
document.getElementById("ba").innerHTML="<img src='admin/images/image.gif'/>";
}
</script>
<script>
fill_div("<? echo stripslashes($ba); ?>");
</script
<div id="<? echo $ba ?>" style="border:1px solid; width:120px; height:40px"></div>
You're passing the string literal"ba" to getElementById when you need to pass the variable ba
document.getElementById(ba).innerHTML="<img src='admin/images/image.gif'/>";
Also the stripslashes might not be a good idea if $ba has quotes in it, if $ba has a " in it will cause an error in your JavaScript if it is not escaped.
try ..
document.getElementById(ba).innerHTML
document.getElementById("ba")
should be
document.getElementById(ba)
Try changing your document.getElementById to:
document.getElementById(ba)
If it has quotes (as in your example code) "ba" is treated as a string, not a variable.
I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.
I tried this in my php site <a href=?command=value>click here</a>
And this to get the value :
if(isset($_POST['command'])){
$command=strip_tags($_GET['command']);
Does not seem to work.. Ps. The url have one this value also: ?lang=en_US
Use $_GET instead of $_POST. And also put double quotes around the value of href attribute.
Try this....this will work...
'click here'
this is to get the value from site:
<?php
if(isset($_REQUEST['command'])){
$command=($_GET['command']);
echo $command;
}
?>
the output: value
Note:Both the codes(php and html) are written in same page(company.php)
we can also write the php code in seperate page. Difference is,the 'isset' function is not needed there.And URL should change.
code will be the following.
'click here'
next_php_page.php code:
<?php
if($_REQUEST['command']){
$command=($_GET['command']);
echo $command;
}
?>