Pass data from javascript to PHP in cakephp - php

Hello I have a select element which has a few options. I am able to get the selected option when an onChange event is fired. Now I need to pass that option text to php, either with pageload or ajax.
echo $form->select('data_source_select',$dataSourceOptions,null,array('escape'=>false, 'empty'=>'Select One','onChange'=>'getData(this)'));
is my select form element, with its options being set in controller. Now onchange, I need to pass the selected option to php/action to load the data specific to this option search. Any help would be great.
Thanks.

As you said, you could either do this as a regular form submission, or as an Ajax form submission. My answer will focus on regular form submission, since Ajax will essentially work the same way and can simply be added on to enhance it later.
One quick solution is to add a hidden field to your form, maybe call it "step" and give it a value of 1. When the data source is selected, simply submit the form and check the value of "step" in your controller. If it is equal to 1, you'll know to load the data based on data_source_select. If the value of "step" is not 1, it indicates that you're on another step in the process and you don't need to process the data source value again.

Related

How to do ajax form post while page load after submit a form in symfony2

I have to retain drop down value and also some specific data according to the drop down selection.
Normally its working fine for me but i need same thing after submit a form (save the data), after save i need to retain the same.
Please advice about this or any other solution already having?

dynamically filled forms with Jquery & Ajax

I have a bit of a problem achieving what I'm trying to do.
I'm doing a mailbox like page and I'd like to add a "Respond" button which will fill a form with some values.
I'm generating my page message by message in php. For each message I generate an input button with its own id and when I click on the button, I'd like to open a form in a new window, with the recipient and title prefilled and not changeable by the user, with another textarea that the user can fill with his message.
For that, I have a general template for a form containing 3 values (recipient, title and message). All are (for the moment) text or textarea values.
Is it possible to use one form to send a message to anyone, or do I have to generate a form for each message with pre filled values ? The last solution seems quite horrible to do.
I tried to find something that I could do with only javascript, since I want the form to be submitted asynchronously by Ajax.
I can make an onClick event and pass the values to that function, but in that case I won't have the values of the textarea the user will have entered.
Is there a way to do, for example, an entire form in Javascript with the values I'll pass through the onClick event, get the value of the textarea from the form that would have been populated with these values, and then send everything through Ajax ?
Well, here is what I did.
For each Respond button generated, I set an onClick event passing the fields I wanted to be pre-filled in my form to a JS function RespondMessage()
I also made a form at the bottom of my page that I hid with css.
This form contains as much hidden fields as I want pre-filled datas, and as much spans as I want to show infos.
When I call the RespondMessage function, I manually set the hidden fields in my form with the arguments I called the function with.
After that I fill my spans with the infos I want to show (for example, I set the recipient's name for my message like that).
Once all of that is done, your form is ready to be displayed. So after setting the fields in the form, I just pass the form from display:none to display:block, and do a lot of css tricks to make that smooth and beautiful.
So now you have your pre-filled form, you just need another function that will make an Ajax call to avoid reloading the page after each response, and you'll call it with the onClick event of your form's submission button.
I hope that this process will help someone.
I'm not quite sure that it's the best way to do it and I find it quite dirty and exploitable so you need a bit more server side checking than usual, but it does the job.
I don't provide code because it's mostly all really specific to the application you're setting up.

HTML form within another form

I am new to html, I would be really glad if you can help me with this.
I have a web page where there is a list and some other text inputs and buttons. This option list can be populated by clicking the "add" button in the page, this add button is to direct to another page and in that page there are some chekboxes, those which are checked are loaded back to the main page,(where I have the list) .
At the end data in the main page needs to be loaded to the database, (what is in the list and in the text inputs).
Since I'm new I just know little about php and html, I thought I should have a form within a another form(form to "add items", form to load to the database) and it is not possible in html. Can anyone suggest the best way to do this? Do I need to use javascript?
Why can't the extra inputs (the ones that would be in the second form) be part of the first form? I think the question will become clearer if you post a sample form so we can see the relationship between the two forms.
But overall, since you're ultimately only submitting one form, then maybe all the inputs belong together. If what you're calling the second form isn't supposed to be visible right away, you can still have it be part of the same form, but only reveal it when needed.
Again, some sample data would help to understand the exact context of your question.
in php if you use input name="somename[]"
for a number of input elems
you will get an array in $_POST['somename'] and access all the values.
I think what you're after - if I understand you correctly - is ajax. Ajax allows you to asynchronously send data to/from another script without leaving the current page. This is accomplished using JavaScript. In your case I think what you need to do is set an onclick event in JavaScript to a button:
<input type="button" onclick="javascriptFunction()">
You can read more about ajax here:
http://www.tizag.com/ajaxTutorial/ajaxform.php

submit form on checkbox select

I have a table listing various line items. Each row has a checkbox. I decided to add a form for each line's checkbox, because I need to submit only one value when it is checked or unchecked.
I tried the following and indeed I get form submission, but how do I update my db? (I use PHP)
$(".rowChkeckBox").click( function(e){
$(".chrowChkeckBoxkBx").val( e.value );
$(this).closest("form").submit();
});
Thanks.
You need to use AJAX for that.
jQuery comes with an own AJAX class (See http://api.jquery.com/category/ajax/)
Fill an AJAX request with the data of your form elements and then send it to a PHP file that is updating the database.
You don't even need to use forms for that, just listen to the click-event of the checkboxes and run that AJAX request once it is clicked.
After that you may update your table (HTML) using $(this) as your clicked checkbox (You can use jQuery's traversing methods to get to an element next to it etc.)
In the PHP file you need to parse the data of your elements via the $_POST or $_GET superglobal and write them into a database with SQL queries (http://php.net/manual/de/book.mysql.php)
I hope I understood your problem.

Best practice for sending AJAX data via form - retrieving value back

I have a small form where I want the values to be updated on change for my select boxes which can be done with:
$("form#updateclient select").change(function(){ // use one selector for all 3 selects
$.post("inc/process-update.php",{
// data to send
completed: $("select#completed").val(),
hours: $("select#hours").val(),
update_id: $("#u_id").val(),
who: $("select#who").val()
}, function(data) {
// do callback stuff with the server response 'data' here
});
});
But I have a input text field, where a user can enter the amount of hours, when they click out of the box THEN send that AJAX request to a PHP page.
And the #completed field is now a checkbox, does that pass the same was as an input? How would that work with jQuery, so if you check or uncheck send that info and update database
The thing I have been trying to research now is what is the best way to retrieve those values from the server and update them on the page using AJAX. So once i update my dropdown, it will keep that same result.
Any ideas?
And the #completed field is now a checkbox, does that pass the same was as an input?
No. A checkbox's .value (as used by jQuery's val() method) is always the string in the value="..." attribute (defaulting to “on” if omitted), regardless of whether it is ticked or not. To get the effective value of a checkbox you need to look at the ‘.checked’ property. eg.
var v= element.checked? element.value : '';
Also, you can't use an onchange handler to detect when a checkbox is changed, as IE doesn't fire that event. Instead, you have to use onclick for that element.

Categories