Modify jQuery.AJAX Values Before Send Using Form.serialize() for 'data' - forms

I have a jQuery.ajax() POST request, that runs on button click and has its data parameter set to $(this).parents('form').serialize().
There is one Textarea in the form that has a default placeholder, which I would like to replace with a blank value just before the AJAX request is sent. Is it possible to achieve this using the beforeSend event of jQuery.ajax?

You could simply manipulate the form data.
$.extend($(this).parents('form').serialize(), { textarea_name: "" });
If you don't want the value being sent at all, delete the property from the object returned by serialize().

Related

CKeditor used on form input

Is it possible to use ckeditor on a form input instead of textarea, i am building a CMS and now trying to add ckeditor and majority of of fields are form input not textarea
Thanks in advance
You can use CKEditor on an element (a div, say) that has contenteditable set. In fact, by default contenteditable elements will have CKEditor editors instantiated. It seems unconventional to use a rich text editor on an input of type text but I imagine it could be done.
As far as I know CkEditor has to be created using a textarea. In saying this, I am using it in a Razor MVC view and its one of the classes in my form..
The request will be blocked though and you will get this error;
A potentially dangerous Request.Form value was detected from the
client
To get around this, you need to get the value of the CKEditor text and html encode it. You can do this
when submitting your form, intercept it on the onsubmit function:
<form id="myform" onsubmit="javascript:onFormSubmit(this);return false" >
...
</form>
Then in this onFormSubmit function
Get the value,
Set the property value to the url encoded value
Do a ajax call to your server with the data
Your function will get the CKEditor encoded value like so:
function onFormSubmit(form)
{
var editor = CKEDITOR.instances["EditorId"];
var richtextValue = editor.getData();
var urlEncodedValue = encodeURIComponent(richtextValue);
// TODO rest of code doing ajax post to submit your form and its data
// Here you need to do an ajax call to your server pass it the form data along with the url encoded CKEditor value for that property
}

Processing of form with redirect [plesk/zend]

I am using Plesk Sample 1.5-1 as a base, but stuck on how to process POST w/parameters.
My form is a 'text' element and 'ok' submit button and below that is a list that will change based on the value of the 'text' element (external XML call).
Inside IndexController, in the ->getRequest->isPost() area, I have a redirect line:
$this->_helper->json(array('redirect' => pm_Context::getBaseUrl()));
Do I have to manually pass the parameters on this line? Or does the controller know because I created a pm_Form_Simple() and added elements? Right now if I am outside the ->isPost block, the parameters are null, so that is why I am thinking I have to manually pass them along.
Do I need something like this?
$this->_redirector->gotoSimple('my-action',
'my-controller',
null,
array('exampleText' => $form->getValue('exampleText'));
I guess am just not understanding how the POST works.
I have looked at the Zend Guestbook example, but it is different enough from Plesk that I can't mentally translate it...and it doesn't redirect to the same page, it redirects some where else.
Ultimately, I want to set the 'exampleText' parameter with a "start date" and after the POST call, make an external XML call and fill out the list... I can make the XML call, but can't get the workflow around empty form -> fill out form and press "ok" -> post processing
thx!
Turns out that the pm_Form_Simple needs JSON I didn't notice that my orig code had JSON but didn't encode the new code...

Zend: Form Validation After AJAX

I have a form done with Zend. I load it with ajax in a dialog. It has 2 selects. Depending on what is selected in the first select, it loads the content of the second one. However, when I submit the form I get a validation error because the options of the second form weren't there at the time of creating it.
Is there a way to fix this "issue"? It does what it needs to do but I don't want it to verify that field anymore. Any way to specify that I don't want that?
You can disable the inArray validator. When constructing the form's select element, set
'registerInArrayValidator' => false
Also, a different solution would be to overload the isValid method, inspect the selected option for the first select element and then populate the options for the second element. Then call parent::isValid to check if the form is in fact valid or not.

C# - Get textbox value and send to a javascript function

I have a simple form with three textboxes and a <canvas> on the same page and I have to validate these three fields and then get the values (if validated) sent to a javascript function to draw a picture and some text inside the <canvas> element. The form and textboxes wouldn't have to disappear after the values were submitted because the user could try out different results with different values. I've done other forms before but never tried using Ajax. I know I could use a client-side validation and get the textbox values with jQuery but I have more server-side code running that would make use of these three values. How can I do this?
in your controller, create a method to handle the results. I assume that this is for logging only, and does not need to actually return data.
public useResults(string value1, string value2)
{
// ... Do something with the results
return Json(true);
}
In your view, work out a way to construct the url to the above action. Perhaps in a hidden field;
#Html.Hidden("useResultsUrl", Url.Action("useResults", "controllerName"))
Then in your javascript, attach a click event to the button, as you probably have already (to trigger your javascript task) and add in an ajax call.
Note that the following uses JQuery, but you could use microsoft AJAX if you preferred;
$(function () {
$("#button").click(function() {
$.ajax({
url: $("input[name='useResultsUrl']").val(), // Get the url from the hidden field
type: "POST",
dataType: "JSON",
data: $("input[type='text']").serialize() // Get the value of the text inputs and serialise them.
});
});
// ... do other stuff
});
Your View can make an ajax call to the server using JQuery - either using JQuery.post
or JQuery.ajax - you give the Url of the controller action to the JQuery method, and it will handle the AJAX call for you.
Then, in your controller action, you return a JsonResult - which serialize the data for you into JSON format:
e.g. return Json( model );
Finally, implement a success function in the JQuery Ajax call in your view - this wil have the data returned by the controller available for you to do whatever you want with.

Is it possible to pass post data to the form (prefill form with data)?

For example https://stackoverflow.com/questions/ask there we have form id="post-form" which has method="post", is it possible somehow open this form and have lets say Name input (display-name) with my name prefilled on load?
Yes. You can do this many ways, with Javascript by setting the input's text, or you can do it server-side with PHP by echoing "value=\"$name\"" for the value attribute of the input.