problem with header location in two different urls

1

I have the following code

require('db.php');

if($_POST['id']);        
{ 
    for($i=0; $i<count($_POST['id']); $i++) 
    {

        $msjn=$_POST['mensaje'];
        $idfoto=$_POST['id'];
        $nomb=$_POST['nombre'];
        $r1=$_POST['responsable1'];
        $r2=$_POST['responsable2'];
        $esms=$_POST['estado_sms'];
        $conc=$_POST['concepto'];
        $fc=$_POST['fecha'];
        $d=$_POST['dia'];
        $m=$_POST['mes'];
        $an=$_POST['ano'];
        $cm=$_POST['cm'];

        $nmob=$_POST['mobile'];

        $query="INSERT INTO sms (id, nombre, responsable1, responsable2, estado_sms, concepto, fecha, mobile, mensaje, dia, mes, ano) VALUES ('".$idfoto[$i]."','".$nomb[$i]."','".$r1[$i]."','".$r2[$i]."','".$esms[$i]."','".$conc."','".$fc[$i]."','".$nmob[$i]."','".$msjn."','".$d."','".$m."','".$an."')"; 
        $r = $conexion->query($query);


        //envio del sms 
        $umob=implode($nmob);
        header("Location: https://sms.net/eapi/submission/send_sms/2/2.0?username=".$usuario."&password=".$pass."&message=".$msjn."&msisdn=".$umob."".$telf."");

        //Aqui debe redireccionarme a la pagina principal
        $alerta="Mensaje enviado Exitosamente!";
header("Location: ../pagina_principal.php?msj=".$alerta." ");   

    }   
}

The problem I have is that if I comment on the second header ("Location: ..") which redirects me to the main page, it redirects me perfectly and sends me the sms, it also inserts the data.

When I use the two header ("Location: ..") I insert the data correctly and redirect me with the message to the main page but do not send me the sms, as I can send these two header correctly.

    
asked by Alexander Quiroz 01.03.2017 в 16:18
source

2 answers

1

why not try saving the form by ajax and after you have saved it send the message, something like this:

<!-- language: lang-js -->

    <script>
    ("#insertar").click(){
         var msjn=$('#mensaje').val();
         var idfoto=$('#id').val();
         ...
         ...
         ...
         var s={'mensaje'=>msjn,'id'=>idfoto,....} 
         $.ajax({
                  type: "POST",
                  url: "https://sms.net/eapi/submission/send_sms/2/2.0",
                  data:s,
                  contentType: "text/plain; charset=UTF-8",
                  dataType: "text",                         
                  error: function(jqXHR, textStatus, errorThrown) {   },
                  success: function(data, textStatus, jqXHR) {
                        var alerta="Mensaje enviado Exitosamente!";
                        var s1="{'msj':alerta}"
                        $.ajax({
                             type: "POST",
                             url: "../pagina_principal.php",
                             data:s1,
                             contentType:"text/plain; charset=UTF-8",
                             dataType: "text",                         
                             error: function(jqXHR, textStatus, errorThrown)                                           {},
                             success: function(data, textStatus, jqXHR) {}                        
                                    }); 

                                   }                        
         }); 
    }

    <script>

<!-- language: lang-html -->

    <form>
    ....
    <button id="insertar">..</button>
    </form> 

Of course for that use a for, for example:

for(var i=0;i<arr.length;i++){  
    var a=arr[i]; 
    var s="{'id'=>a.id,'nombre'=>a.nombre}";
    $.ajax({
              type: "POST",
              url: "..",
              data:s,
              contentType:"text/plain; charset=UTF-8",
              dataType: "text",                         
              error: function(jqXHR, textStatus, errorThrown){},                                 
              success: function(data, textStatus, jqXHR) {}                        
          }); 
}
    
answered by 01.03.2017 в 16:48
-1

You can not do it using two header("Location...") . The first request you have to do it not changing the location of the browser, but internally.

The way you have it seems to be a GET request, so the easiest variant you can try is:

$envio = file_get_contents("https://sms.net/eapi/submission/send_sms/2/2.0?username=".$usuario."&password=".$pass."&message=".$msjn."&msisdn=".$umob.$telf);

$alerta="Mensaje enviado Exitosamente!";
header("Location: ../pagina_principal.php?msj=".$alerta." ");   

But there are actually more elegant ways to do it. For example using curl or Guzzle , which would let you know if the request to the SMS service returned a 200 (success) header or an error header. At this moment you have your message of success in hard and not conditioned to what the SMS service responds.

    
answered by 01.03.2017 в 16:25