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);
                       }
                    }

                });
            }
        });
    });
15
  • What is base_url in your url??? Where you define it???
    – Saty
    Jul 16, 2015 at 6:37
  • base_url is localhost as I am currently testing it in localhost
    – Adi
    Jul 16, 2015 at 6: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, 2015 at 6:44
  • @Adi:Its depends on your query and number of records retrieving. Jul 16, 2015 at 6:47
  • nothing shows up in network while the browser starts freezing
    – Adi
    Jul 16, 2015 at 6:49

2 Answers 2

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!

9
  • I have defined base_url in a script tag at the start of the program
    – Adi
    Jul 16, 2015 at 7:18
  • <script type="text/javascript"> var base_url="<?php echo base_url();?>"; var type="list"; var fileName=""; var fileExt=""; </script>
    – Adi
    Jul 16, 2015 at 7: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? Jul 16, 2015 at 7: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, 2015 at 7: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. Jul 16, 2015 at 7:42
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.

17
  • history_verified_phone:id is not auto incremental
    – Adi
    Jul 16, 2015 at 10:34
  • I checked the previous query in MySQL and it was working .I checked for a particular id say id=10 and updated and inserted accordingly. Since I needed only to display updated phone and updated time so I just saved the query 3rd in a variable.
    – Adi
    Jul 16, 2015 at 10:40
  • OK, so you can use below query instead Query 1: $instVerified = "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 ."'"; Let me know what is it finally done OR any error? Jul 16, 2015 at 10:41
  • Not sure what that quotes and dots around variables do eg:'". $textNum ."' as they are giving errors
    – Adi
    Jul 16, 2015 at 10:51
  • Still the browser freezes.
    – Adi
    Jul 16, 2015 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.