1

I have a function in view used to transfer a value from a text box to a table displayed on a page. Basically it updates the URL and goes into a function called update_verified_phone(). There is another function which is used to update the records using a model named user_info_model() and uses controller named users_info().

Problem is when when I use an AJAX function to post to the controller function named update_verified_phone(), the browser freezes and hangs up. I was wondering why it is happening?

Sorry just new to AJAX.

Here is the code :

$(document).ready(function() 
    {
        $('#btnVerify').click(function(event)
        {
            event.preventDefault();
            var uid = $('#user_id').val();
            var verified_phone = $('#textNum').val();
            if (isNaN(verified_phone)) 
            {
               alert("Please enter only numbers for user verified_phone");
            }
            else
            {  
                $('#textNum').val('');
                //$.post(base_url+'users_info/update_verified_phone', {uid:user_id,verified_phone:textNum}, function(response)
                //{
                $.ajax
                ({
                    type: "POST",
                    url:base_url+'users_info/update_verified_phone',
                    data: {uid:user_id,verified_phone:textNum},
                    //async: true,
                    dataType: 'json',
                    success:function(data)
                    {
                        if(data)
                       {   

                           var headingHtml = headingTemplate
                               ({
                                   'verified_phone':data[0].verified_phone,
                                   'verified_timestamp':data[0].verified_time
                               });
                               $('.userinfo').html(headingHtml);
                               $('.userinfo tr td:eq(4)').html(data[0].verified_phone);
                               $('.userinfo tr td:eq(5)').html(data[0].verified_time);
                       }
                    }

                });
            }
        });
    });
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Adi
  • 43
  • 10
  • What is `base_url` in your url??? Where you define it??? – Saty Jul 16 '15 at 06:37
  • base_url is localhost as I am currently testing it in localhost – Adi Jul 16 '15 at 06:39
  • does it freeze during the request or after ? What do you see in your network tab in the dev tools of your browser ? – Jeroen Jul 16 '15 at 06:44
  • @Adi:Its depends on your query and number of records retrieving. – Chandresh M Jul 16 '15 at 06:47
  • nothing shows up in network while the browser starts freezing – Adi Jul 16 '15 at 06:49
  • Do you get any message in the developer console? – Jerodev Jul 16 '15 at 06:49
  • my query updates two records verified_phone and verified_time (current timestamp) – Adi Jul 16 '15 at 06:50
  • no message in developer console , It shows nothing but shows a message on browser chrome ran out of memory – Adi Jul 16 '15 at 06:52
  • query also retrieves the new verified phone updated record to the page. – Adi Jul 16 '15 at 06:53
  • Try changing `event` to any other variable I suspect it is a keyword – Umair Ayub Jul 16 '15 at 06:54
  • here is the query in model function update_verified_phone($user_id,$textNum) { $this->db->query("INSERT INTO history_verified_phone(id,lastverified_phone,lastverified_time,verified_phone,verified_time) SELECT $user_id,users.verified_phone,users.verified_time,$textNum,NOW() FROM users WHERE id = $user_id"); $this->db->query("UPDATE users SET verified_phone = $textNum , verified_time = NOW() WHERE id = $user_id"); $sql= $this->db->query("SELECT verified_phone,verified_time from users WHERE id = $user_id"); return $sql->result(); } – Adi Jul 16 '15 at 06:54
  • textNum is the if of the textbox from where I get the new phone number to update and change and user_id (primary key) I get from a textbox for a record which phone number needs updation – Adi Jul 16 '15 at 06:58
  • tired updating the event , still browser hangs – Adi Jul 16 '15 at 07:10
  • Have you tried adding a `error` method into your `ajax` call? Maybe it's failing? – jasonscript Jul 16 '15 at 07:19
  • nope , how to add that? – Adi Jul 16 '15 at 07:28

2 Answers2

0

I think you missed to specify JS variable base_url correct it out. After that once you check your AJAX request URL response by direct hit from browser address bar and check what if issue is there OR not.

Let me know if you require any more information regarding this.

Thanks!

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • I have defined base_url in a script tag at the start of the program – Adi Jul 16 '15 at 07:18
  • – Adi Jul 16 '15 at 07:18
  • Ok and have check that response of your request by passing it into new tab..? Paste your URL(Example : www.DOMAIN.com/users_info/update_verified_phone/YOURPARAMTER') and check this page take response quickly or not? – AddWeb Solution Pvt Ltd Jul 16 '15 at 07:25
  • gave Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'users.verified_phone,users.verified_time,,NOW() FROM users WHERE id =' at line 2 INSERT INTO history_verified_phone(id,lastverified_phone,lastverified_time,verified_phone,verified_time) SELECT ,users.verified_phone,users.verified_time,,NOW() FROM users WHERE id = Filename: C:\Users\fab\Desktop\Tptt\server\UniServerZ\www\system\database\DB_driver.php Line Number: 330 – Adi Jul 16 '15 at 07:30
  • This error message may seem cryptic at first. That is because it is a general MySQL error pointing to a syntax error of some sort in the SQL Query statement. So could you please share your query for review? we need check it first there is some silly error so we need to correct it first. – AddWeb Solution Pvt Ltd Jul 16 '15 at 07:42
  • Please share your MySql query which already set/wrote. This issue error is regarding that query and need to correct it. – AddWeb Solution Pvt Ltd Jul 16 '15 at 08:00
  • INSERT INTO history_verified_phone(id,lastverified_phone,lastverified_time,verified_phone,verified_time) SELECT $user_id,users.verified_phone,users.verified_time,$textNum,NOW() FROM users WHERE id = $user_id UPDATE users SET verified_phone = $textNum , verified_time = NOW() WHERE id = $user_id" SELECT verified_phone,verified_time from users WHERE id = $user_id – Adi Jul 16 '15 at 09:01
  • these are the 3 queries , I am returning the result of last one to the web page – Adi Jul 16 '15 at 09:02
  • Please check I have added solution as new answer...Thanks – AddWeb Solution Pvt Ltd Jul 16 '15 at 10:15
0

This is pretty difficult but I try to solve it.

Query 1: Insert query with SELECT statement

//If history_verified_phone:id is auto incremental then no need to cover it into INSERT statement so removed.
$instVerified = "INSERT INTO history_verified_phone(lastverified_phone,lastverified_time,verified_phone,verified_time) 
SELECT users.verified_phone,users.verified_time,'". $textNum ."',NOW() FROM users WHERE id = '". $user_id ."'";

Query 2: Update query

$updtUser = "UPDATE users SET verified_phone = '" . $textNum . "', verified_time = NOW() WHERE id = '" . $user_id . "'";

Query 3: Select query

$selUser = "SELECT verified_phone,verified_time from users WHERE id = '" . $user_id . "'";

Please replace variable with your query, $instVerified for Query 1, $updtUser for Query 2 and $selUser for Query 3 and after that please check what happen. Because I have some doubt with your given query may failed and that is the reason for issue.

Please let me know what is outcomes here.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57