error with scripts

1

I have the following javascript function. but it does not manage to execute correctly.

//Para mostrar la forma de pago dependiendo del Estado
function mostrar_formaPago(estado)
{
    var contenedor, m; 
    contenedor = document.getElementById('contenedor2');  
    estado = document.form1.estado.options[document.form1.estado.selectedIndex].value
    var data='estado='+estado+'';

    ajax=nuevoAjax(); 

    ajax.open('POST','ajax_tarjeta.php',true); 

    ajax.onreadystatechange=function() 
    { 
        if (ajax.readyState==4) 
        { 
            contenedor.innerHTML = ajax.responseText 
        } 
    }

    ajax.setRequestHeader('Connection','Close');
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.setRequestHeader('Cache-Control','no-cache, must-revalidate');
    ajax.setRequestHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
    ajax.send(data);
}

I have an error in the code?

the document ajax_tarjeta.php has the following code

$estado=$_POST['estado'];

$sql="SELECT * FROM  tarjetas WHERE sw_activo='$estado'";

echo "sql:<br>".$sql;
$consulta=mysql_query($sql) or die(mysql_error());

echo "<select name = 'cod_tarj' style='border-color: #FF0000 #FF0000; border-width: 2px 2px; border-style: solid solid; font-size:8pt; color: #FF0000; letter-spacing; width:150px'>";
    echo "<option value = ''></option>";
        while($result=mysql_fetch_array($consulta)) 
        {
            echo "<option value = ".$result['cod_tarj'].">".$result['cod_tarj']."_".$result['desc_tarj']."</option>";
        }

echo "</select>";
?>
    
asked by Norbey Martinez 24.05.2016 в 15:02
source

1 answer

1

I did the test and it worked for me with the following changes. I show a simplification that works without the database and completing the missing parts.

<form id=form1 name=form1 action=post method=>
<div class='cs'>Listar Forma de Pago:</div>
<select id='select_estado' name='estado' onchange='mostrar_formaPago()'>
<option value='0'></option>
<option value='3'>Todas</option>
<option value='1'>Activas</option>
<option value='0'>Inactivas</option>
</select>
<div class='cs'>Forma pago</div><div id='contenedor2'></div>
</form>
<script>
function mostrar_formaPago(estado)
{
        var contenedor, m; 
  contenedor = document.getElementById('contenedor2');  
 // document.form1.orden.value = ''

  estado = document.form1.estado.options[document.form1.estado.selectedIndex].value

  var data='estado='+estado+'';
  /// ajax=nuevoAjax(); 
  var ajax=new XMLHttpRequest();

  ajax.open('POST','ajax_tarjeta.php',true); 

  ajax.onreadystatechange=function() 
  { 
    if (ajax.readyState==4) { 

       contenedor.innerHTML = ajax.responseText 
    } 

  }

        ajax.setRequestHeader('Connection','Close');
        ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        ajax.setRequestHeader('Cache-Control','no-cache, must-revalidate');
        ajax.setRequestHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
        ajax.send(data);

}
</script>

and in the ajax_tarjeta.php I removed the part of the database (in case that is what it is not) replacing it with an example line only:

<?php
header('Access-Control-Allow-Origin','*');

$estado=$_POST['estado'];

$sql="SELECT * FROM  tarjetas WHERE sw_activo='$estado'";

echo "sql:<br>".$sql;

echo "<select name = 'cod_tarj' style='border-color: #FF0000 #FF0000; border-width: 2px 2px; border-style: solid solid; font-size:8pt; color: #FF0000; letter-spacing; width:150px'>";
    echo "<option value = ''></option>"; 
        echo "<option value = ".'77'.">".'77'."_".'esta'."</option>";
echo "</select>";
?>

The select was wrong in your comment and the newAjax replaced it too. I tried it in Chrome and it works.

You could try this example to see if it helps or tell us if this is going on or not. You can also see whether or not the connection to the database goes by replacing the OR DIE with something that shows the error message of the connection

    
answered by 25.05.2016 в 18:36