How to pass a variable from jQuery to PHP? [duplicate] - php

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
I have a jQuery function, which, on click, itercepts a clicked link:
$("a").click(function(e) {
e.preventDefault();
var link = $(this).prop('href');
alert(link);
});
and it works properly, but how to pass that link to a PHP variable to 'echo' it later?
I unsuccessfully tried:
$("a").click(function(event) {
event.preventDefault();
var link = $(this).prop('href');
<?php $link = .'link'. ?>
});
I know it may be a silly question, I looked all over the web but I didn't find any proper answer.

The only way to do this is to have a script wich stores values passed to it in a session or to a DB and either output the session data later or read from the db
$("a").click(function(event) {
event.preventDefault();
var link = $(this).prop('href');
$.post('saveLink.php', {href : link});
});
the saveLink.php would be something like this
<?php
// Clean your POST variable, always clean any POST variables
// see here for a good discussion on that
// http://stackoverflow.com/questions/4223980/the-ultimate-clean-secure-function
$link = htmlspecialchars($_POST['href']);
$_SESSION['links'] = $link;
?>
then later on you can retrieve all you session data
Note
Session data is not permanent and not shareable between users so if you need to do that you would be better off with a db query also if you need to access a particular link then you will need to send some sort of id with it.

PHP is preprocessor running on a server side. jQuery is running on client side (in user's browser) and it can't comunicate with PHP like this.
Although you can send the data to PHP with Ajax request and process them in another php script, for example store them in a database.
$.ajax({
url: '/ajax_catch.php',
type:'POST',
data: {
link: link
},
success: function(m){
//what to do after its done
}
});

Related

Sending data from database using Jquery Ajax JSON

I restarted my old project and have a question regarding old code. 3 years ago I had this type of code to sent data from database to JavaScript arrays using php:
echo json_encode($result_array);
and JQuery:
$.ajax({
type : "POST",
url : "poli.php",
success : function(data) {
poli_owner = $.parseJSON(data);
},
async : false
});
to populate JavaScript array.
My question is - is it still good code or not recommended anymore. If the code is not OK what code is better to use to take data from database and populate JavaScript array using php and JQuery? Thank you.
Yes, it is still good. All the web is still using Ajax and JSON.
Depending on your use case, you can load your data via a separate Ajax request, or just make your server-side language generate data for JS and include it in the page source code.

Reuse data from jquery to php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
Hi again are there a way that i can get the variable "log" in a jquery code like the example bellow with out using alert, a field id, class and etc.
<script>
var log='1';
</script>
<?php
$run = log
?>
I want to get the data of a certain variable in a javascript and use it as a value of a variable in my php code
You can't do it that way.
Because:
javascript runs in browser.
PHP runs at server.
There are two ways one is form submission and other one is ajax. You have to send the values to the server to get it to echo.
As per your updates, i would suggest you to use ajax/form submit:
Ajax:
var log = '1';
$.ajax({
url: 'path/to/php/file.php',
type:'get',
data:{log:log},
success:function(data){
console.log(data); // logs: The posted value is 1
}
});
here in the data object log before : is the key while log after : is the var which refers to value '1'. So, in php you can do this:
<?php
if(isset($_POST['log'])){
$run = $_POST['log'];
echo 'The posted value is '.$run;
}
?>
You cannot get client side variable on server side without the use of AJAX or some sort of post back. In all calls to a server, the server side code is execute then returned to render client side in the browser. Therefor, the client side code is ALWAYS run after the server side.
You can look at using client side code to make an AJAX request to the server and return a value or post to to a page which will load and render it server side.
You are trying to echo a value here, this can be done client side but if there is complex server side logic to get the value then I would use an AJAX request to get it.
AJAX
http://www.w3schools.com/jquery/jquery_ajax_intro.asp
POST a form
http://php.net/manual/en/tutorial.forms.php

append PHP in jquery [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating jQuery AJAX requests to a PHP function
How can i write PHP in Jquery
is there is a way to do this
$('button').click(function(){
<?php session_destroy();?>
}) ;
You can't execute PHP client-side.
But
You can call a php script on click to do what you want:
$('button').click(function(){
$.get("destroySession.php");
});
You can also get or post some values:
var values = {
destroy: true
};
//get request:
$.get("destroySession.php", values);
//post request:
$.post("destroySession.php", values);
You can't execute PHP client-side.
You cannot. PHP is interpreted on the server side and jQuery is interpreted on the client side.
You should either use an anchor <a href="session_destroy.php"> to go to another PHP page and destroy the session or use AJAX to call the destroy function without leaving the page.
jQuery('button').click( function () { jQuery.get("session_destroy.php"); } );

passing variables from php to javascript [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I am trying to create a site where someone can create an "item" and the database will store an id and php generates a url for that id. So next time when the person comes back with that url it will remember the person's settings (variables). Now the problem is that in my site javascript need to know these variables.
So what is the best solution for this? passing the variables in the superglobal "GET" or maybe cookies? Or is there a better way to pass these variables to javascript?
just use php to print some dynamic javascript
<script>
var myVar = "<?php echo json_encode($_COOKIE['somevalue']);?>";
</script>
There are multiple methods for providing the data to the client, such as:
Echo the variables in your javascript, var userid = <?php echo
$userid; ?>
JSON'fy your variables and supply them to your javascript via AJAX/jQuery: $.getJSON(url, function(data){ var userid = data.userid; });
I typically utilize JSON as much as possible when trying to present server-side data to the client-side, as it helps to separate the different layers.

Possible to pass a variable from jQuery with AJAX to PHP page and a session variable? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Passing javascript variable to PHP
Hi
I wonder if it's possible to pass a variable from a jQuery script to a PHP-page and put the variable into a session variable like this:
$_SESSION['mapZoomArea'] = (isset($_GET['mapza']) ? $_GET['mapza'] : "";
I'm not sure how to pass the variable and the url to the server? Preciate some help!
Thanks!
If you want a dedicated service specifically for writing this value into the session, you probably should make it a POST request (GET will work too, but GET requests should be for data retrieval, not for writing to the server).
Therefore, simply create a new PHP page, let's say "storezoomarea.php", and have jQuery make an Ajax POST request to that page:
$.ajax({url: "storezoomarea.php", type: "post", data: {"mapza": mapza}})
Then, on the server side, you can retrieve it from the _POST variable:
$_SESSION['mapZoomArea'] = (isset($_POST['mapza']) ? $_POST['mapza'] : "";
Not bad. Make sure you've called session_start(); first. Pass the variable in as a query string, 'http://whatever.com/?mapza=yourvariablevaluehere'. You can do this with jQuery by:
$.ajax({
url : 'urlhere',
data : { mapza : 'your variable value here' }
});
Ok.
Yes, you can pass that var to a php code, if you make an AJAX call, with jquery ($.Ajax(whatever)), and of course, in the file called with AJAX change the session var.
Hi you should use AJAX. Since you have JQuery available it is very simple.
More reading available here http://api.jquery.com/jQuery.ajax/
Example:
$.ajax({
type: "GET",
url: "some.php",
data: ({'mapza' : yourvariable}),
success: function(msg){
alert( "Data Saved: " + msg );
}
});
HTH :)

Categories