Sep
28
Posted on 28-09-2006
Filed Under (AJAX) by jay

This is a quick little tip on how to pass more than one Form Element to the Ajax.Updater function in Prototype.

This came about because I wanted to do a form POST and not a GET with several Form fields but the only documentation on the internet explained how to send ONE Form field.

It's very simple but it took me a while to really get it right.
Here is a snippet:

JavaScript:
  1. var params = 'title='+txtTitle+'&description='+txtDesc+'&link='+txtLink+'';
  2. new Ajax.Updater('statusMessage','myphppage.php?myparam=save_item, { method:'post', parameters:params,asynchronous:true,  onSuccess:handlerSaveChannel });

This code is using the Ajax.Updater to send a POST to a php page with 3 form fields to be processed. As an added tip I am sending my request to a page and I am using a URL Parameter to tell that page which function to run: myparam=save_item.

On the server side I am getting the form fields from the POST:

PHP:
  1. $myparam = $_GET['myparam'];
  2. switch($myparam) {
  3. case 'save_item:   
  4.     $title = $_POST['title'];
  5.     $description = $_POST['description'];
  6.     $link = $_POST['link'];
  7.        .....

First I am getting the URL parameter to set which select case to run using the $_GET['myparam'].
Then I am stepping into the select statement and getting the 3 form fields from the _POST.
Now I have the values from the form all posted to the server using an Ajax.Updater.

Heres the full code:

Client Side:

JavaScript:
  1. var handlerSaveItem = function SaveItem_Callback(str) {   
  2.    document.getElementById("statusMessage").value = str.responseText;
  3. }
  4.    
  5. function SaveItem() {       
  6.    document.getElementById("statusMessage").value = "Saving Channel...";
  7.     //get the values form the form
  8.     var txtTitle = document.getElementById("txtTitle").value;
  9.     var txtDesc = document.getElementById("txtDesc").value;
  10.     var txtLink = document.getElementById("txtLink").value;
  11.     //place the values in a parameter as a name value pair: name=value
  12.     var params = 'title='+txtTitle+'&description='+txtDesc+'&link='+txtLink+'';
  13.     new Ajax.Updater('statusMessage','myphppage.php?myparam=save_item', { method:'post', parameters:params,asynchronous:true,  onSuccess:handlerSaveChannel });
  14. }

Server side:

PHP:
  1. <?php
  2. if(!isset($_GET['myparam'])){
  3.   die("No function Value given");
  4. }
  5. $myparam = $_GET['myparam'];
  6. $myval = $_GET['myval'];
  7. $content[0] = $_GET['content'];
  8.  
  9. switch($myparam) {
  10. case 'save_item':
  11.     $title = $_POST['title'];
  12.     $description = $_POST['description'];
  13.     $link = $_POST['link'];
  14.    
  15.         $return = "The Title is ". $title;
  16.    
  17.     break;   
  18. }   
  19.   echo $return;
  20. ?>

This code is stripped right down but you get the gist of it. Once you've got the values form the POST you could insert them into a data base and then return a status message.
You could also create several case statmenst for doing database operations for Select, Insert, Update and Delete.

    Read More   

Comments

AnferTuto on 28 July, 2007 at 5:23 pm #

Hola faretaste
mekodinosad


YAAK on 2 December, 2007 at 1:13 pm #

That was really great and helpful. I had the same problem. thank you very much.


Post a Comment
Name:
Email:
Website:
Comments: