How to send data collected by javascript via form - php

At now in view I have a ajax button and pass data like this
echo CHtml::ajaxButton(...,'js:$("#a").sortable("serialize")',..)
but how to pass this data with a form if i want to use form model?

jQuery is native to Yii, so for a start, use that.
Then just do something like (untested):
$('form').submit( function() {
$.post('/url/', $(this).serialize());
});

Related

jQuery include form elements submit

Working from the ground up on a Joomla Component re-config. I'm trying to incorporate an AJAX search function in my component. So far, I have this:
$(document).ready(function() {
$('form').submit(function() {
var results = $('form').serialize();
var url = 'index.php?option=com_mls&task=ListData&format=raw&' + results;
$('#test').html(url);
});
});
This just need to dump the values of the the form elements into a div. right now, it will display the text string, but not the results var.
You are using $('form').serailize() so if your form input elements don't have any name attributes attached to them - which serialize will turn that into the key.. Then you won't get anything when you serialize the form.
Also since you are inside the form's submit function.. it would probably be better to use $(this).serialize() - that way if you ever have multiple forms on one page - it will know which form is getting submitted and serialize the correct form.

How do I use JQuery's "load" function to get the wanted data in a variable?

I need to know how to use jQuery's "load" AJAX function to get the wanted data in a variable?
Should look like this in the end:
var data = "->load data<-";
Thanks!
The load method is there to directly append the data to a jQuery element. Instead you could use jQuery.get or jQuery.post to issue a GET or POST request. Eg.
$.get("url.to/load/from", { param: "Hello" }, function(data){
var loadedData = data;
});
It's a bit difficult to say what you're asking here...
There are 2 load functions in jQuery's core.
If you want to get some data through an ajax call, you should take a look at other jquery ajax methods such as get,post,ajax... and so on:
var data = null;
$.get(url,params,function(response){
data = response;
});
If you want to listen to an load event (such as when the window or an image is loaded) you can do something like :
var data = null;
$(window).load(function(){
data = 'some data';
});
Use http://api.jquery.com/jQuery.get/ or jQuery.post with callbacks to store data into variable.
I hope it will help you
.load is specifically a convenience wrapper to place data directly into a DOM element, without needing to go through custom functions to do so. Use one of the other AJAX methods, like .get.
I have successfully implemented jQuery post
$.post("loadurl.php", { param: value },
function(data) {
alert("Data Loaded: " + data);
});
});

Equivalent of $_POST in CakePHP for ajax calls?

Normally, when I'd do an ajax call to a page with jQuery's $.post(), I'd post to a specific page (i.e. ajax.php) with something like:
var submissionId = 1;
$.post('/ajax/ajax.php', {
submissionId: submissionId
}, function(data) {
alert(data);
});
and inside ajax/ajax.php, I'd manipulate the data how I'd want with $_POST['submissionId']. What is the equivalent to this in CakePHP if I'm posting to a controller?
Do I still use $_POST['submissionId'] or $this->data?
If using $this->data, do I need to create a <form> to wrap the event handler in?
you should create form with cake helper (hide it with css or jquery if you don't want it to be there) and use jquery form plugin
you can submit the form with jquery with submit()
so in the controller you can use $this->data

submitting a form with out using submit?

I want to submit a form without using submit button how can i do that?
Using jQuery you can do this. Check this
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Use javascript. Something like
document.forms["myform"].submit();
or
document.myform.submit();
You need to set the name (1. example) or id (2. example) attribute for your form to make this work.
Through javascript you can call form.submit()
Use jquery's form methods to serialize the form variables and send via ajax.
http://api.jquery.com/category/forms/
You can add some javascript logic to ANY submit methods by passing a function to the form's submit event handler.
Eg.
$('#my_form').submit(function(){
alert('Handler for .submit() called.');
return false;
});
Returning false blocks the form from being submitted by all other methods (including the "traditional" submit button). You'd put your ajax code before the return statement.
or add bind the submit function to ANY dom element (image,button,etc.)
Eg.
$('#my_cool_image').click(function() {
$('#my_form').submit();
});
See more at http://api.jquery.com/submit/
Good Luck

How to POST without using Submit?

I want to post the Form but don't want to use the Submit method. If I use JQuery, how to handle the Form input controls?
You can use the jQuery AJAX .post function functions. An example (untested, but should be working):
<script>
function postit(obj) {
var data = $(obj).serialize();
$.post($(obj).attr("action"), data, function() {
//Put callback functionality here, to be run when the form is submitted.
});
}
</script>
<form action="posthandler.php" onsubmit="postit(this); return false;">
<input type="text" name="field">
<input type="submit">
</form>
Also, read about serialize
(Of course you need to include the jQuery library in your code before using this code).
Just create a function that is triggered by whatever event you want, for example: (found this code in another question)
function example() {
// get all the inputs into an array.
var $inputs = $('#myForm :input');
// not sure if you wanted this, but I thought I'd add it.
// get an associative array of just the values.
var values = {};
$inputs.each(function() {
values[this.name] = $(this).val();
});
}
After that you can do whatever you want with the input values. You might want to consider using more advanced processing though, there are plenty of plugins that can provide this kind of functionality.
I am not sure if I understood the question correctly.
If you don't want to use submit(), you can do the same thing via jQuery.post() using Ajax. The main difference is you have to construct the key value data from the input fields yourself rather than the browser doing it automatically and you won't get a page refresh.
Either Post function or Load function will work.
#PRK are you trying to post the Form when the page loads or when a user hit a button?
load(url, parameters, callback)
eg:
$("#loadItHere").load("some.php", {somedata: 1});

Categories