Jump to content

Why after AJAX post, the URL redirect to a URL with the POST params like GET params in this addon?


malin

Recommended Posts

In the [addonmodule Controller.php](https://github.com/WHMCS/sample-addon-module/blob/master/modules/addons/addonmodule/lib/Admin/Controller.php):

I append this `order_detail` function:


```
    /**
     * @param $vars
     */
    public function order_detail($vars){

        $modulelink = $vars['modulelink']; // eg. addonmodules.php?module=addonmodule
        $version = $vars['version']; // eg. 1.0
        $LANG = $vars['_lang']; // an array of the currently loaded language variables

        $orderid = 28;//isset($_REQUEST['orderid']) ? $_REQUEST['orderid'] : '';

        $sql = "SELECT tblhosting.id, tblhosting.regdate, tblhosting.domain, tblhosting.billingcycle, tblhosting.nextduedate, tblhosting.nextinvoicedate, tblhosting.termination_date, tblhosting.domainstatus, tblhosting.username, tblhosting.notes, tblhosting.dedicatedip, tblhosting.assignedips FROM tblhosting ";
//        $sql .= "LEFT JOIN tblorders ON ";
//        $sql .= "tblhosting.orderid = tblorders.id ";
        $sql .= ("WHERE tblhosting.orderid = " . $orderid);

        $tblhostings = Capsule::select($sql);

        $table_tbody = "";
        for($i = 0; $i < count($tblhostings); $i ++) {

            $item = $tblhostings[$i];

            $row = "<tr>";

            $row .= ("<td><input type='text' id='tblhosting-id' name='id' class='form-control' readonly=\"readonly\" style='width:80px;' value='" . $item->id . "'></td>");
            $row .= ("<td><textarea id='tblhosting-domain' name='domain' class='form-control' style='width:320px;'>" . $item->domain . "</textarea></td>");
            //$row .= ("<td>" . $item->billingcycle . "</td>");
            $row .= ("<td ><div style='width:80px;'>" . $item->nextduedate . "</div></td>");
            $row .= ("<td ><div style='width:80px;'>" . $item->nextinvoicedate . "</div></td>");
            //$row .= ("<td>" . $item->termination_date . "</td>");
            //$row .= ("<td>" . $item->username . "</td>");

            $row .= ("<td><textarea id='tblhosting-dedicatedip' name='dedicatedip' class='form-control' style='width:200px;'>" . $item->dedicatedip . "</textarea></td>");
            $row .= ("<td><textarea id='tblhosting-assignedips' name='assignedips' class='form-control' style='width:200px;'>" . $item->assignedips .  "</textarea></td>");
            $row .= ("<td>" . $item->domainstatus . "</td>");
            $row .= ("<td><div style='width:80px;'>" . $item->regdate . "</div></td>");
            $row .= ("<td><textarea id='tblhosting-notes' type='textarea' name='notes' class='form-control' style='width:200px;'>" . $item->notes .  "</textarea></td>");

            $row .= ("<td> <button id='modify' type='submit' class='btn btn-warning'>modify</button>  <button id='send-email' type='button' class='btn btn-success'>Send email</button>  </td>");

            $row .= "</tr>";

            $table_tbody .= $row;

        }


        $div = "<div class='container'><div><h2>order ID: {$orderid} :</h2></div>";

        $div .= "<div >";

        $div .= "<form '><table class='table'>";

        $div .= "<thead><tr>";

        $div .= "<th>product ID</th>";
        $div .= "<th>product name</th>";
        //$div .= "<th>billingtype</th>";
        $div .= "<th>nextdue</th>";
        $div .= "<th>nextvoice</th>";
        //$div .= "<th>terminate time</th>";
        //$div .= "<th>username</th>";

        $div .= "<th>dedicated IP</th>";
        $div .= "<th>assigned IP</th>";
        $div .= "<th>status</th>";
        $div .= "<th>regdate</th>";
        $div .= "<th>note</th>";
        $div .= "<th>ope</th>";

        $div .= "</tr></thead>";

        $div .= "<tbody>";
        $div .= $table_tbody;
        $div .= "</tbody>";

        $div .= "</table>";

        $div .= "</div></div>";

        $js = "<script>";
        $js .= "

            var origin_url = window.location.href;//\"http://192.168.33.10/whmcs-nlidc.com/whmcs/admin/addonmodules.php?module=addonmodule&action=order_detail&orderid=100\";
            var url_array =origin_url.split(\"/admin/\")
            var url = url_array[0] + \"/admin/api/admin_apis.php\";

            jQuery(document).ready(function(){
                $('#modify').click(function(){
                    //alert('modify');

                    var id = $('#tblhosting-id').val();
                    var domain = $('#tblhosting-domain').val();
                    var dedicatedip = $('#tblhosting-dedicatedip').val();
                    var assignedips = $('#tblhosting-assignedips').val();
                    var notes = $('#tblhosting-notes').val();

                    var params = {
                        admin_api:'update_tblhosting_support',
                        id: id,
                        domain: domain,
                        dedicatedip: dedicatedip,
                        assignedips: assignedips,
                        notes: notes,

                    }

                    $.post(url, params,function(data,status){
                        //alert(\"Data: \" + data + \"nStatus: \" + status);

                        var data_obj = JSON.parse(data);

                        if (status == 'url' && data_obj.status == 200) {
                            alert('update success');

                            alert(window.location.href(www.baidu.com);)
                        }else {
                            //alert('update fail');
                        }
                    });

                })
            });

            jQuery(document).ready(function(){
                $('#send-email').click(function(){
                    alert('send email');
                })
            });

        ";
        $js .= "</script>";


        $html = $div . $js;
        return <<<EOF
            $html
EOF;


    }
}

```

[![enter image description here][1]][1]

I want to use this way to update the `tblhosting` entries.  You see my jQuery code want to use `post` method to update the `tblhosting` entry. 

 when I click update button, it will execute the update url, but however it will redirect to this URL 

```
http://192.168.33.10/whmcs/admin/addonmodules.php?id=28&domain=10.93.240.193-194(15M%29+HK-Z7-A15++E5-2650%282CPU%29%2F32G%2F500G+ssd%2F++&dedicatedip=10.93.240.193-194&assignedips=10.93.240.193%0D%0A10.93.240.194&notes=02442%2F66655%0D%0A10&intellisearch=1&token=ac271aed87528e68969608329e8a1f4d2a35c858&value=
```

[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/eJRM7.png
  [2]: https://i.stack.imgur.com/vEw9w.png


---

As we know the ajax will not refresh a whole page, and will not redirect by default after request, why there goes different? 

Who know the way to avoid the redirect?  

---

I also tried use 
```
window.location.href(origin_url);
```
to redirect to back after the wrong redirect, but it will not execute. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated