This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I want to store the value of x in js variable into php variable on the same page.
Short answer is: you can not.
The reason for this: PHP is executed on the server side, and the response is sent to the browser, and (usually) generated as HTML. The javascript is rendered after this, so at this point, the PHP code no longer exists.
Long answer:
You can send a javascript variable to PHP using an XHR request (or more commonly known as an AJAX call). This will be a different request from the one that loads your initial page though. For more information see this: https://www.w3schools.com/Php/php_ajax_php.asp
you can use ajax or
add input hidden with this value and when submit form sent a value
i think in most cast ajax in best.
$.ajax({
url: 'url',
method: 'POST',
data: 'data',
cache: false,
success:function(data){
},
error: function(data){
}
}); // end ajax
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
My goal is to create a form to send an image to the server through Ajax via jQuery.
I already asked the question here (Problem sending a form with a jquery file component by ajax), but it's been closed and it still doesn't work. From my question, I changed the sending function like this (according to this post: jQuery AJAX file upload PHP):
$( "#sendProfileImg").on('submit', function(e) {
e.preventDefault();
var file_data = $('#profileImgFile').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
console.log(form_data);
$.ajax({
url: 'uploadImage.php',
data: form_data,
type: 'POST',
dataType: 'text',
contentType: false,
cache: false,
processData:false,
success: function( data ) {
console.log(data);
}
});
});
But the answer I get from my uploadImage.php file (which consist of just var_dump($_POST);) is the following:
array(0) {
}
Any advice?
Since your request only has the file you might want want to use the $_FILES variable instead of $_POST. Uploaded files can only accessed through the $_FILES variable.
Thank you very much everbody, the problem is solved.
I discovered 2 issues:
I have to use form_data.append. I didn't know this function and it wasn't on the tutorials I followed. Furthermore, I had to use this funtion for every field of the formular, not just the one with the file
$_POST return no information about the posted files. That's why I didn't get any return about the posted information.
Thanks again everybody
three things
use $_FILES instead $_POST please check this url
check if you have an updated browser(not all browsers are compatible with FormData)
check the browser compatibility here
This question already has answers here:
Jquery call another function after ajax in loop is done
(1 answer)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I am writing a code in which user checks multiple check boxes and those users assign to the selected division. I am using $.post function in a loop of check boxes. Everything is working fine and checked users are being assigned to the selected division. Now, the issue is that I want to print a response after $.post function but everything I write after this post function, javascript executes it before that function and that's why, I am unable to print the response. Please check the below code for your convenience and suggest a possible solution.
Thanks a lot
function assign_user(div_id){
//alert(div_id);
var favorite = [];
$.each($("input[name='user']:checked"), function(){
value = $(this).val();
$.post("../db/assign_user.php",{div_id:div_id,value:value},
function(x){
});
// I want to print final response here but it executes before post function
});
}
Ajax requests are asynchronous. They are sent to the server, then the javascript continues on without waiting for a response. If you type your Ajax request like:
$.ajax({
url: "../db/assign_user.php",
type: 'POST',
data: {div_id:div_id,value:value},
success: function(data){
alert(data);
}
});
then in the success function, you can handle or alert the response.
This question already has answers here:
How can I upload files asynchronously with jQuery?
(34 answers)
Closed 9 years ago.
I have this Js/Ajax code:
<script type="text/javascript">
$(document).ready(function(){
$("#message").hide();
$("#addsliderimage").submit(function(e){
e.preventDefault();
dataString=$("#addsliderimage").serialize();
$.ajax({
type: "POST",
url: "addsliderimage_go.php",
cache: false,
data: dataString,
success: function(res){
//$("#message").show();
$("#message").html(res);
$('#message').fadeIn('slow');
if(res.indexOf("success")!=-1)
{
window.location.href = res.substr(8);
}
}
});
});
});
</script>
when the form is submitted, the page with the form on doesn't change the page is just submits the data to the URL in the ajax code.
i know this works as im using it on other forms however on this particular form im uploading images/files so i need a way to set the enctype to multipart/form-data
If you want to upload the files using AJAX you propably should use the well-known "hack", a simple iframe with a simple form that is submitted not by AJAX and catch the load event of that iframe so you can get the result of the upload.
For the part you have doubt in, well, since you prevent the default event when the "presubmit" (to name it some way) function ends nothing happens (you know, the default event return "true" after the submission), so you need to tell before the closure you're passing to submit function that it is needed to be submitted, but you gotta handle whether it is being submitted twice or not.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby on Rails: Sending javascript data to ruby controller
Total noobie with JSON, javascript and RoR
In my RoR application I have some javascript on a webpage. I would like to pass in one of the arrays I have built up to a Ruby function in my controller (is that possible?)
I've read somewhere I need to convert to JSON. Using this command gives me something I can work with (totalChanges is my array)
JSON.stringify(totalChanges)
but how do I pass this to a ruby function? If anything doesn't make sense please straighten me out.
Thanks
EDIT:
I'm using jquery. The comments have pointed out that I can use something like http://api.jquery.com/jQuery.ajax/ to send data over to a controller.
the particular action I want to do is save some data (in a javascript array) to a database and then reload the page to show the new data. jquery.ajax just seems to have so many options I'm a bit lost.
You can place an AJAX request using jQuery like this:
$.ajax({
type: "POST",
url: "/route/to/action",
data: { mydata: JSON.stringify(totalChanges) },
success: function(response, textStatus, jqXHR) {
console.log("Yay it worked.", response);
}
);
In your action, you can then retrieve the data passed to your action like this:
def action
mydata = params["mydata"]
# Save it to the database...
end
I want to use JavaScript variable into python Block.
<script>
$(document).ready(function() {
$("#WO_cpp_id").change(function() {
id = this.selectedIndex;
ajax('{{=URL(r=request,f='get_CIs',vars={'CPP_Id':'#here I want to use id variable')}}', ['WO_cpp_id'], 'WO_ci_id');
})
.change(); }); </script>
Thanks in Advance
Your python code is running on the server. Your JavaScript code (as quoted) is running on the client. So you can't directly use a JavaScript variable in your Python code. What you do is send the data you want to send from the client to the server in any of several ways.
One of those ways is "ajax". This client-side code will send the contents of the variable foo to the server as a "fooParameter" parameter on a POST:
var foo = "This is some information";
$.ajax({
url: "myscript.py",
method: "POST",
data: {fooParameter: foo},
success: function(responseData) {
// Successful POST; do something with the response if you want
},
error: function(jxhr, statusText, err) {
// Error, handle it
}
});
More in the jQuery docs and the Wikipedia article on ajax.
That won't work. Python runs on the server before the page is ever rendered on the client; Javascript runs in the browser after the page is rendered. The id variable isn't even set when the Python code runs.
Instead, you should have your javascript code add the extra data you want to set to an existing query string (or by using the data attribute of jQuery's ajax options).