Passing Javascript String into php String [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Passing javascript variables to php?
(5 answers)
Closed 8 years ago.
let's say i have:
<script>
var jsString="hello";
</script>
and i want it to pass into php string:
$phpString = jsString;
how do i do that correctly?
please tell me the right way. thanks in advance.

You need a Ajax call to pass the JS value into php variable
JS Code will be (your js file)
var jsString="hello";
$.ajax({
url: "ajax.php",
type: "post",
data: jsString
});
And in ajax.php (your php file) code will be
$phpString = $_POST['data']; // assign hello to phpString

You will need to use an HTTP POST to send the data to PHP. Check out this tutorial: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php to see how to send a post without JQuery. Also see the XMLHTTPRequest docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest. As other answers have noted, JQuery is the makes this much easier with $.post: http://api.jquery.com/jquery.post/.
To get the string in PHP use the $_POST variable.

Related

How do I pass variables and data from JavaScript to PHP? [duplicate]

This question already has answers here:
How do I edit PHP variable with JavaScript/jQuery?
(2 answers)
jQuery Ajax POST example with PHP
(17 answers)
Closed 2 years ago.
How do I pass variables and data from JavaScript to PHP?
like this:
var client_code = $('#client_code').val();
$client_code = client_code;
You can't pass data from JS to PHP directly. But you can use AJAX functions to pass the data as asynchronous.
You can read from https://www.w3schools.com/jquery/jquery_ref_ajax.asp

How does an onclick funktion work, how do I get php informations to javascript, how do I get javascript informations to php and how do I use ajax js? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
I try to use an onclick function. If the user clicks the html button a js function should be done. For this function I need some variables of the php code. How do I get them?
If I get them I have to change them. So how do I got them (the changed variables) to php?
I thought that ajax could perhaps help me. But how do I use ajax?
Using a library like Axios or jQuery might help, compared to using Javascript's fetch function. Here's how you might achieve that using jQuery:
HTML
<button onClick="handleClick()">Click me<button>
Javascript
function handleClick() {
console.log('Clicked the button')
$.get('path/to/script.php', function (data) {
// It helps if the data returned in the PHP script is JSON formatted
console.log(data)
});
}

Pass a value from javascript to php [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 6 years ago.
I want to send the value of address to a new PHP page
<script>
var address = place.formatted_address;
document.getElementById('af').innerHTML = address;
</script>
PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:
<script type="text/javascript">
var foo = '<?php echo $foo ?>';
</script>
To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});
On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];

Serialize array to query string parameter [duplicate]

This question already has answers here:
Query String generated by $.param() include square brackets for array
(2 answers)
Closed 7 years ago.
Currently I do some code refactoring and try replace generation of query string by concatentation to serializing of json object
From:
$.ajax({
url:'./services/DataService/getDetails?metric=9&'+dashBoards.getFilter()+'groupby=quarter&'+dashBoards.options.time.start1+'&'+dashBoards.options.time.end1+'&peergroup='+dashBoards.options.peerGroup,
type:"GET",
To:
$.ajax({
url:'./services/DataService/getDetails',
data: jsonObject,
type:"GET",
Almost everything works fine, except one thing. If jsonObject contains array field it looks in query string like that:
...?metric[]=1&metric[]=3
Instead of
...?metric=1&metric=3
Is there way how can I fix it? Thanks!
You can fix it adding to $.ajax parameter traditional: true.
Here you can find reference why

Get Yii user state variables in javascript [duplicate]

This question already has answers here:
In Yii, pass PHP variables to JavaScript
(6 answers)
Closed 8 years ago.
I am using Yii::app()->user->setState to set some user's session variables.
Is there a way to get the values of them from within JavaScript?
Something like this perhaps? You have to echo it to the JavaScript.
<script>
var myVariable = "<?= Yii::app()->user->getState('myVariable', '') ?>";
alert(myVariable);
</script>

Categories