【问题标题】:Php mysql undefined index error used jquery for traferring data from one page to anotherphp mysql未定义索引错误使用jquery将数据从一页转移到另一页
【发布时间】:2015-07-28 18:41:25
【问题描述】:

这是我第一个使用 Jquery 的项目。 有两个页面 1.listofleaders.php 2.leadersprofile.php

在第一页,即 listofleaders.php 我有一个输入文本框,用户在其中输入领导者姓名,我使用 jQuery 代码将文本框值传输到 leaderprofile.php 页面

    <html>
    <head>
    <script>
      function ls() 
        {
        var leaderdetails = "leaderprofile.php?lname="+$("#gopal").val();
        $.get(leaderdetails, function( data ) {
        //alert(leaderdetails);
        location.href = "leaderprofile.php";
        });
        }   
    </script>
    </head>
    <body>
    <input type="text" id="gopal" name="t"  placeholder="Start Typing" size="50" />
<button onclick="ls();" type="button">Go!</button><br><br>
    </body>
    </html>

在第二页的leadersprofile.php我已经写了这个,

<?php
include "admin/includes/dbconfig.php";
$lname = $_GET['lname'];
echo $lname;
?> 

但在第二页,即 leaderprofile.php 上,它向我显示错误 未定义索引:lname

我对这种方法是否正确? 我哪里错了? 希望你能理解。

【问题讨论】:

  • location.href = "leaderprofile.php"; 将您重定向到 leaderprofile.php 而不使用参数。你期待什么?
  • 我想带参数去leaderprofile.php我应该在那里写什么?
  • location.href = "leaderprofile.php?parameter=value";?
  • 我必须删除 var leaderdetails = "leaderprofile.php?lname="+$("#gopal").val();
  • 我不知道你应该如何处理你的代码。

标签: javascript php jquery html mysql


【解决方案1】:

所以我在这里根据您的问题描述猜测您要达到的目标。

如果您想将&lt;input&gt; 值发送到另一个页面,您最好使用经典的 POST 请求(无需改进 jQuery):

<form method="post" action="leadersprofile.php">
    <input type="text" name="lname"/>
    <button type="submit">Send</button>
</form>

在leadersprofile.php中:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['lname'])) {
    $lname = $_POST['lname'];
    var_dump($lname); // outputs whatever the user input was
}

现在,如果您想在不重新加载页面的情况下将数据发送到 leadersprofile.php,您正在寻找 Ajax 请求 (XmlHttpR请求)。

jQuery(function($) {
    $('form').on('submit', function(e) {
        e.preventDefault(); // prevents default behavior that is submitting the form
        $.ajax({
            method: 'post', // can be also 'get'
            url: 'leadersprofile.php',
            data: {lname: $('input').val() },
            success: function(html) {
                $('div').html(html); // place whataver was printed in leadesrprofile.php into a div
            },
            error: function(r) { // fire if HTTP status code != 200
                console.log(r);
            }
        });
    });
});

【讨论】:

    【解决方案2】:

    您似乎正确地使用了 JQuery。提取值和发送 GET 请求的 Javascript 应该可以正常工作。

    您的误解在于您如何检查PHP文件是否已收到请求。这个重定向

    location.href = "leaderprofile.php";
    

    不会向您提供有关您刚刚发出的 GET 请求的任何信息。相反,您可以尝试:

    location.href = "leaderprofile.php?lname=" + $("#gopal").val()
    

    验证您的 PHP 和 Javascript 是否按预期执行。如果您看到您期望的值,那么我相信您已经确认了两件事:

    • 已成功从文本框中提取正确的值
    • GET请求成功,正在调用成功回调

    【讨论】:

      【解决方案3】:

      我明白你的问题。试试下面的代码。

      listofleaders.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Document</title>
      </head>
      <body>
          <form>
              <table>
                  <tr>
                      <td>Name:</td>
                      <td><input type="text" id="name"></td>
                  </tr>
                  <tr>
                      <td></td>
                      <td><button id="submit">Submit</button></td>
                  </tr>
              </table>
          </form>
          <script src = "jquery.js"></script>
          <script src = "leader.js"></script>
      </body>
      </html>
      

      当提交按钮被点击时,leader.js文件会得到文本框的值。

      leader.js

      $(document).ready(function() {
          $('#submit').on('click', function(){
      
              var name = $('#name').val();
      
              $.ajax({
                  url:'leaderprofile.php',
                  type:'POST',
                  data:{'name':name},
                  success:function(data){
      
                  }
              });
          });
      
      });
      

      现在,这个 leader.js 文件会将 name 密钥发送到 liderprofile.php。

      在 php 文件之后 witt 将数据(名称)返回到 js 文件..并且 js 文件将警告名称。

      leaderprofile.php

      <?php
      
      $name = $_POST['name'];
      echo $name;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多