This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
How to read values from header url in javascript?
For example if I have something like:
www.something.com/details.html?test=something
I need to be able to read this data when I redirect to page details.html.
What would be a good approach for this? Maybe adding some php code to get data?
Are you talking about getting the value from a get variable and using it in javascript? If so I've used something similar to this before:
<script type="text/javascript">
function myfunction(){
var value = "<?php echo $_GET['test']; ?>"
}
</script>
And it's worked beautifully for me.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I was trying to unset a php session variable inside a jQuery if block. Whenever someone unchecks a checkbox it should unset a specific session variable. Here is my code below
PHP
session_start();
$_SESSION['myName'] = "John";
HTML
<form action="" method="post">
<input type="checkbox" id="myCheckbox"> Check to set session
</form>
jQuery :
$('form').change(function(){
if($('#myCheckbox').is(':checked')){
// Some statements here
}else{
<?php unset($_SESSION['myName']); ?>
}
});
But the code is not working here. It is not unsetting the php session variable. Please provide me a solution. Thanks.
Note : I can do it by using AJAX by loading the php code from another page. But I want it to be done by using jQuery only.
Or is there any solution in jQuery that I can unset session variable without using PHP code?
It will not work this way.
All PHP codes are executed before Javascript function .
To achieve this :
You have to do AJAX or form submission.
You just cant do it by only jQuery.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I want to call a PHP function when someone clicks an html link. I don't know how to do it. Please help.
Didn't get a code?
<?php
function sendCode(){
//code
}
?>
Do it like this:
Didn't get a code?
<?php
if(isset($_GET['sendcode'])){
sendCode();
}
function sendCode(){
//code
}
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I run PHP code when a user clicks on a link?
I have an image .
<image name="" src="">
I have a php code that needs to be run only after the image has been clicked.
<?php
$var = somthing;
if(condition)
{
sql stmts;
}
?>
like that.
Both are in the same php page. PLease help me to sort out this problem.
Thanks..
You can send a request with javascript. With jQuery that would look like this:
$.get("yourfile.php?function=imageClick", function(data){});
In your php somewhere at the top add:
if($_GET['function'] == 'imageClick'){
// do your php stuff
}
You can't run PHP code on the client side.
You can do that through an AJAX call: http://www.w3schools.com/ajax/default.asp
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variables from php to javascript
I am using this awesome jQuery Stick Notification Plugin which creates notifications upon clicking various buttons.
I used the following code to display the notification box...
$(".notify").click(function() {
$.sticky('The page has loaded!');
});
Can i make it display some PHP variables in place of static text message?
Hope i've made it clear enough.
Without making asynchronous HTTP calls, you will have to insert the PHP variable at server-side:
$(".notify").click(function() {
$.sticky('<?php echo htmlentities($message); ?>');
});
Wrap it with htmlentities() just in case $message contains some chars that make the JavaScript string invalid.
yes you can like
$(".notify").click(function() {
$.sticky('<?php echo "The page has been loaded"; ?>');
});
This question already has answers here:
How can I convert language of a div?
(4 answers)
Closed 7 years ago.
"<div id="diva">this is div a</div>"
"<div id="divb"> this is div b</div>"
now how can I change the text of div dynamically by button click. here also mention that all text come from a database. when I click the button then it detects the div id and replace the text with desire text.
All code will be in PHP.
This will work once the page is being loaded:
document.getElementById('diva').innerHTML =
<?php echo functionThatReturnsSomeValue(); ?>;
Dynamically, you would need to do an AJAX call on the PHP file which outputs a value. You'd be better off if you used a JS framework such as jQuery, rather than implementing the AJAX call yourself.
PHP cannot modify the content of the page after it has been served to the browser. However this would be trivial with a JavaScript Library such as jQuery.