处理 AJAX 后在同一页的php,这两个ajax后在同一页没有网址后

kyvafyod  于 2023-05-16  发布在  PHP
关注(0)|答案(1)|浏览(103)

我需要处理2后在同一页面上处理2个不同的PHP代码。
第一个POST验证后,如果所有字段都有有效数据,这将启用另一个块,在新块中将有更多的输入,在第二个块启用后,我需要第二个POST,在第二个POST上,我正在努力,我无法得到第二个POST的回声。
就像有两个表单,在同一个页面上提交了form1之后,这将启用一个新的表单(form2),然后验证来自第二 AJAX 帖子的表单2数据,使用来自第二个帖子的数据,我将执行一个php函数。
我确实有2个表单作为适当的html,但form2总是被提交,即使有预防defautl表单作为总是提交,这在我的页面上造成了很多冲突。

<html>
<header>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
    </script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </script>

</header>

<body>

<div class="container">
    <div class="row justify-content-md-center alternate-background">
        <main class="col-12 col-md-10 col-lg-8">
            <h1 class="form-signin-heading mt-4 mb-3 alternate-background"> Llene los 2 campos para validar sus numero de cliente y rif</h1>
            <div id="test" style="hide"></div>
            <form method="post" id="a">
                <div class="form-group">
                    <input type="text" class="form-control" name="bookId" id="bookId" value="">
                    <input type="text" class="form-control" name="book" id="book" value="">
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-block" name="validate" id="validate">Update</button>
                </div>
            </form>
        </main>
    </div>
</div>

<?php
//error_reporting ( E_ALL );
//ini_set ( 'display_errors', 1 );
//if( isset($_POST['ajax1']) && isset($_POST['bookId']) ){

if (isset($_POST['bookId'])) {

    if (empty(trim($_POST['bookId'])) || empty(trim($_POST['book']))) {
        echo 'Some fields are empty. the post came empty';
    }
    else {

        $projectid = $_POST['bookId'];
        $projecti = $_POST['book'];
        echo $projectid;
        echo $projecti;
        echo '
          <div class="container">
            <div class="row justify-content-md-center alternate-background">
              <main class="col-12 col-md-10 col-lg-8">
                <h1 class="form-signin-heading mt-4 mb-3 alternate-background"> porfavor crear su usuario y contraseña</h1>
                
                  <form  method="post" id="a2">
                  
                    <div class="row mb-3">
                      <label id="username-label" class="col-form-label col-12 col-md-4 text-md-right text-md-end">Usuario *</label>
                      <div class="col-12 col-md-8">
                        <span id="usernameCheck" class="small"></span>
                        <input type="text" class="form-control" id="username" name="username" placeholder="Usuario" value="" required="" autofocus="" autocomplete="username">
                      </div>
                    </div>
        
                    <div class="row mb-3">
                      <label for="fname" id="fname-label" class="col-form-label col-12 col-md-4 text-md-right text-md-end">Nombre *</label>
                      <div class="col-12 col-md-8">
                        <input type="text" class="form-control" id="fname" name="fname" placeholder="Nombre" value="" required="" autofocus="" autocomplete="given-name">
                      </div>
                    </div>
        
                    <div class="col-12 col-md-8 offset-md-4">
                      <div class="submit btn btn-primary "  id="registerUser">
                        <span class="fa fa-user-plus mr-2 me-2"></span> Registrarse          
                      </div>
                    </div>
                    
                  </form>
                  
              </main>
            </div>
          </div>
       ';
    }
}
else {
    //echo "both must be filled";
}

if (isset($_POST['username'])) {

    $projecx = $_POST['username'];
    $projecxx = $_POST['fname'];
    echo $projecx;
    echo $projecxx;
    // show the data
}
?>

<script type="text/javascript">

    $("#registerUser").click(function () {

        var usernn = $("#username").val();
        var usernnn = $("#fname").val();
        console.log(usernn);
        console.log(usernnn);

        jQuery.ajax({
            type: "POST",
            data: {username: usernn, fname: usernnn}
            /*
            success: function(data) {
               jQuery(".res").html(data);
               $("#test").html(data);
               console.log(data);
            }
            */
        });
    });

    $("#validate").click(function () {

        console.log("The paragraph was clicked.");
        var unc = $("#bookId").val();
        var rif = $("#book").val();

        jQuery.ajax({
            type: "POST",
            data: {bookId: unc, book: rif}

            success: function (data) {
                jQuery(".res").html(data);
                $("#test").html(data);
            }
        });
    });
</script>

</body>
</html>
whlutmcx

whlutmcx1#

请将$("#registerUser").click(function(){....})操作放在系统将显示容器的块中。
以这种方式,当呈现容器块时,系统可以正确地与点击动作链接。
所以,根据你的代码,请修改到下面,看看效果吧:

<html>
<header>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> 
        </script>

          </header>

<body>
    
    <div class="container">
     <div class="row justify-content-md-center alternate-background">
      <main class="col-12 col-md-10 col-lg-8">
        <h1 class="form-signin-heading mt-4 mb-3 alternate-background"> Llene los 2 campos para validar sus numero de cliente y rif</h1>
       
              <div id="test" style="hide"></div>
                <form  method="post" id="a">
                   <div class="form-group">
                       <input type="text" class="form-control" name="bookId" id="bookId" value="">
                       <input type="text" class="form-control" name="book" id="book" value="">
                   </div>
                   <div class="modal-footer">
                       <button type="submit" class="btn btn-block" name="validate" id="validate">Update</button>
                   </div>
               </form>
            </main>
          </div>
        </div>
 

    
<?php   
    if (isset($_POST['bookId'])) {
          if (empty(trim($_POST['bookId'])) || empty(trim($_POST['book'])) ) {
           echo 'Some fields are empty. the post came empty';
          } else {  
                                   
          $projectid = $_POST['bookId'];    
          $projecti = $_POST['book'];   
          echo $projectid;
          echo $projecti;   
          echo '
          <div class="container">
        <div class="row justify-content-md-center alternate-background">
          <main class="col-12 col-md-10 col-lg-8">
      
      <h1 class="form-signin-heading mt-4 mb-3 alternate-background"> porfavor crear su usuario y contraseña</h1>
       <form  method="post" id="a2">
      <div class="row mb-3">
          <label id="username-label" class="col-form-label col-12 col-md-4 text-md-right text-md-end">Usuario *</label>

          <div class="col-12 col-md-8">
            <span id="usernameCheck" class="small"></span>
            <input type="text" class="form-control" id="username" name="username" placeholder="Usuario" value="" required="" autofocus="" autocomplete="username">
          </div>
        </div>
        
        <div class="row mb-3">
          <label for="fname" id="fname-label" class="col-form-label col-12 col-md-4 text-md-right text-md-end">Nombre *</label>
          <div class="col-12 col-md-8">
            <input type="text" class="form-control" id="fname" name="fname" placeholder="Nombre" value="" required="" autofocus="" autocomplete="given-name">
          </div>
        </div>
        
        <div class="col-12 col-md-8 offset-md-4">
          <div class="submit btn btn-primary "  id="registerUser">
            <span class="fa fa-user-plus mr-2 me-2"></span> Registrarse          </div>
              </div>
       </form >
        </main>
        </div>
        </div>
       ';
?>
<script> 

$("#registerUser").click(function(){
alert('ok');

var usernn = $("#username").val();
var usernnn = $("#fname").val();
alert( usernn);
alert( usernnn);

//PLEASE put other code here 

                 });
               </script>
<?php
        }
      } else {          
            //echo "both must be filled";
        }
        
if (isset($_POST['username'])) {
    
         $projecx = $_POST['username']; 
        $projecxx = $_POST['fname'];    
          echo $projecx;
         echo $projecxx;        
         // show the data   
          }
         ?>
    
  
  <script type="text/javascript">

    
               $("#validate").click(function(){
              console.log("The paragraph was clicked.");
               var unc = $("#bookId").val();
                  var rif = $("#book").val();
           
                jQuery.ajax({
                type: "POST",
                data: {bookId: unc, book: rif }
                 
                success: function(data){
                   jQuery(".res").html(data);

                 $("#test").html(data);

                }
                });
                
                              });

                         </script>
                      </body>
                       </html>

相关问题