dqdt45183
2011-04-15 09:48
浏览 62
已采纳

如何从AJAX调用中返回变量?

Just a simple question (I think).

Have a look at the code in this fiddle: http://jsfiddle.net/d9wdM/1/

What I doing is creating a website as a personal project that let's the user enter any amount of names and e-mail addresses and once the form is submitted, a random name is selected from the names and an e-mail is sent to that person and also their name is outputted on the page.

I can get the AJAX call to work but I don't know how to pass the name variable back from process.php to index.php as all it can return is a 1.

I hope that makes sense, if it doens't please let me know and I will try and make it clearer.

Thanks again,

Martin

图片转代码服务由CSDN问答提供 功能建议

只是一个简单的问题(我认为)。

看看 这个小提琴中的代码: http://jsfiddle.net/d9wdM/1/

我所做的是创建一个网站作为个人项目,让用户输入任意数量的名称和电子邮件地址,一旦提交表单,就会从名称和e中选择一个随机名称 -mail被发送给该人,并且他们的名字也会在页面上输出。

我可以让AJAX调用工作,但我不知道如何从名称变量传递 process.php到index.php因为它可以返回的是1。

我希望这是有道理的,如果它不行,请告诉我,我会尽量让它更清晰 。

再次感谢,

Martin

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyao5186 2011-04-15 13:35
    已采纳

    Managed to solve it, changed the ajax call to:

    <script type="text/javascript">
        $(function() {
            $('.tearoundform').submit(function() 
            {
                 var query = $(this).serialize();
                 $('#form_results').fadeOut(500).addClass('ajax-loading');
                 $.ajax({
                     type: "GET",
                     url: "process.php",
                     data: query,
                     success: function(data)
                     {
                          $('#form_results').removeClass('ajax-loading').html(data).fadeIn(500);
                     }
                 });
            return false;
           });
        });
    </script>`
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanban4769 2011-04-15 09:59

    In your ajax code your are misssing dataType which means the type of data that you're expecting back from the server.

    So in your case you can set dataType as html and render html from server. This will help you.

    评论
    解决 无用
    打赏 举报
  • drema2014 2011-04-15 10:03

    I'll suggest u one thing if u want something return from ajax page then must use dataType='json'

    and use json_encode(array('returnData'=>1))on process.php

    again on index.php u can use json_decode()

    Reference
    dataType='json'

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题