CodeIgniter Forums
Trouble while Integrating Query in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Trouble while Integrating Query in CI (/thread-9553.html)

Pages: 1 2


Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Mizanur Islam Laskar[/eluser]
Hi,

This is my first posting on a very peculiar trouble while while integrating jQuery in CodeIgniter. I'm trying to develope an AJAX registration form and I'm getting the following problems:

1) I'm not getting confirmation for any field when I go to the next field pressing TAB
2) Backend .php files for AJAX works are not functoning properly, hence getting wrong data.

But I don't face the above problems when I develope from scratch. Can anybody give me a help plz? I'm sure, if its solved, then it should be an asset for many beginers CI developers. I'm stating my files as below:

Portion of config.php:
Code:
$config['base_url']    = "http://localhost/projects/inbook";

//Define CSS Files
$config['inner_css'] = "assets/styles/inner_page.css";
$config['milk'] = "assets/styles/milk.css";
$config['chili'] = "assets/styles/chili.css";

//Define JavaScript Files
$config['jquery'] = "assets/JavaScripts/jquery.js";
$config['jquery_ajaxQueue'] = "assets/JavaScripts/jquery.ajaxQueue.js";
$config['jquery_delegate'] = "assets/JavaScripts/jquery.delegate.js";
$config['jquery_metadata'] = "assets/JavaScripts/jquery.metadata.js";
$config['jquery_validate'] = "assets/JavaScripts/jquery.validate.js";

//Define PHP Files
$config['captcha'] = "assets/PHPforAjax/captcha.php";
$config['check_captcha'] = "assets/PHPforAjax/check_captcha.php";
$config['check_email'] = "assets/PHPforAjax/check_email.php";


For the above CSS and JavaScript files, please browse http://jquery.bassistance.de/validate/demo/milk/ in Fireowrks and download those files using JS view extension.

captcha.php
Code:
<?
//include("config.inc");  
session_start();  
$width = 100;  
$height = 50;  
$im = imagecreate($width, $height);  
$bg = imagecolorallocate($im, 174, 214, 91);  

// generate random string  
$len = 5;  
$chars = '0123456789';  
$string = '';  
for ($i = 0; $i < $len; $i++) {  
   $pos = rand(0, strlen($chars)-1);  
   $string .= $chars{$pos};  
}  
$_SESSION['captcha_code'] = md5($string);  

// grid  
$grid_color = imagecolorallocate($im, 253, 236, 117);  
$number_to_loop = ceil($width / 20);  
for($i = 0; $i < $number_to_loop; $i++) {  
   $x = ($i + 1) * 20;  
   imageline($im, $x, 0, $x, $height, $grid_color);  
}  
$number_to_loop = ceil($height / 10);  
for($i = 0; $i < $number_to_loop; $i++) {  
   $y = ($i + 1) * 10;  
   imageline($im, 0, $y, $width, $y, $grid_color);  
}  

// random lines  
$line_color = imagecolorallocate($im, 254, 185, 80);  
for($i = 0; $i < 30; $i++) {  
   $rand_x_1 = rand(0, $width - 1);  
   $rand_x_2 = rand(0, $width - 1);  
   $rand_y_1 = rand(0, $height - 1);  
   $rand_y_2 = rand(0, $height - 1);  
   imageline($im, $rand_x_1, $rand_y_1, $rand_x_2, $rand_y_2, $line_color);  
}  

// write the text  
$text_color = imagecolorallocate($im, 110, 110, 206);  
$rand_x = rand(0, $width - 50);  
$rand_y = rand(0, $height - 15);  
imagestring($im, 10, $rand_x, $rand_y, $string, $text_color);  

header ("Content-type: image/png");  
imagepng($im);  
?&gt;

check_captcha.php
Code:
&lt;?

session_start();

if(isset($_GET['captcha']) && isset($HTTP_SESSION_VARS['captcha_code']))
    {
     if(md5($_GET['captcha']) == $HTTP_SESSION_VARS['captcha_code'])
        {
         $valid = 'true';
         }
    else
        {
         $valid = 'false';
         }
    }
else
    {
     if(!isset($_GET['captcha']))
        {
         $valid = 'false';
         }
     if(!isset($HTTP_SESSION_VARS['captcha_code']))
        {
         $valid = 'false';
         }
     }
    
echo $valid;    
        
?&gt;

check_email.php

Code:
&lt;?

    include("config.php");
    
    $email = trim(strtolower($_REQUEST['email'];
    $r_chk = mysql_query("SELECT user_email FROM user_profile WHERE user_email='$email'");
    if(mysql_num_rows($r_chk) > 0)
        {
         $valid = 'false';
         }
    else
        {
         $valid = 'true';
         }
        
    echo $valid;

?&gt;

Please see my View and Controller files in my next posting


Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Mizanur Islam Laskar[/eluser]
CONTROLLER (SignUp.php):

Code:
&lt;?php

    class SignUp extends Controller
        {
        
          function SignUp()
              {
             parent::Controller();
             }
            
          function index()
              {
             $data=array();
             $data['title']=':: Sign Up INBOOK.COM ::';
             $data['metaTag1'] = "&lt;meta name='keywords' c&gt;";
             $data['metaTag2'] = "&lt;meta name='description' c&gt;";
            
             $data['base'] = $this->config->item('base_url');
            
             $data['inner_css'] = $this->config->item('inner_css');
             $data['milk'] = $this->config->item('milk');
             $data['chili'] = $this->config->item('chili');
            
             $data['jquery'] = $this->config->item('jquery');
             $data['jquery_ajaxQueue'] = $this->config->item('jquery_ajaxQueue');
             $data['jquery_delegate'] = $this->config->item('jquery_delegate');
             $data['jquery_metadata'] = $this->config->item('jquery_metadata');
             $data['jquery_validate'] = $this->config->item('jquery_validate');
            
            
             $data['captcha'] = $this->config->item('captcha');
             $data['check_captcha'] = $this->config->item('check_captcha');
             $data['check_email'] = $this->config->item('check_email');
            
             $this->load->model('inbook_model');
             $data['main_menu'] = $this->inbook_model->show_main_menu();
             $data['sub_menu'] = $this->inbook_model->show_sub_menu();
            
             $this->load->view('SignUp',$data);
             }          
        
         }

?&gt;



Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Mizanur Islam Laskar[/eluser]
VIEW File (SignUp.php):
Code:
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" c&gt;
&lt;?php echo $metaTag1; ?&gt;
&lt;?php echo $metaTag2; ?&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;

&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo "$base/$inner_css";?&gt;"&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo "$base/$milk";?&gt;"&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo "$base/$chili";?&gt;"&gt;

[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]

[removed]
$(document).ready(function() {
    var validator = $("#signupform").validate({
        rules: {
            email: { required: true, email: true, remote: "&lt;?php echo "$base/$check_email";?&gt;" },
            password: { required: true, minlength: 6 },
            password_confirm: { required: true, minlength: 6, equalTo: "#password" },
            captcha: { required: true, minLength: 5, remote: "&lt;?php echo "$base/$check_captcha";?&gt;" }
        },
        messages: {
            email: { required: "Please enter a valid Email Address", minLength: "Please enter a valid Email Address", remote: jQuery.format("{0} is already in use") },
            password: { required: "Provide a password", rangelength: jQuery.format("Enter at least {0} characters") },
            password_confirm: { required: "Repeat your password", minLength: jQuery.format("Enter at least {0} characters"), equalTo: "Enter the same Password as above"
            },
            captcha: { required: "Please type the Verification Code", minLength: jQuery.format("Please type the {0}-digit Verification Code"), remote: jQuery.format("Please type the Verification Code")
            }
        },
        errorPlacement: function(error, element) {
            if ( element.is(":radio") ) error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") ) error.appendTo ( element.next() );
            else error.appendTo( element.parent().next() );
        },
        
        submitHandler: function() { document.getElementById('signupform').submit(); },
        success: function(label) { label.html("&nbsp;").addClass("checked"); }
    });
    
});
[removed]

&lt;/head&gt;
&lt;body&gt;
&lt;form id="signupform" name="signupform" autocomplete="off" method="post" acti&gt;
                
    <table border="0" cellspacing="3" cellpadding="3">
                      
        <tr>
            <td class="label" valign="middle"><label id="lemail" for="email">Email Address</label></td>
            <td class="field" valign="middle">
            &lt;input class="input_txt" id="email" name="email" type="text" value="" size="30" maxlength="80" /&gt;
            <br /><font style="font-size:12px;">[ this will not be shared ]</font>
            </td>
            <td class="status" valign="middle"></td>
        </tr>
        
        <tr>
            <td class="label" valign="middle"><label id="lpassword" for="password">Password</label></td>
            <td class="field" valign="middle">&lt;input class="input_txt" id="password" name="password" type="password" value="" size="30" maxlength="50" /&gt;&lt;/td>
            <td class="status" valign="middle"></td>
        </tr>
        
        <tr>
            <td class="label" valign="middle"><label id="lpassword_confirm" for="password_confirm">Confirm Password</label></td>
            <td class="field" valign="middle">&lt;input class="input_txt" id="password_confirm" name="password_confirm" type="password" value="" size="30" maxlength="50" /&gt;&lt;/td>
            <td class="status" valign="middle"></td>
        </tr>
                      
         <tr>
            <td class="label" valign="middle"><label id="lcaptcha" for="captcha">Type what you see</label></td>
            <td class="field" valign="middle">
            <img >&nbsp;
            &lt;input class="input_txt" style="float:right;" id="captcha" name="captcha" value="" type="text" size="8" maxlength="8" /&gt;
            </td>
            <td class="status" valign="middle"></td>
         </tr>
                      
        <tr>
            <td class="label" valign="middle"><label id="lsignupsubmit" for="signupsubmit">Signup</label></td>
            <td class="field" colspan="2">
            &lt;input type="hidden" name="go" value="yes"&gt;&lt;input type="submit" name="btn" value="Continue">
            </td>
        </tr>
                      
    </table>
&lt;/form&gt;
        
&lt;/body&gt;
&lt;/html&gt;



Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Pascal Kriete[/eluser]
You'll need to run your script tags through postable or something similar so we can see them. Also, let's reduce this a bit. All you need is a very simple controller and view, that way you'll get a much better response. Reading through, and interpreting all that code takes time.

Does this work?
Controller:
Code:
$this->load->helper('url');

$this->load->view('test');

View:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript" src="&lt;?=base_url()?&gt;assets/JavaScripts/jquery.js" charset="utf-8"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
    if(jQuery) {
        alert('jquery loaded');
    }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>Test</h1>
&lt;/body&gt;
&lt;/html&gt;



Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Mizanur Islam Laskar[/eluser]
First of all, THANK YOU VERY MUCH INPARO, foryour quickest response. I applied URL Helper as your suggestions, but I'm in the same place. Let me make it more clear of what I'm suffering:

1) In the registration form, while I press TAB button without typing anything, I don't see any confirmation (like any positive/negative comments upon my typing).

2) If I type an email address that is not in MySQL, then it keeps saying that particular email is already in use. Also in the captcha field, even if I type the correct digits, it says that "Please type the Verification Code" .

My modified Controller(SignUp.php):
Code:
&lt;?php

    class SignUp extends Controller
        {
        
          function SignUp()
              {
             parent::Controller();
             $this->load->helper('url');
             }
            
          function index()
              {
             $data=array();
             $data['title']=':: Sign Up INBOOK.COM ::';
             $data['metaTag1'] = "&lt;meta name='keywords' c&gt;";
             $data['metaTag2'] = "&lt;meta name='description' c&gt;";            
             $this->load->model('inbook_model');
             $data['main_menu'] = $this->inbook_model->show_main_menu();
             $data['sub_menu'] = $this->inbook_model->show_sub_menu();
             $this->load->view('SignUp',$data);
             }          
        
         }

?&gt;

A portion of my modified View(SignUp.php):
Code:
.......
&lt;link rel="stylesheet" type="text/css" href="&lt;?=base_url()?&gt;assets/styles/inner_page.css"&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?=base_url()?&gt;assets/styles/milk.css"&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?=base_url()?&gt;assets/styles/chili.css"&gt;

[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]

[removed]
    if(jQuery)
    {
        $(document).ready(function() {
        // validate signup form on keyup and submit
        var validator = $("#signupform").validate({
            rules: {
                email: {
                    required: true,
                    email: true,
                    remote: "&lt;?=base_url()?&gt;assets/PHPforAjax/check_email.php"
                },
                password: {
                    required: true,
                    minlength: 6
                },
                password_confirm: {
                    required: true,
                    minlength: 6,
                    equalTo: "#password"
                },
                captcha: {
                    required: true,
                    minLength: 5,
                    remote: "&lt;?=base_url()?&gt;assets/PHPforAjax/check_captcha.php"
                }
            },
            messages: {
                email: {
                    required: "Please enter a valid Email Address",
                    minLength: "Please enter a valid Email Address",
                    remote: jQuery.format("{0} is already in use")
                },
                password: {
                    required: "Provide a password",
                    rangelength: jQuery.format("Enter at least {0} characters")
                },
                password_confirm: {
                    required: "Repeat your password",
                    minLength: jQuery.format("Enter at least {0} characters"),
                    equalTo: "Enter the same Password as above"
                },
                captcha: {
                    required: "Please type the Verification Code",
                    minLength: jQuery.format("Please type the {0}-digit Verification Code"),
                    remote: jQuery.format("Please type the Verification Code")
                }
            },
            // the errorPlacement has to take the table layout into account
            errorPlacement: function(error, element) {
                if ( element.is(":radio") )
                    error.appendTo( element.parent().next().next() );
                else if ( element.is(":checkbox") )
                    error.appendTo ( element.next() );
                else
                    error.appendTo( element.parent().next() );
            },
            
            submitHandler: function() {
                //alert("submitted!");
                document.getElementById('signupform').submit();
            },
            // set this class to error-labels to indicate valid fields
            success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").addClass("checked");
            }
        });
        
    });
    }

[removed]
......



Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Pascal Kriete[/eluser]
Ah ok, I understand the issue now.

The tabbing doesn't do anything because the validation is triggered onChange not onBlur.

Your check_email code has a syntax error on it (you're not closing the trim) so that's why that fails. CI has a captcha plugin (system/plugins) that you can use instead of writing your own from scratch. Otherwise I would suggest you move your own to a helper and use that. I fail to understand why you have separate scripts for the remote functions. Just make them controller functions and call those. That way you have all the power of CI and don't need to reinvent the wheel.


Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Mizanur Islam Laskar[/eluser]
Well, don't have any idea where onChange or onBlur is triggered in my view page. Can you please give me a solution with an example of how can I get rid of this?...I'm really getting fustrated. Plz go to http://www.simplypost.ca/register_step1.php and use <b>[email protected]</b> or <b>[email protected]</b> and also use both correct and incorrect disits in captcha, then play with TAB...thats the thing I want, which was done from scratch, other than using any framework.

Besides, according to your last suggestions, I created the following MVC, but stuck in the same pitfall:

Model File(check_model.php):
Code:
&lt;?php

    class Check_model extends Model
        {
        
         function Check_model()
             {
             parent::Model();
             }
            
         function check_email($email)
             {
             $valid = 0;
             $this->load->database();
             $query=$this->db->getwhere('user_profile',array('user_email'=>$email));
             $valid += $query->num_rows();
             return $valid;
             }              
        
         }

?&gt;

Portion of my Controller(SignUp.php):
Code:
&lt;?php

    class SignUp extends Controller
        {
        
          function SignUp()
              {
             parent::Controller();
             $this->load->helper('url');
             }
                  ......
            
          function confirm_email( )
              {
             $email = $_GET['email'];
             $this->load->model('check_model');
             $valid_email = $this->check_model->check_email($email);
             return $valid_email;
             }              
        
         }

?&gt;

Portion of my View File(SignUp.php):
Code:
......
email: { required: true, email: true, remote: "http://localhost/projects/inbook/index.php/SignUp/confirm_email" },
......



Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Pascal Kriete[/eluser]
If I enter something and hit tab it works just fine. If your users are prone to not filling out forms ... well what can I say.

To get the desired effect you would need to look into validate.js and what triggers the validation - then add your own.


Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Mizanur Islam Laskar[/eluser]
Actually, I don't want to modify any JS file there. Since if it works fine without ny interruption in http://www.simplypost.ca/register_step1.php from scratch-level of coding, then I don't understand why it should not in CI.

Anyway, this problem is making me stopped from any further step of my current 2 projects. Thats why I placed my sufferings in this quick forum with all neccesary codes/reffrences. So I'll realy appreciate if anybody can give me any solution to the point..Thanks.


Trouble while Integrating Query in CI - El Forum - 06-30-2008

[eluser]Pascal Kriete[/eluser]
Just had another look at your last code snippet.
Code:
function confirm_email( )
{
    $email = $_GET['email'];
    $this->load->model('check_model');
    $valid_email = $this->check_model->check_email($email);
    return $valid_email;
}

The only way to get data to an ajax call is to output it. Returning it will stop your script and get you nowhere.