Re: submitting forms with ajax
| From: | samantha_o | Date: | Mon, 19 Nov 2007 09:21:52 +0000 |
| Subject: | Re: submitting forms with ajax | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-264767@lists.php.net to get a copy of this message | ||
Thanks a lot. Really appreciate it. I will try to apply it.
quickshiftin wrote:
>
> json is just a terse way to create objects anonymously as well as other
> data
> structures in javascript.
> http://json.org/
>
> to build a json response with html
> essentially all you need to do is create a javascript array in php and put
> the html
> you want for the next page in that. then you use the eval() function in
> javascript
> to 'incarnate' the javascript array (which contains the html). once youve
> done that
> you can put the html into the dom wherever you like. typically you will
> replace some
> existing dom content w/ the new html.
>
> so as an example; lets say on the php side of things you have your html;
> for
> the sake
> of brevity lets assume its in a variable called *$html*. you will need to
> encode this for
> transmission (so the javascript interpreter doesnt blow up when it tries
> to
> 'incarnate' the
> string as literal javascript code). if youre using php5 this is as simple
> as
>
> $jsonResponse = json_encode(utf8_encode('var newHtml = [' . $html . ']'));
>
> notice the braces on either side of the html; here were creating a
> javascript array containing
> our html string. if you have multiple html segments youd like to stitch
> into different parts of the
> dom, simply delimit them with commas, such as
>
> $jsonResponse = json_encode(utf8_encode('var newHtml = [' . $html . ', ' .
> $html1 . ', ' . $html2 . ']'));
> and so on.
>
> then you will send that to the browser, where your ajax handler will
> intercept it.
> echo $jsonReponse;
>
> on the javascript side all you need to do is run this output from the php
> through the javascript interpreter.
> jquery will hand you back the results packed up somehow, you will have to
> read up on how it will hand this
> to you (its an XMLHttpRequest object under the hood); but lets say its in
> a
> variable, *responseText*.
> the javascript to incarnate your string is:
>
> eval('('+responseText+')');
>
> now all the strings of html you put in the array on the server side are
> available to your javascript as indexes of
> the *newHtml* array.
>
> so lets say you have a section of the dom in a div tag, with id, *
> dynamicContentSection*; using traditional dhtml
> techniques you would do something like this to get your new html in place
>
> document.getElementById('dynamicContentSection').innerHtml = newHtml[0];
>
> thats about it. there are plenty of places to make mistakes though :)
> and there alternatives to the methods ive suggested, of course.
>
> take a look a firebug (firefox plugin) and jslint (javascript syntax
> checker)
> theyre great javascript debugging tools.
>
> -nathan
>
>
> On Nov 18, 2007 8:25 PM, samantha_o <ooi_lching@yahoo.com.sg> wrote:
>
>>
>> I did not try using json because i am very new in this field and dont
>> know
>> much about it. Can you give me some examples about it? Thanks for
>> helping.
>>
>>
>> Shiplu wrote:
>> >
>> > Why dont you use json as server response? Then manipulate it by own
>> > javascript. You can use jquery $.getJSON function to do the ajax part
>> > for you.
>> >
>> > On 11/16/07, Shiplu <shiplu.net@gmail.com> wrote:
>> >> Why dont you use json as server response? Then manipulate it by own
>> >> javascript. You can use jquery $.getJSON function to do the ajax part
>> >> for you.
>> >>
>> >> On 11/16/07, samantha_o <ooi_lching@yahoo.com.sg> wrote:
>> >> >
>> >> > Hi,
>> >> >
>> >> > i would like to submit forms with ajax, using jquery and then load
>> the
>> >> next
>> >> > page. I had successfully do it with jquery and form plugin. however,
>> it
>> >> does
>> >> > not work whenever the server response consist of both HTMLs and
>> >> javascript
>> >> > together. Is it possible to make it works? I am new in all this. Can
>> >> anyone
>> >> > help me?
>> >> > --
>> >> > View this message in context:
>> >> >
>> >>
>>
>> http://www.nabble.com/submitting-forms-with-ajax-tf4819530.html#a13788322
>> >> > Sent from the PHP - General mailing list archive at Nabble.com.
>> >> >
>> >> > --
>> >> > PHP General Mailing List (http://www.php.net/)
>> >> > To unsubscribe, visit: È"auÍ€¦:ô=
>> >> > vGxhttp://www.php.net/unsub.php
>> >> >
>> >> >
>> >>
>> >
>> > --
>> > PHP General Mailing List (http://www.php.net/)
>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> >
>> >
>>
>> --
>> View this message in context:
>>
>> http://www.nabble.com/submitting-forms-with-ajax-tf4819530.html#a13826338
>> Sent from the PHP - General mailing list archive at Nabble.com.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>
--
View this message in context: http://www.nabble.com/submitting-forms-with-ajax-tf4819530.html#a13830261
Sent from the PHP - General mailing list archive at Nabble.com.