I want to access javascript variable in php of the same page.
I'm doing a AJAX call and I get a JSON data.
This AJAX call is being fired upon a dropdown change.
I wanted to run few PHP functions on the same in the AJAX function based on the dropdown value.
$.ajax({
type: "POST",
url: "data.php",
data: { dropdown1: dropdown1,
variable2: variable2
},
async: true,
dataType: 'json'
}).done(function(result) {
// chart functions
<?php
$dropdown_val = $_POST['dropdown1']
?>
})
Is it possible that I can send the form value inside of the page itself to PHP function?
Suggest me please...
Unfortunately this isn't possible. You'd have to run another AJAX call and put your PHP inside that. Alternatively, couldn't you put the PHP inside the data.php file and run it from within that?
Related
I was trying to make an ajax call and show an html part inside a div class. For that i used the following way.
$.ajax({
type: "get",
url: "{{url('searchByCheckbox')}}",
dataType: 'html',
success: function(html)
{
$(".infinite-scroll").html(html)
}
});
But the problem is there is a script inside that html part which i wanted to load when i make first ajax call it's not loaded but for the second one it's loaded the script.
suppose the html response like this :
<script>
alert()
</script>
// html
How do i make it work?
I put the script above the html page which i'm getting as response.
(those who are marking the Question as duplicate should read at least what i want and what they wanted )
Im sure that the error is happening because of the script, because you are closing the </script> tag inside the html.
The best solution is to return the data from your ajax call as a json
To do that you should:
1- add a dataType to your ajax parameter as the below:
$.ajax({
type: "get",
dataType: "json",
2- In the php file handling the ajax call, you must resurn the values as a json as below:
Assume that currently you are doing the following:
echo $html
You should change it to match the below:
$retArr = array(
"html" => $html, //Your html code without the javascript function
"jsFunc" => $jsFunc //Your java script function parameters
) ;
echo json_encode($retArr) ;
And then your ajax success must be as the below:
success: function(data)
{ //You can access all the object parametes by calling the object name `data` followed by a dot `.` and then by the parameter key
data.jsFunc // Your javascript function params
$(".infinite-scroll").html(data.html) ;
}
I just need to know what all I need to do for passing my JavaScript variable to the PHP file.
All I have done till now is created a form which inputs some values alongside that I have also a JavaScript variable which I need to pass to the same location as of the form.
So how do I achieve it in Laravel 5.1?
Try jquery ajax method, in which you can pass you js variable and use it in php on any other server side scripting language.
Ajax Documentation
Syntax:
$.ajax({
url: 'process.php',
method: 'post',
data:
{
variable1 : value1, // you can use these variable in process.php file
variable2 : value2
},
success: function(response)
{
}
});
Thanks for your time in reading this post.
I am facing issues while passing javascript variable to php file.
Please look at the below code for your reference which i have written in checkout.php.
$.ajax({
type: "POST",
url: "checkout.php",
data: localStorage.getItem("simpleCart_items"),
success: function(data) {
console.log("result"+JSON.stringify(data))
My php code :
<?php $data=$_POST['data']; echo $data;?>
Redirect is happening but $data value is null.
The { data: data } is passed only to the first checkout.php call using POST method. When you redirect the user with window.location.href it has nothing to do with the previous call.
Depending on your use-case you could in the first call to checkout.php save data to session and the access it in the second call but it really depends what you're trying to do.
Edit: jQuery's data parameter is the entire payload you want to send to server. So if you were sending:
$.post({
data: {
message: 'Hello'
}
...
});
It'll be available in checkout.php as $_POST['message'] and not $_POST['data']['message'].
I know how to pass variables through AJAX calls via onClick to a PHP file and asynchronously loading the results on the initial page.
I now need to analogously pass a variable via onClick to a PHP file but I need to open a new window or redirect the whole page with the passed variable. The URL needs to contain the variable, so that the query/results can be "statically" sent to someone, like 'xyz.php?var=xyz'
I thought I could do something like this
$("#submit").click(function(event) {
var category_id = {};
category_id['linkgen'] = $("#linkgen").val();
$.ajax({
type: "GET",
url: "generatedlink.php",
dataType: "html",
data: category_id,
success: function(response){
window.open('generatedlink.php');
}
});
});
This only opens 'generatedlink.php'. I actually want what is passed via AJAX, i.e. 'generatedlink.php?linkgen=blabla' onClick in a new window/reloaded page! I'd very much appreciate your help.
just try: without ajax call
$("#submit").click(function(event) {
window.open('generatedlink.php?inkgen='+$("#linkgen").val());
});
I'm working on this function that loads a php file in the background:
<div id="content1"></div>
<script type="text/javascript">
<!--
$("#content1").load("/bm_products_filter.php");
//-->
</script>
The original php code is this:
<?php require('/bm_products_filter.php');
?>
With the original code the page works fine, with the java code it gives errors. This is because the php doesn't use the variables in the current page. I know how to pass variables to the external php but is there a way that it uses all the variables on the current page?
I am not sure if I got you wrong but you cannot load PHP files with JavaScript or JQuery.
JavaScript is client sided and has no access to the server if you want to load some data on a point of time when the template builds / is built without PHP oyu have to use an Ajax request which is also pretty simple with JQuery.
Please check out:
http://api.jquery.com/jquery.ajax/
If you can rewrite the bm_products_filter.php to return a JSON output, you can use a function as below to get values returned from the php.
jsonKey1 I have used below is one of the JSON keys returned from the php.
function getToken() {
var requestStr = "./bm_products_filter.php";
$.ajax({
url: requestStr,
type: "GET",
cache: true,
dataType: 'json',
success: function (data) {
bmProductsData = data.jsonKey1;
}
});
}
Hope this helps.