【问题标题】:unable to make ajax work to send form data无法使 ajax 工作以发送表单数据
【发布时间】:2014-11-15 10:06:42
【问题描述】:

我看不出下面的代码有什么问题。我只是想将一个 php 变量提交到另一个 php 页面。请不要说会话,因为我知道肯定会话不会在这里为我工作。我想要的只是通过 ajax 将会话变量发送到下一个 php 页面,而用户不知道它。

        <?php
        session_start();
        $fname=$_SESSION['mail'];
        ?>

        <!DOCTYPE HTML>
        <html>
        <title>Addressbook</title>
        <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>





 <script type="text/javascript">

$(function(){


$.ajax({
     url:"DbManipulate.php",
              type:"POST",
              data:"source1:"<?php echo $fname ?>""

               });

}

 </script>          
        <link rel="stylesheet" type="text/css" href="crudstyle.css" />

        </head>
        <body>

            <div id="hidden_form_container" style="display:none;"></div>

        <div id="mhead"><h2>Your Adressbook</h2></div>
        <div id="note"> <span> your addressbook is connected to our servers :) </span></div>
        <?php
        echo $fname;

        ?>
        <table id='demoajax' cellspacing="0">
        </table>
        <script type="text/javascript" src="script.js"></script>

        </body>
        </html>

【问题讨论】:

标签: javascript php jquery ajax session


【解决方案1】:

在您的代码中将data 作为object 传递是不正确的。请按以下方式更改并尝试

data:{source1:"<?php echo $fname ?>"}

【讨论】:

  • 然后我可以访问 source1 作为发布的数据吗?像 $POST["source1"]
  • 是的,当然!您可以使用 $_POST["source1"] ...您在之前的评论中错过了 _。
  • 试过了,你说没用。我使用 $POST_["source1"] 检索并在 php 页面中回显它。但我没有得到任何输出。
  • 能否请您使用 firebug 跟踪请求或在 chrome 中单击 F12 并查看网络选项卡...
【解决方案2】:

$.ajax() 调用中的data:value 部分存在语法错误

你应该使用

data: "source1=<? php echo $fname ?>"

data:{source1:"<? php echo $fname ?>"}

【讨论】:

    【解决方案3】:
           <?php
            session_start();
            $fname="surat";
            ?>
    
            <!DOCTYPE HTML>
            <html>
            <title>Addressbook</title>
            <head>
            <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
    
    
    
    
    
     <script type="text/javascript">
    
    $(function(){
    
    var sessioni='<?php echo $fname ?>';
    $.ajax({
         url:"DbManipulate.php",
                  type:"POST",
                  data:{source1:sessioni}
    
                   });
    
    });
    
     </script>          
            <link rel="stylesheet" type="text/css" href="crudstyle.css" />
    
            </head>
            <body>
    
                <div id="hidden_form_container" style="display:none;"></div>
    
            <div id="mhead"><h2>Your Adressbook</h2></div>
            <div id="note"> <span> your addressbook is connected to our servers :) </span></div>
            <?php
            echo $fname;
    
            ?>
            <table id='demoajax' cellspacing="0">
            </table>
            <script type="text/javascript" src="script.js"></script>
    
            </body>
            </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多