【问题标题】:Updating cropped image data in mySQLi with jQuery, & Ajax使用 jQuery 和 Ajax 在 mySQL 中更新裁剪的图像数据
【发布时间】:2018-04-30 19:34:56
【问题描述】:

我有一个表单(以模态形式出现),您可以在其中上传图像裁剪并调整其大小。有两个按钮 Cancel 和 Save & Crop。当点击保存和裁剪时,我在functions.js 中有以下 jQuery:

/* handle functionality when click crop button  */
jQuery('#save_crop').on('click', function(e){
    e.preventDefault();
    params = {
        targetUrl: 'change_pic.php?action=save',
        action: 'save',
        x_axis: jQuery('#hdn-x1-axis').val(),
        y_axis: jQuery('#hdn-y1-axis').val(),
        x2_axis: jQuery('#hdn-x2-axis').val(),
        y2_axis: jQuery('#hdn-y2-axis').val(),
        thumb_width: jQuery('#hdn-thumb-width').val(),
        thumb_height:jQuery('#hdn-thumb-height').val()
    };
    saveCropImage(params);
});


/* Function to save crop images */
function saveCropImage(params) {
    jQuery.ajax({
        url: params['targetUrl'],
        cache: false,
        dataType: "html",
        data: {
            action: params['action'],
            id: jQuery('#hdn-profile-id').val(),
            t: 'ajax',
            w1: params['thumb_width'],
            x1: params['x_axis'],
            h1: params['thumb_height'],
            y1: params['y_axis'],
            x2: params['x2_axis'],
            y2: params['y2_axis'],
            image_name: jQuery('#image_name').val()
        },
        type: 'Post',
        success: function (response) {
            jQuery('#change_pic_modal').modal('hide');
            jQuery(".imgareaselect-border1,.imgareaselect-border2,.imgareaselect-border3,.imgareaselect-border4,.imgareaselect-border2,.imgareaselect-outer").css('display', 'none');
            jQuery("#profile_picture").attr('src', response);
            jQuery("#preview-profile-pic").html('');
            jQuery("#profile-pic").val();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('status Code:' + xhr.status + 'Error Message :' + thrownError);
        }
    });
}

});

在change_pic.php我有以下内容:

首先是一个switch语句:

/* Get post details */
$post = isset($_POST) ? $_POST: array();
switch($post['action']) {
    case 'save' :
        saveProfilePic();
        saveProfilePicTmp();
        break;
    default:
        changeProfilePic();
}

有了以上内容,我现在有了case 'save':,这将激活saveProfilePic() 和saveProfilePicTmp()。后者工作正常,图像以经过清理的名称保存到正确的文件夹中。另一个saveProfilePic() 什么都不做,我不明白为什么?这是函数:

    /* Function to handle save profile pic */
function saveProfilePic(){
    include_once("config/config.php");
    $post = isset($_POST) ? $_POST: array();    
    //Handle profile picture update with MySQL update Query using $options array    
    if($post['username-id']){
        $sql_query = "SELECT * FROM users WHERE username = '".mysqli_escape_string($con, $post['username-id'])."'";     
        $resultset = mysqli_query($con, $sql_query) or die("database error:". mysqli_error($con));      
        if(mysqli_num_rows($resultset)) {                     
            $sql_update = "UPDATE users set profile_pic='".mysqli_escape_string($con,$post['image_name'])."' WHERE username = '".mysqli_escape_string($con, $post['username-id'])."'";          
            mysqli_query($con, $sql_update) or die("database error:". mysqli_error($con));
        }
    }
}

($post['username-id']) 作为隐藏值来自表单,我需要将其与特定用户联系起来。谁能明白为什么这行不通?基本上我的表单上发生的事情是模态保持打开状态并且裁剪的图像区域消失了。模式应该关闭,但保持打开状态我假设 b/c saveProfilePic(); 没有执行。

任何帮助将不胜感激。

【问题讨论】:

    标签: php jquery ajax mysqli


    【解决方案1】:

    你说:

    ($post['username-id']) 来自表单作为隐藏值

    但是您不会在任何地方将其发送到您的 ajax 中(好吧,无论如何都没有标记为 username-id)。您的 data 对象需要包含它,或者您必须在会话中包含它:

    data: {
        // Presumably  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv is the id?
        'username-id': jQuery('#hdn-profile-id').val(),
        action: params['action'],
        // If the above is a correct assumption, then you don't need this (or
        // just revise this key name from "id" to "username-id" like
        // I demonstrated)
        id: jQuery('#hdn-profile-id').val(),
        t: 'ajax',
        w1:params['thumb_width'],
        x1:params['x_axis'],
        h1:params['thumb_height'],
        y1:params['y_axis'],
        x2:params['x2_axis'],
        y2:params['y2_axis'],
        image_name :jQuery('#image_name').val()
    },
    

    如果你不想更改上面的data,那么将PHP更改为寻找id而不是username-id:

    function saveProfilePic()
    {
        # I would not do this, I would put the connection at the top of the page and
        # inject the $con into the function saveProfilePic($con)
        include_once("config/config.php");
        # I would check for !empty(), not isset() because it could be set, just empty
        $post = (!empty($_POST))? $_POST : array();    
        # Check for just id here not username-id
        if(!empty($post['id'])) {
            # You should be binding parameters, forget using the escaping stuff...
            $username_id = mysqli_escape_string($con, $post['id']);
            $sql_query = "SELECT * FROM users WHERE username = '".$username_id."'";     
            $resultset = mysqli_query($con, $sql_query) or die("database error:". mysqli_error($con));
            if(mysqli_num_rows($resultset)) {
                # Again, bind parameters here, don't escape anymore
                $sql_update = "UPDATE users set profile_pic='".mysqli_escape_string($con,$post['image_name'])."' WHERE username = '".$username_id."'";          
                mysqli_query($con, $sql_update) or die("database error:". mysqli_error($con));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      相关资源
      最近更新 更多