【问题标题】:jQuery, Ajax, PHP FunctionjQuery、Ajax、PHP 函数
【发布时间】:2011-12-07 14:01:58
【问题描述】:

希望这是发布此内容的正确位置,并且有人可以提供帮助。

我有一个简单的应用程序,它使用 PHP GD 库以设置的字体显示文本 以固定大小。

http://www.ttmt.org.uk/

当它工作时,您可以选择文本和大小并按设置,它将显示在灰色框中。

在没有 Ajax 的情况下执行此操作时,我只是使用这个 php 来回显 img 标记,并将 php 函数作为 src。

    echo '<img src="imageftt.php?text='.$myText.'&size='.$mySize.'&font='.$theFont.'">';

imageftt.php - PHP,这是创建图像的 GD 库函数。

    <?php

    header('Content-Type: image/png');

    $im = imagecreatetruecolor(1000, 200);

    $gray = imagecolorallocate($im, 240, 240, 240);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 1000, 199, $gray);

    $theText = $_POST['text'];
    $theSize = $_POST['size'];
    $theFont = $_POST['font'];

    imagefttext($im, $theSize, 0, 15, 160, $black, $theFont, $theText);     

    imagepng($im);

    imagedestroy($im);

    ?>

=

我的问题是我想使用 jQuery 和 Ajax 来执行此操作,因此页面不必重新加载。

我有这个从表单中收集值的 jQuery

    <script type="text/javascript" >
      $(function(){
        $('.button').click(function(){
          var text = $('#text').val();
          var size = $('#size').val();
          var font = $('corbelb.ttf');
          //alert(text);
          //alert(size);
          //
          var dataString = 'text=' + text + 'size=' + size + 'font=' + font;
          $.ajax({
            type: 'POST',
            url: 'imageftt.php',
            data: dataString,
            success: function(){
              alert()

            }
          });
          return false;
        });
      });
    </script>

我的问题是我看不到如何将我的 php 函数与 jQuery 函数一起使用。

希望这是有道理的 - 任何帮助

HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <title>untitled</title>
        <link rel="stylesheet" href="style.css" type="text/css" />

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
      <script src="../js/jquery.easing.1.3.js" type="text/javascript"></script>

      <script type="text/javascript" >
        $(function(){
          $('.button').click(function(){
            var text = $('#text').val();
            var size = $('#size').val();
            var font = $('corbelb.ttf');
            //alert(text);
            //alert(size);
            //
            var dataString = 'text=' + text + 'size=' + size + 'font=' + font;
            $.ajax({
              type: 'POST',
              url: 'imageftt.php',
              data: dataString,
              success: function(){
                alert()

              }
            });
            return false;
          });
        });
      </script>

    </head>

    <body>
      <div id="wrap">
        <form action="" method="">
          <select name="text" id="text">
            <option value="<?php echo $_POST['text'];?>">Text</option>
            <option value="ABCDEFGHJIKLMNOPQRSTUVWXYZ">ABCDEFGHIJKLMNOPQRSTUVWXYZ</option>
            <option value="abcdefghijklmnopqrstuvwxyz">abcdefghijklmnopqrstuvwxyz</option>
            <option value="0123456789">0123456789</option>
          </select>
          <select name="size" id="size">
            <option value="<?php echo $_POST['size'];?>">Size</option>
            <option value="72">72</option>
            <option value="84">84</option>
            <option value="96">96</option>
            <option value="108">108</option>
          </select>  
          <input type="submit" name="submit" class="button" value="Set &rarr;" />
        </form>

        <div id="top">

            <?php 
              $theFont="corbelb.ttf";

            if(!empty($_POST['submit'])){
                $myText = $_POST['text'];
              $mySize = $_POST['size'];
              echo '<img src="imageftt.php?text='.$myText.'&size='.$mySize.'&font='.$theFont.'">';
            }

            ?>

        </div>

      </div>
    </body>
    </html>

* 更新 *

在帮助下,我对此有了进一步的了解,我正在研究类似此处的建议,但它仍然无法正常工作

使用下面的代码,所有值都得到了正确的警告,但没有显示文本。生成了一个灰色框,这让我认为它正在调用 PHP 并返回结果,但值“var dataString = 'text=' + text + 'size=' + size + 'font=' + font;” php没有发送或接收。

http://www.ttmt.org.uk/1/

<script type="text/javascript" >
    $(function(){
      $('.button').click(function(e){
       e.preventDefault();
        var text = $('#text').val();
        var size = $('#size').val();
        var font = 'corbelb.ttf';
        //alert(text);
        //alert(size);
        //alert(font);
        //
        var dataString = 'text=' + text + 'size=' + size + 'font=' + font;
        $("#top").find("img").remove();
        $("#top").append("<img src='imageftt.php?"+dataString+"'/>");

      });
    });
  </script>

【问题讨论】:

  • 你真的不需要 AJAX。只需使用类似 $('#preview').attr('src', 'imageftt.php?text=' + text + '&size' + size ......
  • 弗拉基米尔是正确的。除非您对要插入的内容执行一些服务器端逻辑,否则您可以使用简单的 jQuery 来更改内容。

标签: php jquery ajax


【解决方案1】:

你不能用 ajax 来做。您应该做的只是将图像 src 更改为新的 uri 以适应新的字体属性,它会下载新图像而无需重新加载整个页面。

类似:

var text = $('#text').val();
var size = $('#size').val();
var font = $('corbelb.ttf');
var dataString = 'text=' + text + '&size=' + size + '&font=' + font;
$('#imageTagId').attr('src', 'imageftt.php?'+dataString);

【讨论】:

  • U-DON:从技术上讲,您可以使用 AJAX 做到这一点,但为什么呢?更改图像 src 效率更高,并且解决了此海报的问题。
  • 您可以向呈现图像的 phpscript 发出请求,但我不知道如何从那里获取图像并将其放入 html。
【解决方案2】:

我认为有一个更简单的方法:
收集表单数据并使用新请求更改 img 源:

'imageftt.php?text='+newText+'&size='+newSize+'&font='+newFont 

我的意思是“img”标签是静态的。

【讨论】:

    【解决方案3】:

    您可以在目标 PHP 代码中提取 POST 数据。调用的结果应该是渲染 PHP 返回的 HTML。

    $.ajax({
      type: 'POST',
      url: 'imageftt.php',
      data: dataString,
      success: function(result){
        $("#target_div").html(result);
      }
    });
    

    编辑:经过进一步检查,您似乎不需要 AJAX 来实现您的目的,但如果您曾经这样做过,这就是您的做法......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-05
      • 2015-09-11
      • 2013-04-16
      • 2011-05-08
      • 2011-01-17
      • 2011-02-17
      相关资源
      最近更新 更多