方法 isset 在带有 AJAX 的 php 站点中不起作用


method isset does not work in php Site With AJAX

这是我的标题.php页面,其中包含一些页面的链接,我使用ajax开发我的网站

<li><a href="log.php" class="ajaxtrigger">login</a></li>
<li><a href="chenge_pass.php" class="ajaxtrigger" >chenge password </a></li>
//some codes
<div id="target">  </div>  

这是我的 Ajax 代码

   $(document).ready(function(){
  $('.ajaxtrigger').click(function(){
    var url = $(this).attr('href');
    $('#target').load(url);
    return false;
  });
});

任何带有类 ajaxtrigger 的链接都将通过 AJAX 加载内容,并将其显示在具有 ID 目标的元素中。我的问题是,例如,当我想更改我的密码并单击链接"更改密码"时,页面将调用但文本框未设置(我认为!因为if(isset($_REQUEST["TXT2"]((剂量不返回true!(并且"if"剂量中的所有代码未运行这是我chenge_pass.php:

<form name="c" action="" method="POST">
last password<input class=fild name="txt2" type="password">
new password<input class=fild name="txt4" type="password" >
repeat new password<input class=fild name="txt3" type="password" >
<input class="btn b2" name="btn" type="submit" value="run">
 </form>
 <?php
   if(isset($_REQUEST["txt2"]))
  {
   if(!empty($_REQUEST["txt3"]) and !empty($_REQUEST["txt4"]) and !empty($_REQUEST["txt2"])  )
   {
    $txt3=($_REQUEST["txt3"]);
    $txt4= ($_REQUEST["txt4"]);
    $txt2=($_REQUEST["txt2"]);
    //code for save changes into mysql database
    }
    }
 ?>

如何设置文本框,以便将更改保存到数据库中?

这个问题

有点模糊...但如果我可以尝试理解和总结,那就是:"您有一个通过 AJAX 加载chenge_pass.php文件的页面。然后在提交时,您可能希望在将字段保存到数据库之前对字段进行一些状态验证。

如果这是真的...

您的文本字段将不会固定,因为#target中的内容来自当前对浏览器历史记录不可见的 PHP 文件chenge_pass.php(除非您使用的是 push-state,此处未指明(。

因此,当提交表单(您的规范中的操作默认为浏览器 URL 指向的页面(时,页面将重新加载......我敢说,您必须重新触发单击才能查看密码表单。

现在我们明白了,有两种解决方案

  1. 完全删除 AJAX:这样,您可以将chenge_pass.php文件正确加载为其自己的页面。表单提交后,您可以正确查看字段。

  2. 通过AJAX
  3. 提交您的密码表单:这是我的首选解决方案,因为无论如何表单都是通过AJAX加载的。

无论您选择哪个选项,都要修复您所说的内容...

将调用页面,但未设置文本框

。你必须有这样的东西

<input class=fild name="txt2" type="password" value="<?php echo $_REQUEST['txt2']?>">

这样,如果您填充了 $_REQUEST['txt2'],它将以表单显示。

编辑表单: <form name="c" action="chenge_pass.php" method="POST">

有点不清楚你在问什么,但从我所看到的,你会把你的表格发布到错误的脚本中。

使用 ajax 加载表单时,将加载以下表单:

<form name="c" action="" method="POST">

空的action属性将导致表单发布到当前页面/脚本。这是包含您的链接的页面,而不是chenge_pass.php脚本。

因此,首先,您需要更改它:

<form name="c" action="chenge_pass.php" method="POST">

当您单击表单未发布的链接时,您必须单击按钮<input class="btn b2" name="btn" type="submit" value="run">以发布表单输入。

如果你想处理 ajax,你应该首先删除你的 href:

<a href="javascript:void(0);" class="ajaxtrigger" >chenge password </a>

然后,您可以通过以下代码发布数据:

$(document).ready(function(){
  $('.ajaxtrigger').click(function(){
    var url = "chenge_pass.php";
    $.ajax({
      type: "POST",
      url: url,
      data: $("form").serialize()
    });
 });
});

我发现你的href链接是活跃的,所以它在ajax运行之前先运行,所以你的$_REQUEST["txt2"]是空的。尝试通过插入点击返回假

<li><a href="chenge_pass.php" class="ajaxtrigger" onclick="return false;">chenge password </a></li>

也许你应该重新考虑代码的架构。如果你使用的是ajax,试着把你的视图和你的php进程分开,像这样:

application | |- controllers | | | - change_passoword.php | |- views | | | - change_password.html | |- index.html

索引.html

<html>
    <!-- Your html... -->
    <li><a href="log.php" class="ajaxtrigger">login</a></li>
    <li><a href="chenge_pass.php" class="ajaxtrigger" >chenge password </a></li>
    //some codes
    <div id="target">  </div>  
</html>
<script>
$(document).ready(function(){
  $('.ajaxtrigger').click(function(){
      var url = $(this).attr('href');
      $('#target').load(url);
      e.preventDefault();
  });
});
</script>

views/change_password.html

<form id="myForm" name="c" action="controllers/change_password" method="POST">
    last password<input class=fild name="txt2" type="password">
    new password<input class=fild name="txt4" type="password" >
    repeat new password<input class=fild name="txt3" type="password" >
    <input class="btn b2" name="btn" type="submit" value="run">
</form>
<script>
    var form = $('#myForm');
    form.submit(function(e) {
        e.preventDefault();
        $.ajax({
            url: form.attr('action'),
            type: "POST",
            dataType: "JSON",
            data: $('#myForm').serialize(),
            success: function(data) {
                // Do something
                // data is an object, data.success is a boolean, data.message is the controller message, see controllers/change_passwords.php to understand why
            },
            error: function(a,b,c) {
                console.error("Error during request", b);
            }
        });
    });
</script>

控制器/change_password.php

<?php
header("Content-Type: application/json");
if (!empty($_POST) && !empty($_POST['txt2']) && !empty($_POST['txt3']) && !empty($_POST['txt4'])) {
    // Do your stuff here
    if ($allIsFine) { //If everything is fine
        echo json_encode(array("success" => true, "message" => "Your password has been changed"));
    } else {
        echo json_encode(array("success" => false, "message" => "Something went wrong"));
    }
} else {
    echo json_encode(array("success" => false, "message" => "Please fill and inputs"));
}