Thursday, April 12, 2012

Uploadify open browse dialog when click on a custom picture

I am using Uploadify to upload pictures.



I am having a picture, and I want to Uploadify open the browse files dialog after clicking on the image, how can I achieve this ?



Here is my code



$("#btnAddImg").click(function() {
Uploading();
});

function Uploading() {

var a = $("input#Id").val();

alert("uploading" + a);

$("#fileuploader").uploadify({
'uploader': '/Scripts/uploadify/uploadify.swf',
'cancelImg': '/Content/Images/icones/cancel.png',
//'buttonText': 'Select Image',
//'buttonImg': '/Content/Images/icones/picture-add-icon.png',
'script': 'Home/Upload',
'folder': a,
'fileDesc': 'Image Files',
'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
'multi': true,
'auto': true
});
}


Ps: I don' t want to use the 'buttonImg': integrate property, as I want uploadify to load only after clicking my custom image ( as behind the scene I want to get the dynamic folder path and pass to uploadify)





HTTPClient::ConnectTimeoutError while using 'fb_graph' gem

I have one rails project and two machinges. One is running Ubuntu 10.04 and the other 12.04 beta. I'm using the 'fb_graph' gem on the 10.04 machine all works fine, but when I do on the 12.04 machine things like





me = FbGraph::User.me(token).fetch





I get



HTTPClient::ConnectTimeoutError: execution expired
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient/session.rb:300:in `connect'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient/session.rb:300:in `ssl_connect'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient/session.rb:739:in `block in connect'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient/session.rb:731:in `connect'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient/session.rb:594:in `query'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient/session.rb:161:in `query'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient.rb:1060:in `do_get_block'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient.rb:869:in `block in do_request'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient.rb:956:in `protect_keep_alive_disconnected'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient.rb:868:in `do_request'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient.rb:756:in `request'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/httpclient-2.2.4/lib/httpclient.rb:661:in `get'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/fb_graph-2.4.10/lib/fb_graph/node.rb:49:in `block in get'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/fb_graph-2.4.10/lib/fb_graph/node.rb:122:in `handle_response'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/fb_graph-2.4.10/lib/fb_graph/node.rb:48:in `get'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/fb_graph-2.4.10/lib/fb_graph/node.rb:18:in `fetch'
from (irb):10
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start'
from /home/jan/.rvm/gems/ruby-1.9.2-p290@ece/gems/railties-3.2.0/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'


I have no idea why this happen nor how I could solve it. Any ideas? Thanks!



Jan





ajax call to login.php , response failed

I am doing login.php with the ajax call..Below is my ajax bit code and login.php code..while giving valid user name and password also..am getting invalid user name or password message..am totally failed understand these lines of code


$is_ajax = $_REQUEST['is_ajax'];
     if(isset($is_ajax) && $is_ajax) // in login.php, Many thanks
 
 $("#login").click(function() {    
     var action = $("#form1").attr('action');
     var form_data = {
         username: $("#username").val(),
         password: $("#password").val(),
         is_ajax: 1
     };
 
     $.ajax({
         type: "POST",
         url: login.php,
         data: form_data,
         success: function(response)
         {
             if(response == 'success')
                 $("#form1").slideUp('slow', function() {
                     $("#message").html("<p class='success'>You have logged in successfully!</p>");
                 });
             else
                 $("#message").html("<p class='error'>Invalid username and/or password.</p>");    
         }
     });
 
     return false;
 });
 


and this is the login.php code.


<?PHP
 if(isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax']) {
     $is_ajax = $_REQUEST['is_ajax'];
 
     $uname = $_POST['username'];
     $pword = $_POST['password'];
     $uname = htmlspecialchars($uname);
     $pword = htmlspecialchars($pword);
 
     $user_name = "root";
     $pass_word = "root";
     $database = "test";
     $server = "127.0.0.1";
 
     $username = $_REQUEST['username'];
     $password = $_REQUEST['password'];
 
     $db_handle = mysql_connect($server, $user_name, $pass_word);
     $db_found = mysql_select_db($database, $db_handle);
     $uname = quote_smart($uname, $db_handle);
     $pword = quote_smart($pword, $db_handle);
     $SQL = "SELECT * FROM login WHERE L1 =  $uname AND L2 = md5($pword)";
     $result = mysql_query($SQL);
     $num_rows = mysql_num_rows($result);
     if ($num_rows > 0) {
         echo "success"; 
     }
 
     mysql_close($db_handle);
 }
 ?>
 


Answer:


A few things:
  1. You need to sanitize your user input before using it in a query. htmlspecialchars doesn't do it. Use mysql_real_escape_string (maybe quote_smart does this?).
  2. Your URL is not quoted, so it is interpreted as code. Put login.php in quotes.
  3. Make sure you understand what $_POST and $_REQUEST are. It looks like you don't actually use$username or $password anywhere, so it doesn't look like this is hurting you right now, but you should know that the entire contents of the $_POST array are also found in the $_REQUEST array (along with $_GET and $_COOKIE).
  4. For debugging, in your PHP script, you should echo some kind of failure message to see where the script fails and in your jQuery script, you should add the response to your failure message to see what the response actually was.
Try this:
$("#login").click(function() {    
    var action = $("#form1").attr('action');
    var form_data = {
        username: $("#username").val(),
        password: $("#password").val(),
        is_ajax: 1
    };

    $.ajax({
        type: "POST",
        url: "login.php",
        data: form_data,
        success: function(response)
        {
            if(response == 'success')
                $("#form1").slideUp('slow', function() {
                    $("#message").html("<p class='success'>You have logged in successfully!</p>");
                });
            else
                $("#message").html("<p class='error'>Invalid username and/or password. (debugging, response = '". response ."'</p>");    
        }
    });

    return false;
});
login.php:
if(isset($_POST['is_ajax']) && $_POST['is_ajax'])
{
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $dbUsername = "root";
    $dbPassword = "root";
    $database = "test";
    $server = "127.0.0.1";

    $db_handle = mysql_connect($server, $dbUsername, $dbPassword);
    $db_found = mysql_select_db($database, $db_handle);
    $result = mysql_query("SELECT * FROM login WHERE L1 = '{$username}' AND L2 = MD5('{$password}')");
    $num_rows = mysql_num_rows($result);
    if ($num_rows > 0)
        echo "success";
    else
        echo "username '{$username}' and password '{$password}' not found";

    mysql_close($db_handle);
}
else
    echo "is_ajax = {$_POST["is_ajax"]}";