Problems, need help? Have a tip or advice? Post it here.
21 posts Page 2 of 3
How do i get the value back to the ajax success?
I tried <cms:gpc> and json, etc.
But no value returned.
Image
where innovation meets technology
Please advice!
Image
where innovation meets technology
The way AJAX works, suppose script A calls up script B via AJAX sending it some data.
The script B, after doing whatever it is supposed to do, is then expected to *send back* whatever data is required by script A to continue.

It could either be a simple code signifying success/failure or could be a complex JS object containing arbitrary data.
In your case what script A requires back is the ID of the page newly created by script B.

So you should make script B send back that ID (or error if page could not be created).
A simple implementation could be as follows -
Code: Select all
<cms:db_persist
    ..
>
    <cms:if k_error >
        <cms:abort>ERROR</cms:abort>
    <cms:else_if k_success />
        <cms:abort><cms:show k_last_insert_id /></cms:abort>
    </cms:if>
</cms:db_persist >

The <cms:abort> tag helps in discarding any previous output and sending back only the enclosed content, in this case either a string 'ERROR' or the ID of the created page. If your needs are more complex, please see @cheesypoof's implemntation where he sends back complex data as a JS object - viewtopic.php?f=2&t=10415

That completes the script B's part of the deal.
Now the script A can read the returned data and act accordingly.

In your case, the calling JS code can use the returned data as follows -
Code: Select all
$.ajax({
    ..
    ..
    success: function(response){
        if(response != 'ERROR'){
            window.location.href = "generate-sos.php?q="+response;
        }
    }
});

Hope this helps.
@KK Sir,

As always your finishing touches to the code are bang on helpful. Thanks a lot sir for your inputs. Yes the code you have shared works perfect.

Also I will like to mention that this post by you helps me to not only understand but also implement AJAX with a lot of ease.

please see @cheesypoof's implemntation where he sends back complex data as a JS object - viewtopic.php?f=2&t=10415

Sir i referred this above mentioned topic. I was curious to know that is it possible to receive data in json from a mobile app if so what should be my approach theoretically? Please can you provide me with a theoretical walk through please.

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
@KK Sir,
And @All Couch Developers

A small issue.

I am able to duplicate the complete template and as per the code discussed. The only thing not duplicating is the image. Since I am using the Data Bound Forms, I have secure_file region for image.

How can I copy the image?

My Ajax has the image as:
Code: Select all
$(document).on('click','#generatesos',function(e) {
                  $.ajax({
                     url:"<cms:show k_site_link />fir-sos-json.php",
                     type:"POST",
                     data:{
                        <cms:pages id=rt_id limit='1' >
                        ...
                        "ipt_disaster_image":<cms:escape_json><cms:show_securefile 'ipt_disaster_image' ><cms:securefile_link file_id /></cms:show_securefile></cms:escape_json>,
                        ...
                        </cms:pages>
                     },
                     contentType:"application/json; charset=utf-8",
                     success: function(data){
                        alert("Data Save");
                     }
                  });
               });


and in the db_persist of the fir-sos-json.php file i have the image as:
Code: Select all
<cms:set disaster_image="<cms:gpc 'ipt_disaster_image' method='get' />" scope="global" />
<cms:db_persist
    ...
    ipt_disaster_image  =   "<cms:show disaster_image />"
    ...
>


The image does not save. Please can you point me to the right direction?
I saw your post at: viewtopic.php?f=8&t=11545#p30808 but did not understand anything. It has to do something with the image being copied to a new page from an existing page. But could not understand how to implement it in my code, if it can be in the first place.

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
@genxcoders, it is a brickwall in front of you - hard to pass through. I suggest to data-stream (base64) the image (https://en.wikipedia.org/wiki/Data_URI_scheme) and save it into a textarea via db_persist. You can configure list-view, form-view and frontend to display that data as image.
@trendoman (Bro Anton)

As suggested by you
I suggest to data-stream (base64) the image (https://en.wikipedia.org/wiki/Data_URI_scheme) and save it into a textarea via db_persist.


I have data-streamed the image using the base64.

The error issue now is that:
1. If i keep the ajax request as GET: I get the error Request-URI Too Long The requested URL's length exceeds the capacity limit for this server. But without the image base64 string, data submits and gets saved.
2. If i keep the ajax request as POST: I get the error
<br><b>Name:</b> Required field cannot be left empty


I am converting the image into base64 string as:
Code: Select all
<cms:set b64_img="<cms:show_securefile 'ipt_disaster_image' ><cms:securefile_link file_id /></cms:show_securefile>" />
               <cms:php>
                  global $b64_img;
                  global $b64_data;
                  $b64_img = file_get_contents('<cms:show b64_img />');
                  $b64_data = base64_encode($b64_img);
               </cms:php>


Now there are two things:
1. In type:"POST" => I always get error for required field.
2. Is it possible to make the base64 image encoded string small to fit the URI or is there something else that i need to do to generate the encoded string smaller.

I understand that the image size matters. My images will be between 1MB to 3MB. This encoding will be helpful since the data being submitted is through a mobile app, where base64 string will be employed to send the image.

Regards,
GenXCoders (Aashish)

Edit #1: I referred the topic of db_persist and saw that the k_page_name is not used. I tried the same but it make no difference. With POSt type ajax i still get the same error for Name.
Image
where innovation meets technology
Is it possible to make the base64 image encoded string small

Of course, there is another (more complicated) way - pass on solely the accessible URL of the image, then on the recipient side download it with PHP (curl) and you are free to do with it whatever needed.
@trendoman (Bro Anton)

Can you please elaborate?
Of course, there is another (more complicated) way - pass on solely the accessible URL of the image, then on the recipient side download it with PHP (curl) and you are free to do with it whatever needed.


I tried a similar approach (without curl). Instead of the base64 string I sent the location of the file from the generating template (note in web only this is possible, as i have tested it there). On the receiving template, I have saved the value in a textarea and then <cms:show ... /> in the img src attribute.

But the generating template has a securefile region using which i can delete the image (generating template template). But at the receiving template i cannot delete it. Nor can i do the same with the base 64 string. Front end is fine. But is there any way the the image file can be shown in the securefile region type in the backend too?

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
@trendoman

I will give you the flow of the process to be achieved.

1. First Information Report (FIR) can be generated from the web and mobile app. The FIR consists of Title, Photo, Description, etc.

2. All FIR are received at the web end. The received FIR are then authenticated manually (human touch point). If the FIR is found genuine it is converted into a SOS (Save Our Souls) Distress signal.

3. All data from FIR (template) is copied into the SOS (template).

4. SOS once generated, presents the user with all details coming from FIR and some additional options. The additional options are Department Lists and Individual Persons List. The user (admin) can then select department/individual and send them a push notification (firebase) to the Mobile Application, which in turn sounds an alarm in the application.

All data to be communicated to the mobile app and from the mobile app is to be done using JSON. Hence I require the JSON solution at almost every place.

Hope i have been able to explain the flow to you.

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
21 posts Page 2 of 3