Agustín Osorio
Agustín Osorio

Reputation: 101

my ajax dont work at all?

I have an AJAX function (below). I am trying to pass an input from one page to another.

It redirects me to the other page but does not pass the required value.

I do not understand why it doesn't work as I expected.

I am looking for another way to do this. Any help will be appreciated.

  //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>

Upvotes: 1

Views: 42

Answers (1)

Victor Hugo Terceros
Victor Hugo Terceros

Reputation: 3169

Boy, I think your solution is simpler, you don't need the ajax call if you want to redirect with a param.

so replace this

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

by this

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

and in your destination page perfiles.php

just read the param and use it, you can read params in JS using this as a guide How to get the value from the GET parameters?

Upvotes: 1

Related Questions