duanjia8215 2014-01-29 15:34
浏览 65
已采纳

使用fancybox的Ajax表单中的问题

I'm having a lot of issues in my code (beginner level). I want to send to a form some data from another form, that second form will receive three variables, two defined by inputs, and one will be referred to the link.

This is the code I use:

    <script>
    $(document).ready(function () {
      $('.thumb').on("click", function (e) {
        e.preventDefault();
        var nome = 'thumb'; <-(here, i wanna change the value at clicking in other link eg.: 'thum', or 'thumb2, or 'thumb3')
        var fabricante = $('#busca').val(); <-(here goes the data from a dropdown menu)
        var sku = $('#sku').val(); <- (here the data from a input field (nothing special) 
        $.ajax({
          type: "GET",
          cache: false,
          url: this.href,
          data: { n:nome, f:fabricante, s:sku } <- i think my problem is here)
          success: function (data) {
            $.fancybox(data, {
              fitToView: false,
              width: 905,
              height: 505,
              autoSize: false,
              closeClick: false,
              openEffect: 'none',
              closeEffect: 'none'
            }); // fancybox
          } // success
        }); // ajax
      }); // on
    }); // ready

    </script>

The page that will receive the data:

<?
 $nome      = $_GET['n']; 
 $fabricante= $_GET['f'];
 $sku       = $_GET['s'];
?>
<!DOCTYPE html>
<html lang="pt-br" >
    <head>
        <meta charset="utf-8" />

        <link href="css/main.css" rel="stylesheet" type="text/css" />
        <link href="css/jquery.Jcrop.min.css" rel="stylesheet" type="text/css" />

        <!-- add scripts -->
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.Jcrop.min.js"></script>
        <script src="js/script.js"></script>
    </head>
    <body>
    <div class="demo">
            <div class="bbody">

                <!-- upload form -->
                <form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkForm()">
                    <!-- hidden crop params -->
                    <? echo $fabricante . $nome . $sku; ?>
                    <input type="hidden" id="x1" name="x1" />
                    <input type="hidden" id="y1" name="y1" />
                    <input type="hidden" id="x2" name="x2" />
                    <input type="hidden" id="y2" name="y2" />
                    <input type="hidden" id="path" name="fabricante" value=<? echo $fabricante ?> />
                    <input type="hidden" id="path" name="sku" value=<? echo $sku ?> />
                    <input type="hidden" id="path" name="<? echo $nome ?>" value=<? echo $nome ?> />

                    <h2>Escolha a imagem</h2>
                    <div><input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" /></div>

                    <div class="error"></div>

                    <div class="step2">
                        <h2>Faça o recorte da imagem</h2>
                        <img id="preview" />

                        <div class="info">
                            <label>File size</label> <input type="text" id="filesize" name="filesize" />
                            <label>Type</label> <input type="text" id="filetype" name="filetype" />
                            <label>Image dimension</label> <input type="text" id="filedim" name="filedim" />
                            <label>W</label> <input type="text" id="w" name="w" />
                            <label>H</label> <input type="text" id="h" name="h" />
                        </div>

                        <input type="submit" value="Enviar" />
                    </div>
                </form>
            </div>
        </div>
    </body>
</html>

The php that deals with the data from the form ahead

<?php
session_start();
$_SESSION[$c1]=0;
$_SESSION[$c2]=0;
$_SESSION[$c3]=0;
/**
 *
 * HTML5 Image uploader with Jcrop
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Copyright 2012, Script Tutorials
 * http://www.script-tutorials.com/
 */
 $sku = $_POST['sku'];
 $fabricante=$_POST['fabricante'];
  $mode = 0777;
  if(!is_dir($_SERVER["DOCUMENT_ROOT"] . '/img/' . $fabricante)){
    mkdir($_SERVER["DOCUMENT_ROOT"] . '/img/' . $fabricante, $mode, true);
  }
  $sku = $_POST ["sku"];
  if(!is_dir($_SERVER["DOCUMENT_ROOT"] . '/img/' . $fabricante . '/' . $sku)){
    mkdir($_SERVER["DOCUMENT_ROOT"] . '/img/' . $fabricante . '/' . $sku, $mode, true);
  }
  $fold = $_SERVER["DOCUMENT_ROOT"] . '/img/' . $fabricante;
  chmod($fold, 0777);
  $pasta    = $_SERVER["DOCUMENT_ROOT"] . '/img/' . $fabricante . "/" . $sku . "/";
  chmod($pasta, 0777);
function uploadImageFile() { // Note: GD library is required for this function

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if ($cont = $_POST["thumb"]){
        $iWidth =420; 
        $iHeight = 336; // desired image result dimensions
        $iJpgQuality = 90;}
        if ($cont = $_POST["gde1"]){
        $iWidth =700; 
        $iHeight = 500; // desired image result dimensions
        $iJpgQuality = 90;} 
        if ($cont = $_POST["gde2"]){
        $iWidth =700; 
        $iHeight = 500; // desired image result dimensions
        $iJpgQuality = 90;}

        if ($_FILES) {

            // if no errors and size less than 250kb
            if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
                if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {

                    // new unique filename
                    $sTempFileName2= md5(time().rand());
                    $sTempFileName = $pasta . $sTempFileName2;

                    // move uploaded file into cache folder
                    move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);

                    // change file permission to 644
                    @chmod($sTempFileName, 0644);

                    if (file_exists($sTempFileName) && filesize($sTempFileName) > 0) {
                        $aSize = getimagesize($sTempFileName); // try to obtain image info
                        if (!$aSize) {
                            @unlink($sTempFileName);
                            return;
                        }

                        // check for image type
                        switch($aSize[2]) {
                            case IMAGETYPE_JPEG:
                                $sExt = '.jpg';

                                // create a new image from file 
                                $vImg = @imagecreatefromjpeg($sTempFileName);
                                break;
                            /*case IMAGETYPE_GIF:
                                $sExt = '.gif';

                                // create a new image from file 
                                $vImg = @imagecreatefromgif($sTempFileName);
                                break;*/
                            case IMAGETYPE_PNG:
                                $sExt = '.png';

                                // create a new image from file 
                                $vImg = @imagecreatefrompng($sTempFileName);
                                break;
                            default:
                                @unlink($sTempFileName);
                                return;
                        }

                        // create a new true color image
                        $vDstImg = imagecreatetruecolor( $iWidth, $iHeight );
                        $white = imagecolorallocate($vDstImg, 255, 255, 255);
                        imagefill($vDstImg, 0, 0, $white);

                        // copy and resize part of an image with resampling
                        imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);

                        // define a result image filename
                        $sResultFileName = $sTempFileName . $sExt;
                        $_SESSION[$foto]=$sTempFileName2 . $sExt;

                        // output image to file
                        imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
                        @unlink($sTempFileName);

                        return $sResultFileName;
                    }
                }
            }
        }
    }
}

$sImage = uploadImageFile();

if ($cont = $_POST["thumb"]){   

I think that here I want to send the answer to a <img href=''> in the first page (that will be three, each one will receive a different var).

$_SESSION[$thumb] = $sImage;
$_SESSION[$c1]=$_SESSION[$c1]+1;
}
if ($cont = $_POST["gde1"]){
$_SESSION[$gde1] = $sImage;
$_SESSION[$c2]=$_SESSION[$c2]+1;
}
if ($cont = $_POST["gde2"]){
$_SESSION[$gde2] = $sImage;
$_SESSION[$c1]=$_SESSION[$c1]+1;
}
echo  "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";;

Any help is really appreciated.

  • 写回答

1条回答 默认 最新

  • douyi1966 2014-01-29 17:29
    关注

    Below is the correct code, you were missing a comma.

    <script>
    $(document).ready(function () {
      $('.thumb').on("click", function (e) {
        e.preventDefault();
        var nome = 'thumb'; 
        var fabricante = $('#busca').val(); 
        var sku = $('#sku').val();
        $.ajax({
          type: "GET",
          cache: false,
          url: this.href,
          data: { n:nome, f:fabricante, s:sku }, //Added missing comma here
          success: function (data) {
            $.fancybox(data, {
              fitToView: false,
              width: 905,
              height: 505,
              autoSize: false,
              closeClick: false,
              openEffect: 'none',
              closeEffect: 'none'
            }); // fancybox
          } // success
        }); // ajax
      }); // on
    }); // ready
    
    </script>
    

    Also this line has an extra semicolon:

    echo "</script>";;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services