2017-09-13 93 views
1

我有一个AJAX函数(如下)。我试图将一个页面的输入传递给另一个页面。我的ajax不工作?

它将我重定向到其他页面,但未通过所需的值。

我不明白为什么它不能如我所料。

我正在寻找另一种方式来做到这一点。任何帮助将不胜感激。

//second page 
 
    
 
    <?php 
 
    $cuit = $_POST['cuit']; 
 
    ?> 
 
<input id="act" type="text" class="form-control" value="<?php echo $cuit ?>">

function mod(id, origen) { 
 
    swal({ 
 
    title: 'Are you sure?', 
 
    text: "You won't be able to revert this!", 
 
    type: 'warning', 
 
    showCancelButton: true, 
 
    confirmButtonColor: '#3085d6', 
 
    cancelButtonColor: '#d33', 
 
    confirmButtonText: 'Yes, delete it!', 
 
    cancelButtonText: 'No, cancel!', 
 
    confirmButtonClass: 'btn btn-success', 
 
    cancelButtonClass: 'btn btn-danger', 
 
    buttonsStyling: false 
 
    }).then(function() { 
 
    if (origen == "perfiles") { 
 
     $.post("api/eliminar_perfil.php", { 
 
     id: id 
 
     }, function(mensaje) { 
 
     $("#tr_" + id).remove(); 
 
     }); 
 
    } else if (origen == "index") { 
 
     $.ajax({ 
 
     type: "post", 
 
     url: "perfiles.php", 
 
     data: { 
 
      cuit: $("#cuit").val() 
 
     }, 
 
     success: function(result) { 
 
      location.href = 'http://localhost/miramonteapp/perfiles.php'; 
 
     } 
 
     }); 
 
    } 
 
    swal('Deleted!', 'Your file has been deleted.', 'success') 
 
    }, function(dismiss) { 
 
    // dismiss can be 'cancel', 'overlay', 
 
    // 'close', and 'timer' 
 
    if (dismiss === 'cancel') { 
 
     swal('Cancelled', 'Your imaginary file is safe :)', 'error') 
 
    } 
 
    }) 
 
}
<input id="cuit" type="text" class="form-control" onkeyup="searchCliente();"> 
 

 
<button id="btnBuscar" onclick="mod($('#cuit'), 'index');" type="button" class="btn btn-default" name="button">Buscar</button>

+2

问题是你正在通过ajax发送你的数据到perfiles.php,但是你在成功函数中对perfiles.php做了一个新的请求(第二个请求不知道你在第一个请求中传输的数据) 。这似乎是一个老派的html表单,它将数据提交到perfiles.php几乎就是一个很好的使用案例,根本没有ajax。 – James

回答

1

男孩,我想您的解决方案是简单的,如果你想用一个参数去重定向你不需要Ajax调用。

所以更换此

$.ajax({ 
       type : "post", 
       url : "perfiles.php", 
       data : {cuit: $("#cuit").val()}, 
       success : function(result) { 
       location.href = 'http://localhost/miramonteapp/perfiles.php'; 
       } 
      }); 

location.href = 'http://localhost/miramonteapp/perfiles.php?cuit=' + 
$("#cuit").val(); 

,并在您的目的地页面perfiles.php

刚读帕拉姆并使用它,你可以阅读PARAMS JS使用此为指导How to get the value from the GET parameters?