Problems, need help? Have a tip or advice? Post it here.
21 posts Page 1 of 3
Hello.

@KK Sir
@trendoman
@AllCouchDevelopers

I wanted to know if is possible to store data (data already exists) from one template to another template using db_persist and ajax.

The editable regions in both the templates are same in count and by their names.

I am trying to do the following:
1. Data is fed into a template (temp-one.php)
2. temp-one.php has a page view (generated using custom routes) which contains the data and a button.
3. by clicking the button an AJAX call needs to be generated.
4. The ajax call should initiate saving the data from temp-one.php (as normal data or json) and save it to temp-two.php using db_persist tag.
5. If successfully the data is submitted to temp-two.php then the page should be redirected to temp-two.php.

All ajax operations are to be performed on temp-one.php.

Would this be possible?

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
Hi,

I don't see any reason why it wouldn't - your approach is perfectly valid.
You may use the first template for carrying out the copying (using <cms:is_ajax> to figure out if the template is being invoked through AJAX) or may use a different template for the purpose.
@KK Sir,

using <cms:is_ajax> to figure out if the template is being invoked through AJAX)

Sir I have almost no knowledge of AJAX, hence the confusion.

I have coded the following:

The ajax code:
Code: Select all
                                            $(document).on('click','#generatesos',function(e) {
                  $.ajax({
                     url:"<cms:show k_site_link />fir-sos-json.php",
                     type:"POST",
                     dataType:"text",
                     data:{
                        <cms:pages id=rt_id limit='1' >
                        k_page_title:"<cms:show k_page_title />",
                         k_page_name:"<cms:show k_page_name />",
                         ipt_train_no:"<cms:show ipt_train_no />",
                         ipt_desc:"<cms:show ipt_desc />"
                        </cms:pages>
                     },
                     contentType:"application/json; charset=utf-8",
                     success: function(data){
                        alert("Data Save");
                     }
                  });
               });


And the code for fir-sos.php:
Code: Select all
<?php require_once('../couch/cms.php'); ?>
<cms:db_persist
    _auto_title         =   '0'
    _invalidate_cache   =   '0'
    _masterpage         =   'generate-sos.php'
    _mode               =   'create'
       
    k_page_title        =   "<cms:show k_page_title />"
    k_page_name         =   "<cms:show k_page_name />"

    ipt_train_no        =   "<cms:show ipt_train_no />"   
    ipt_desc            =   "<cms:show ipt_desc />"
>
    <cms:if k_error >
        <cms:each k_error >
            <cms:show item /><br>
        </cms:each>
    <cms:else_if k_success />
        <cms:redirect url="generate-sos.php" />
    </cms:if>
</cms:db_persist >
<?php COUCH::invoke(); ?>


Nothing happens. I know that the there is some major mistake in my code. But due to lack of knowledge I am unable to proceed any further.

Please advise. Any help with the code would be highly appriciated.

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
:roll:
Image
where innovation meets technology
To begin with, always escape data while outputting JSON - please see viewtopic.php?f=4&t=12093#p33251

Coming to you main problem -
Your AJAX code is calling up 'fir-sos-json.php' template posting your data to it.
In your 'fir-sos-json.php' template you need to first recover that data.

Use <cms:gpc /> with 'post' param (https://docs.couchcms.com/tags-reference/gpc.html) to get hold of the supplied data.
Once you have it, use it to populate the <cms:db_persist /> params.

Hope this helps.
@KK Sir,

To begin with, always escape data while outputting JSON - please see viewtopic.php?f=4&t=12093#p33251

I have made the changes 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' >
                        "k_page_title":<cms:escape_json><cms:show k_page_title /></cms:escape_json>,
                         "k_page_name":<cms:escape_json><cms:show k_page_name /></cms:escape_json>,
                         "ipt_train_no":<cms:escape_json><cms:show ipt_train_no /></cms:escape_json>,
                         "ipt_desc":<cms:escape_json><cms:nl2br><cms:show ipt_desc /></cms:nl2br></cms:escape_json>
                        </cms:pages>
                     },
                     contentType:"application/json; charset=utf-8",
                     success: function(data){
                        alert("Data Save");
                     }
                  });
               });


Your AJAX code is calling up 'fir-sos-json.php' template posting your data to it.
In your 'fir-sos-json.php' template you need to first recover that data.

Use <cms:gpc /> with 'post' param (https://docs.couchcms.com/tags-reference/gpc.html) to get hold of the supplied data.
Once you have it, use it to populate the <cms:db_persist /> params.

And my fir-sos-json.php
Code: Select all
<?php require_once('../couch/cms.php'); ?>
<cms:set title="<cms:gpc 'k_page_title' method='post' />" scope="global" />
<cms:set name="<cms:gpc 'k_page_name' method='post' />" scope="global" />
<cms:set train_no="<cms:gpc 'ipt_train_no' method='post' />" scope="global" />
<cms:set desc="<cms:gpc 'ipt_desc' method='post' />" scope="global" />
<cms:db_persist
    _auto_title         =   '0'
    _invalidate_cache   =   '0'
    _masterpage         =   'generate-sos.php'
    _mode               =   'create'
       
    k_page_title        =   "<cms:show title />"
    k_page_name         =   "<cms:show name />"

    ipt_train_no        =   "<cms:show train_no />"   
    ipt_desc            =   "<cms:show desc />"
>
    <cms:if k_error >
        <cms:each k_error >
            <cms:show item /><br>
        </cms:each>
    <cms:else_if k_success />
        <cms:redirect url="generate-sos.php" />
    </cms:if>
</cms:db_persist >
<?php COUCH::invoke(); ?>


Still the data is not submitting. Am i still going wrong somewhere?

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
@KK sir.

My Generate SOS Button (the button intended to call the ajax code) is defined as:
Code: Select all
<!-- Custom Route: view_fir.html -->
<cms:pages masterpage='generate-fir.php'>
    ...
</cms:pages>

    <form>
   <button class="btn btn-primary" type="submit" id="generatesos">
       Generate SOS
   </button>
    </form>


Is this the reason of issue that the data is not saving? I have made the changes suggested by you but still not able to achieve the functionality.

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
Aashish, we use various debugging tools to avoid helplessness in such situations.
First, in browser open network console and filter by XHR. Submit the form and check if browser passes values to the next page.
Second, on the recepient page check the content of POST data by outputting in php variable $_POST.
@BroAnton

Thanks for the headsup.
First, in browser open network console and filter by XHR. Submit the form and check if browser passes values to the next page.
Second, on the recepient page check the content of POST data by outputting in php variable $_POST.


the issue was that there was an error in the Name field as i had set it to <cms:random_ name /> instead of <cms:random_name />

Just one thing, how can i send the page to the recently generated page in generate-sos.php?

My code right now is:
Code: Select all
$(document).on('click','#generatesos',function(e) {
                   e.preventDefault();
                  $.ajax({
                     url:"fir-sos-json.php",
                     type:"GET",
                     data:{
                        <cms:pages id=rt_id limit='1' >
                        "k_page_title":<cms:escape_json><cms:show k_page_title /></cms:escape_json>,
                         "k_page_name":<cms:escape_json><cms:show k_page_name /></cms:escape_json>,
                         "ipt_train_no":<cms:escape_json><cms:show ipt_train_no /></cms:escape_json>,
                         "ipt_desc":<cms:escape_json><cms:nl2br><cms:show ipt_desc /></cms:nl2br></cms:escape_json>
                        </cms:pages>
                     },
                     contentType:"application/json; charset=utf-8",
                     success: function(response){
                        alert("FIR converted to SOS successfully.");
                        window.location.href = "generate-sos.php?q=<cms:show k_last_insert_id />";
                     }
                  });
               });


but the redirection
window.location.href = "generate-sos.php?q=<cms:show k_last_insert_id />";
does not work.

this k_last_insert_id is coming from fir-sos-json.php. But i am unable to get it redirected to generate-sos.php?param=value
Any heads up on that?

Regards,
GenXCoders
Aashish
Image
where innovation meets technology
You have an unused variable response, which can be filled with k_last_insert_id
success: function(response){
21 posts Page 1 of 3