Jquery Ajax load/post [closed] - ajax

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Im trying to get the .load method to post to a page and then echo back what was posted to see if it is working, i can't get this to work. I tells me that the variable is undefined, here is what i have:
teachers.php:
<?php
echo $POST['class'];
The JS:
$('#test').load('teacher.php', {class: 'test'});
That is triggered once something is clicked.
#test is a <div> that I want the data to post to.
Thanks for the help.

It's $_POST instead of $POST. Don't know if that was a typo, but that will also create some issues.

Most of the times problems like these are due to the asynchronicity of AJAX.
Everything you load via AJAX (with .post(), .load(), .ajax(), .get()) doesn't "exist" unless the AJAX round trip of request-response has been completed.
Be sure to "use" what you load with AJAX (a HTML bit, a JSON response, ...) in a callback function, that is called on the success of the AJAX request.
For example let's say my page ajax.php returns this string: <div id='ajaxdiv'>hello!</div>, we want to load this (HTML) string in #mydiv:
$("#mydiv").load("ajax.php");
console.log($("#mydiv #ajaxdiv")); <- undefined
It won't work because the code continues regardless of the completion of the request. Instead:
$("#mydiv").load("ajax.php", function () { console.log($("#mydiv #ajaxdiv"));});
works because JQuery makes sure to use the callback function only if the request has been completed.
(You can also add parameters like your {class: test}, just put them between the URL and the callback function).
Edit: use $_POST and not $POST.

if your just trying to post information from jQuery to a PHP file when something with the id of test is clicked then do this:
teachers.php:
<?php
echo $_POST['class'];
?>
The JS:
$('#test').click(function() {
$.post('teacher.php', {class: 'test'},
function(output) {
//output = what is echoed from the php file.
alert(output);
);
});

Related

In which scope my ajax data send from view to handler is stored in coldbox

Please let me know answer if any one knows about it.In which scope my ajax data send from view to handler in coldbox
When you're making an ajax POST, it gets treated as a brand new request. This means you'll need a separate route and handler for that request.
Within your new handler (let's call it /handlers/data.cfc) you'll want to format your response appropriately for your code. ColdBox comes with some nifty tools to help you do this. One way would be to use renderData() within your handler or view.
Rough example:
event.renderData( type="json", data=yourData );
Once set up correctly, the ajax calling code should receive the formatted data from your new handler as expected.
Side note: I recommend including code samples when asking questions on StackOverflow. It will help those that want to provide assistance understand exactly what you are trying to do.

How, in Node.js, to respond to a GET request from client (send some HTML in response)?

I have a Node.js app which, among other things, responds to a AJAX (jQuery) $.get() request from a Web page by sending some HTML back to that page. The app uses Express.
So in the server code, I have:
app.get('/friends', api.friends);, where api is defined as api = require('./static/routes/api') and i'm setting app.use(app.router);.
In myapi.js module, I have api.friends code: I have
exports.friends = function(request, response)
{
...lots of code...
};
wherein I create some specific HTML.
Now, my question is: How do I actually send this HTML back to the client? I can't use the passed-in response object because this is no longer an Express-type response object, so the usual reponse.send(), .end(), etc. methods don't exist.
I have no idea what to do, reflecting a lack of understanding of Node and its innards (this is my first Node app), so any and all help will be greatly appreciated and welcomed. Thank you.
As #Daniel stated in his comment, the response object is certainly an Express object, and you can return your HTML simply by rendering a view, like so:
exports.friends = function(request, response) {
//do stuff
response.render('friends.html');
};
Of course, you would have to define your views in your app.js setup, with something like this:
app.set('views', __dirname + '/views')
Ugh! I am such an idiot! In my exports.friends handler, there's a request going out which, as a part of its calling parameters, takes a function of the form function(error, response, body). Note the response parameter. I was sending the results within this function, which of course used this response object and not the one passed through exports.friends(request, response). Doh. Oh well - thank you anyway - you made me look at the code again, and with the persepctive of knowing that the response object was legitimate, I was able to see the error. Thank you again - appreciated!

How do I prevent tampering with AJAX process page?

I am using Ajax for processing with JQUERY. The Data_string is sent to my process.php page, where it is saved.
Issue: right now anyone can directly type example.com/process.php to access my process page, or type example.com/process.php/var1=foo1&var2=foo2 to emulate a form submission. How do I prevent this from happening?
Also, in the Ajax code I specified POST. What is the difference here between POST and GET?
First of all submit your AJAX form via POST and on a server side make sure that request come within same domain and is called via AJAX.
I have couple of functions in my library for this task
function valid_referer()
{
if(isset($_SERVER['HTTP_REFERER']))
return parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['SERVER_NAME'];
else
return false;
}
function is_ajax()
{
$key = 'HTTP_X_REQUESTED_WITH';
return isset($_SERVER[$key]) && strtolower($_SERVER[$key]) == 'xmlhttprequest';
}
You might read this post regarding difference between post and get
While as Jason LeBrun says it is pretty much impossible to prevent people simulating a form submission, you can at least stop the casual attempts to. Along with implementing Nazariy's suggestions (which are both easy to get round if you really want to), you could also generate some unique value on the server side (i'll call it a token), which gets inserted into the same page as your Ajax. The Ajax would would then pass this token in with your other arguments to the process.php page whereupon you can check this token is valid.
UPDATE
see this question with regards to the token
anti-CSRF token and Javascript
You can not prevent people from manually emulating the submission of form data on your website. You can make it arbitrarily more difficult, but you won't be able to prevent it completely.

Magento - Communication between 2 blocks

I made a module with 2 blocks:
- a block in the right column contains a form,
- And a block in the middle of the page
I would like the block of the middle of the page receives the variables sent by the form of block on the right.
I am a bit lost ...
Thanks
Submitting a form requires the page to re-post, so you should be able to get the parameters from the posted array directly. If you don't want to redraw the entire page, you'll want to use Javascript to read the input fields and change the second block directly.
More detail on what the two blocks are supposed to do would be helpful. Also, you're likely to get more responses if you accept answers to questions that you post. You have 8 questions and no accepted answers right now, which will discourage people from answering.
Thanks,
Joe
It's still not clear whether you want to update w/ Javascript or after posting the page.
If Javascript, do something like this:
var boxval = $("#elem1_id").val();
$("#elem2").append(boxval);
If posting, set some name for the input and do this in box two's class:
public function __construct() {
parent::__construct();
$this->setPostedValue($this->getRequest()->getParam('box1name'));
}
And in your template:
<?php print $this->getPostedValue(); ?>

jQuery AJAX Form, cannot send array?

I'm practicing my jQuery skills (ok, learning too) and experienced an issue. I got a file upload form with file input. I'm using this plugin (http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Uploading) to upload multiple files at once. So I'm using following form input:
<input type="file" class="multi" name="photo[]" accept="gif|jpg|jpeg|png" maxlength="5"/>
Now... I'm trying to send an AJAX request to php file that will handle upload and server-side validation:
$('#upload_photos_s').click(function(q){
var photo = $('[name=photo[]]').val();
// Process form
$.ajax({
type: "POST",
url: "upload.php",
data: 'photo[]='+photo,
success: function(html){
alert($('[name=photo[]]').val());
$("#photo_upload_form").html(html);
}
});
return false;
});
While using Firebug I can see that there's only one file in photo[].
Any suggestions why? Is there something I missed?
Regards,
Tom
As it stands, you are indeed querying the value of the first photo[] member only. photo[].val() will not return an array containing all the values.
You would have to run through each member of photo[], e.g. using each(), to build an array of values.
However, I'm not sure this is the right path to go for whatever you want to do. You are aware that what you are doing is uploading the file names only, not their data?
It is not possible to upload files using AJAX without the help of additional tools like Flash-based SWFUPload. This is for security purposes to prevent scripts from having direct access to local files.
Maybe what you're trying to do is best suited for an approach where the form's target property points at an <iframe>. That would not trigger a reload of the page, but still submit the form the "traditional" way, allowing for old-school file uploads.
Well on the link you provided, there is a part called Ajax specifying the simplest way is to use the jQuery Form Plugin.
Documentation of plugins help a lot usually ^^.
Have a nice day :)

Resources