dpdjv9559 2012-10-07 10:47
浏览 23
已采纳

没有从JQuery中的PHP脚本返回数据

I'm learning how to integrate JQuery, AJAX and PHP together.

My problem is that my success function isn't getting any value from the parameter and just getting a '0'. I'm not really sure what I'm doing wrong either, but I have just been following this tutorial JQuery & PHP Tutorial and just copied how he got the value from the PHP code using echoes and a parameter variable r.

I have tried searching for the solution, but I'm not sure if any of the results are relevant to what I am doing. I tried following some of their advice but none seems to work (the promise thing for jQuery)

I hope someone can enlighten me what I am doing wrong as I am eager to learn more PHP and jQuery.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
    $(function () {
        jQuery("form").submit(function(e) { 
             var input = $("#input").val();
             var url = 'input=' + input;
             $.ajax({
               type: "POST",
               url: "process.php",
               data: url,
               success: function(r) {
                   $("#output").text(function(r) {
                        return r;
                   });
               }
             });
             e.preventDefault();
        });
    });
</script>
<title>Insert title here</title>
</head>
<body>
    <form action="process.php" method="post">
        Inputs:<br />
        <textarea rows="15" cols="60" id="input" name="input">Some text...
        </textarea><br /><br />
        Start:
        <input type="text" id="start" name="start" />
        End:
        <input type="text" id="end" name="end" /><br />
        <input type="submit" id="submit" name="submit" value="Submit">
    </form>
    <p id="output">Output: Some Random Text</p>
</body>
</html>

PHP:

require 'parser.php';
$parser = new Parser();
#header('index.php');
$hash = $parser->parse($_POST['input']);
$keys = array_keys($hash);
foreach($keys as $key) {
    echo "$key ->";
    $dests = $hash[$key];
    foreach($dests as $dest) {
        echo " $dest";
    }
    echo "<br />";
}

?>
  • 写回答

1条回答 默认 最新

  • douzhi8488 2012-10-07 10:59
    关注
    <script>
        $(function () {
            jQuery("form").submit(function(e) { 
                 var input1 = $("#input").val();
                 var url = {'input' :  input1};   // a little change to the data parameter
                 $.ajax({
                   type: "POST",
                   url: "process.php",
                   data: url,
                   success: function(r) {
                    //   $("#output").text(r);  //<--- changed to this
                       $("#output").html(r);  // this is better as you output html
                   }
                 });
                 e.preventDefault();
            });
        });
    </script>
    

    for jquery text(), use the return value from the server directly. If you are outputting html use jquery html() instead

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)