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

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

Related

How to read JSON with multiple key value pair [duplicate]

This question already has answers here:
How to parse JSON data with jQuery / JavaScript?
(11 answers)
Closed 4 years ago.
On AJAX request response is following :
[ {"status":{"login":"invalid"}},
{"user":{"username":false,"pwd":false}}
]
How to read values in Jquery.
Use JSON.parse(string) to get an Object.
After that you can access values as default:
var json = JSON.parse(res.data)
console.log(json.status.login)

Get and output JSON [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
I know this propably got answered very often but I still don't get it.
The only information I need is "up" and "down".
I got some JSON API Data and just want to get some specific data from it and just output it via alert().
What I got:
$.getJSON('URL', function(data) {
jsonData = data;
});
alert(data['items'][0]['up'][0]);
What the JSON looks like:
I tried to do this with the help of the mozilla wiki...

How to put this JS variable into PHP variable? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I would like to know how to make use of the variable hash in a PHP variable
var hash = md5(a);
Here is the equivalent in PHP:
$hash = md5($a);

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>

Passing Javascript String into php String [duplicate]

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.

Categories