jQuery, Ajax, PHP Function


jQuery, Ajax, PHP Function

希望这是正确的地方,有人可以帮助。

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

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

当它工作时,你可以选择文本和大小,按set键,它将显示在灰色框中。

在没有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>

* UPDATE *

在帮助下,我得到了进一步的帮助,我正在做一些类似这里的建议,但它仍然不起作用

使用下面的代码,所有值都被正确警告,但文本不显示。产生了一个灰色的盒子,这让我认为它正在调用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。你要做的就是把image 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);

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

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

我的意思是"img"标签将是静态的

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

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

Edit:经过进一步检查,您似乎不需要AJAX来满足您的目的,但是如果您需要的话,您应该这样做…