hi guys I need a prototypr function to submit my form but I need it to be an onclick="" one cause I use a function to call forms come in to a div in html code and I understood that the javascripts does not redetect forms and divs in my page
depends once again on what you're trying to do. where are you submitting it to?
js will find the new form, what it won't do is eval js that is created. you can however still include calls to an existing function within the populating code.
prototype has a bunch of form handling, just need to know what you're trying to accomplish.
I want submit the form via ajax and place the result from php file in a div where I have loaded the form
I used below code but after submitting the form the whole page refreshes don't know why?
Code:
// here we define global variable
var ajaxdestination="";
function getdata(what,where) { // get data from source (what)
try {
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { /* do nothing */ }
document.getElementById(where).innerHTML ="<center><img src='img/indicator.gif'></center>";
// we are defining the destination DIV id, must be stored in global variable (ajaxdestination)
ajaxdestination=where;
xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
xmlhttp.open("GET", what);
xmlhttp.send(null);
return false;
}
function triggered() { // put data returned by requested URL to selected DIV
if (xmlhttp.readyState == 4) if (xmlhttp.status == 200)
document.getElementById(ajaxdestination).innerHTML =xmlhttp.responseText;
by setting action="#"
and on input button nclick="getdata('view.php?id=$r[id]&ob=article','loading')"
ok, you can use all that if you want...... or you can use the ajax handling prototype has built in.
in your other thread i put up a link to Ajax.Updater, which is what you want here.
here is the code to take all the values from a form, send it to a php page with an ajax request and update an id with the return.
Code:
new Ajax.Updater('idofcontainer', 'mypage.php', {method: 'post', parameters: $('formid').serialize()});
Updater will handle creating the request and post the values from your form to the php page you specify. Upon return, it will update the container you specify with the results. There are actually a LOT more options and callbacks you can use, but this basic example should get you going.
If you want to you can include all of this in a function so you can call it all over the page if you so desire.
Code:
function updater(itemid,form){
new Ajax.Updater( itemid, 'func.php',
{ method: 'post', parameters: $(form).serialize()
} );
}
you have an event observer acting on the form you're submitting with updater, i don't have any idea why you'd do that but it sure would cause a conflict.
don't use void... don't use a link or a submit or whatever at all. just make a regular button or something and use an onclick event to call the function. then it would probably avoid that observer because it isn't submitting in the traditional sense.
thanks dude
I executed the function successfully on a test page but on the main page
gives an error like undefined form key
I think it's because not being compatible with some other scripts I use?
I think 1.6 also tried 1.5.0 no success
perhaps I should use some other functions to send the file for me
do you know a free ebook for learning prototype seems nice to learn I'm a php scripter
Last edited by SarahAria; 02-12-2009 at 04:54 PM..
bottom line, updater is normally super easy to use. i don't know how you're generating these forms (and i've never seen form-key or isAjax used as a param before, nor are they in the API that i see) but you must be hitting a conflict with some of your older code somehow. the fact that it worked on a test page but spazzed out on you on the main page supports this.
the page is kind of a mess. out of general principle i'd say you might consider a clean start for it. are you really using everything that's in there? could it be cleaned up to remove the potential for conflict? i'd suggest re-evaluating what your page needs to do and building the logic from the bottom up again. it looks to me like you have redundant stuff in there.
at the very least, just take a look at what you need your js to do. i think you'll find you can do a lot more with a lot less.
and once again, you need those values you're passing in quotes... i see you forgot that again.
if you'll make a list of things you need your js to do throughout the flow of the page i'd be happy to show you what i think might be the best way to accomplish those goals.
thanks dude now I'm sending all data from form to the php file I'm really learning something!
the only problem is that the file (from form '<input type=file>') is not sent.
What I am doing:
right now I'm writing a Graphical CMS in php 80% it's now completed the only problem began since using ajax to make my cms really special without knowledge about it.
the main problem:
and right now I'm stocked at sending forms with files in ajax while the form is loaded with ajax.
I'll try to upload a beta version tommorrow so you can see where the problem is
thanks
Sarah
Last edited by SarahAria; 02-13-2009 at 10:03 PM..