PDA

View Full Version : Ajax Submissions (Different Concept)


Jazz914
05-10-2010, 11:19 AM
I'm currently trying to get used to JQuery, but personally I prefer Prototypejs and scriptalicious.

Basically my question is, I see loads of tutorials on submitting forms via ajax to a designated and absolute file path such as "/process.php", but i've never seen the concept of ajax forms being submitted with the php file itself, I can see why not but i'm wondering if its possible. An example would be:


<form action="" method="post" id="blah">
Fields here
</form>
<?php
/*Have the ajax submission process this code instead of an external file*/
?>


I ask this, because it would seem like a good thing to know as it would make life easier, I understand the problems which can come from this such as if you're using a templating system it would just load the whole page into the specified response location.

But my question is, is it possible WITHOUT loading the whole webpage into the div..

I pretty much know the answer but I'm not fluent in javascript so it would be nice if this theory would be confirmed from some of you javascript guru's out there ^_^
----

PS: If this IS possible, in one framework but not the other, I don't mind just let me know which :)

Jazz914
05-12-2010, 10:25 AM
Bumpy.

No-one has any clue if this is or not possible? :O

VIPStephan
05-12-2010, 10:47 AM
I’m not sure about AJAX submissions but you can load part of a page into an element with jQuery’s load() (http://api.jquery.com/load/) function (see section “Loading page fragments”). So you can have a complete page as basic functionality if JS isn’t avaliable and if it is then you only load a part of the other page into the current one.

Not sure if that’s what you mean?

Jazz914
05-12-2010, 12:00 PM
I'll try explaining a different way :)

Okay, I have a HTML form which is being validated by PHP
<div id="response"></div><form action="/validate.php" method="post" id="blah">
Fields here
</form>

This form would post the data to a php file called "/validate.php", but I generally don't do this what I do is keep all the error checking in the same file like so:

<div id="response"></div><form action="" method="post" id="blah">
Fields here
</form>
<?php
if (isset($_POST['submit'])) {
// Validate the forms data
}
?>

Now with the first method what i'd do with javascript is include in the code the file path known as "validate.php", which would be fine and returning it in a div named "response"

But with the second method I am posting the data to the file itself which means if I were to include the path to the page itself it would load the entire website into the div as well as the response, so the #response is almost like an iframe, because posting the file to itself JavaScript is being receiving the post data and the website, and that's what its returning.

------
another example

If the codingforums form was posting like I am explaining here we'll take the preview feature before posting a message, instead of previewing JUST the post you'd see "http://codingforums.com/newreply.php?do=postreply&t=195715" in the preview box (The full website)

I am sorry if i'm not explaining myself good enough, the concept is fairly tricky to understand D:

VIPStephan
05-12-2010, 12:28 PM
No, I see what you’re saying. But wouldn’t a same-file validation just require an update of the current data? Like you’re filling out a form, posting the submission to a script on the same page via AJAX, and depending on the outcome update the respective information (i. e. replace one text string with the new one) on the page?

Jazz914
05-12-2010, 01:24 PM
Yeah, but it includes the whole page of the site, so because I am using a template system and everything is included via the index page.

So its posting to a new page and returning the iframe like div (With the data submitted) inside the response div.

I am not really a javascript person, so I don't know what the possibilities of the language is and I wouldn't know how to approach / Search for something like this because if I search for "AJax Form Submissions" they all return results stating things like create an external php file.. I have never found one which lets you use the same php file whilst using a navigation system like this..