Ejecutar Php en JavaScript

Quisiera trabajar con javascript y php. Cuando el usuario presione Enter que haga una consulta a una tabla de SqlServer, me dijeron que lo hiciera con javascript, este es el código pero no me ejecuta el código php; el código funciona bien cuando no lo pongo dentro del javascript.
<script language="JavaScript" type="text/JavaScript">
<!--
// Función que actualiza la página al cambiar el Número de Factura
function BuscaAfiliado ()
{
Factura = document.forms.form1.Factura.value;
alert(Factura);
<?php include "ValidaAfiliado.php";
if(isset($_REQUEST['Factura'])){
$VP_Factura = ValidaFactura($_REQUEST['Factura']);
$VL_TipIden = $VP_Factura[0];
$VL_NoIden = $VP_Factura[1];
}
?>
}
// -->//-->
</script>

2 respuestas

Respuesta
1
Desgraciadamente php y javascript no trabajan cómodamente juntos en situaciones como la que planteas...
Si quieres podes contarme un poco más del script así planteamos una solución alternativa...
Saludos.
De esos 20 campos, 4 son de búsqueda y 6 hay que digitarlos.
Todo eso ya lo tengo listo, mi problema es que los usuarios están acostumbrados a utilizar enter para pasar a otro campo; tengo un botón para ejecutar los comandos php para guardar todo lo que guardaron en el formulario, como sabes si oprimen Enter se ejecuta ese botón porque es de tipo submit, si le pongo de tipo button no se como ejecutar la sentencia php.
Aquí esta mi código si puedes revisarlo y darme alguna alternativa para el guardado de datos y que pueda utilizar el enter para pasar de un campo a otro, se que es con document. form.txt.focus(), me sale perfecto cuando no hay boton pero cuando lo hay manda a ejecutar php.
<?php
if(isset($_REQUEST['Oculto']))
{
if(isset($_REQUEST['btnGuardar']))
{
include "conexion.php";
mssql_select_db ("BD", $conexion);
if($Ins=mssql_query("insert into table(Factura,Medico,CodPresServ,
CodConsulta,FechaConsulta,FinalConsulta,CausaExterna,
DiagPrincipal,DiagRelac1,DiagRelac2,TipoDiag,Empresa)
values(".$_REQUEST['Factura'].",'".$_REQUEST['Medico'].
"','UT-001',".$_REQUEST['txtCodConsul'].",'"
.$_REQUEST['txtFechaConsul']."','".$_REQUEST['txtFinaConsul'].
"','".$_REQUEST['txtCausaExte']."','".$_REQUEST['Diagnostico'].
"','".$_REQUEST['Diagnostico1']."','".$_REQUEST['Diagnostico2'].
"','".$_REQUEST['txtTipoDiag']."','".$_REQUEST['Empresa']."')",$conexion))
echo "<script>";
echo "alert('Datos Guardados con éxito');";
echo "</script>";
mssql_close($conexion);
}
}
?>
<html>
<head>
<title>Documento sin título</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="">
<table width="90%" border="1" align="center">
<tr>
<td><table width="75%" align="center">
<tr>
<td colspan="4"><div align="center"><font color="#006666" size="5" face="Courier New, Courier, mono">Consulta
Externa Afiliados</font></div>
<div align="right"></div></td>
</tr>
<tr>
<td><div align="right">Factura</div></td>
<td><input name="Factura" type="text" id="Factura" onKeyPress="if(event.keyCode==13){'&<?php include "ValidaAfiliado.php";
if(isset($_REQUEST['Factura'])){
$arr[0] = $_REQUEST['Medico'];
$arr[1] = $_REQUEST['txtCodConsul'];
$arr[2] = $_REQUEST['txtFechaConsul'];
$arr[3] = $_REQUEST['txtFinaConsul'];
$arr[4] = $_REQUEST['txtCausaExte'];
$arr[5] = $_REQUEST['txtTipoDiag'];
$arr[6] = $_REQUEST['Empresa'];
$VP_Factura = ValidaFactura($_REQUEST['Factura']);
$VL_TipIden = $VP_Factura[0];
$VL_NoIden = $VP_Factura[1];
$VL_PriApe = $VP_Factura[2];
$VL_SegApe = $VP_Factura[3];
$VL_PriNomb = $VP_Factura[4];
$VL_SegNomb = $VP_Factura[5];
if($VL_TipIden != ""){
$VL_Medico = $arr[0];
$VL_CodConsul = $arr[1];
$VL_FechaConsul = $arr[2];
$VL_FinConsul = $arr[3];
$VL_CausaExte = $arr[4];
$VL_TipoDiag = $arr[5];
$VL_Empresa = $arr[6];}
else{
$VL_Medico = "";
$VL_CodConsul = "";
$VL_FechaConsul = "";
$VL_FinConsul = "";
$VL_CausaExte = "";
$VL_TipoDiag = "";
$VL_Empresa = "";
}
}
?>&'; submit();}"
value="<?php echo $Factura;?>" size="10" maxlength="10"> </td>
<td><div align="right">Medico</div></td>
<td><input name="Medico" type="text" id="Medico" value="<?php echo $VL_Medico;?>" size="30" maxlength="30" ></td>
</tr>
<tr>
<td><div align="right">Tipo Identificación</div></td>
<td><input name="txtTipIden" type="text" id="txtTipIden" value="<?php echo $VL_TipIden;?>" size="2" maxlength="2" readonly=""></td>
<td><div align="right">Número Identificación</div></td>
<td><input name="txtNoIden" type="text" id="txtNoIden" value="<?php echo $VL_NoIden;?>" size="16" maxlength="16" readonly=""></td>
</tr>
<tr>
<td><div align="right">Primer Apellido</div></td>
<td><input name="txtPriApel" type="text" id="txtPriApel" value="<?php echo $VL_PriApe;?>" size="20" maxlength="20" readonly=""></td>
<td><div align="right">Segundo Apellido</div></td>
<td><input name="txtSegApel" type="text" id="txtSegApel" value="<?php echo $VL_SegApe;?>" size="20" maxlength="20" readonly=""></td>
</tr>
<tr>
<td><div align="right">Primer Nombre</div></td>
<td><input name="txtPriNombre" type="text" id="txtPriNombre" value="<?php echo $VL_PriNomb;?>" size="20" maxlength="20" readonly=""></td>
<td><div align="right">Segundo Nombre</div></td>
<td><input name="txtSegNombre" type="text" id="txtSegNombre" value="<?php echo $VL_SegNomb;?>" size="20" maxlength="20" readonly=""></td>
</tr>
<tr>
<td><div align="right">Código Consulta</div></td>
<td><input name="txtCodConsul" type="text" id="txtCodConsul" size="5" maxlength="5" onKeyPress="if(event.keyCode==13){document.form1.txtFechaConsul.focus();}" value="<?php echo $VL_CodConsul;?>"></td>
<td><div align="right">Fecha Consulta</div></td>
<td><input name="txtFechaConsul" type="text" id="txtFechaConsul" value="<?php echo $VL_FechaConsul;?>" size="10" maxlength="10"></td>
</tr>
<tr>
<td><div align="right">Finalidad Consulta</div></td>
<td><input name="txtFinaConsul" type="text" id="txtFinaConsul" size="2" maxlength="2" value="<?php echo $VL_FinConsul;?>"></td>
<td><div align="right">Causa Externa</div></td>
<td><input name="txtCausaExte" type="text" id="txtCausaExte" value="<?php echo $VL_CausaExte;?>" size="2" maxlength="2" ></td>
</tr>
<tr>
<td height="24"> <div align="right">Diagnostico</div></td>
<td><input name="Diagnostico" type="text" id="Diagnostico" onKeyPress="if(event.keyCode==13){'&<?php include "ValidaDiag.php";
if(isset($_REQUEST['Diagnostico'])){
$VP_NombDiag = ValidaDiagnos($_REQUEST['Diagnostico']);
$VL_NombDiag=$VP_NombDiag;}
?>&'; submit(); }" value="<?php echo $Diagnostico;?>" size="4" maxlength="4"></td>
<td><div align="right">Nombre Diagnostico</div></td>
<td><input name="txtNombDiag" type="text" id="txtNombDiag" value="<?php echo $VL_NombDiag;?>" size="30" maxlength="30" readonly=""></td>
</tr>
<tr>
<td><div align="right">Diagnostico Relativo 1</div></td>
<td><input name="Diagnostico1" type="text" id="Diagnostico1" onKeyPress="if(event.keyCode==13){'&<?php include "ValidaDiag1.php";
if(isset($_REQUEST['Diagnostico1'])){
$VP_NombDiag1 = ValidaDiagnos1($_REQUEST['Diagnostico1']);
$VL_NombDiag1 = $VP_NombDiag1;}
?>&'; submit(); }" value="<?php echo $Diagnostico1;?>" size="4" maxlength="4" ></td>
<td><div align="right">Nombre Diagnostico</div></td>
<td><input name="txtNombDiag1" type="text" id="txtNombDiag1" size="30" maxlength="30" value="<?php echo $VL_NombDiag1;?>" readonly=""></td>
</tr>
<tr>
<td><div align="right">Diagnostico Relativo 2</div></td>
<td><input name="Diagnostico2" type="text" id="Diagnostico2" onKeyPress="if(event.keyCode==13){'&<?php include "ValidaDiag2.php";
if(isset($_REQUEST['Diagnostico2'])){
$VP_NombDiag2 = ValidaDiagnos2($_REQUEST['Diagnostico2']);
$VL_NombDiag2=$VP_NombDiag2;}
?>&'; submit(); }" value="<?php echo $Diagnostico2;?>" size="4" maxlength="4"></td>
<td><div align="right">Nombre Diagnostico</div></td>
<td><input name="txtNombDiag2" type="text" id="txtNombDiag2" onKeyPress="if(event.keyCode==13){document.form1.txtTipoDiag.focus();}" value="<?php echo $VL_NombDiag2;?>" size="30" maxlength="30" readonly=""></td>
</tr>
<tr>
<td><div align="right">Tipo Diagnostico</div></td>
<td><input name="txtTipoDiag" type="text" id="txtTipoDiag" value="<?php echo $VL_TipoDiag;?>" size="1" maxlength="1" ></td>
<td><div align="right">Empresa</div></td>
<td><input name="Empresa" type="text" id="Empresa" value="<?php echo $VL_Empresa;?>" size="30" maxlength="30"></td>
</tr>
<tr>
<td><div align="right">
<input name="Oculto" type="hidden" id="Oculto" value="1">
</div></td>
<td>  </td>
<td><div align="right">
<input type="button" name="btnGuardar" value="Guardar" >
</div></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
Agradezco la ayuda que me puedas brindar.
este enlace: http://www.elcodigo.com/cgi-bin/DBread.cgi?tabla=scripts&campo=0&clave=82&info=1
Te dice como moverte en un formulario indicando cual es el próximo campo... así podes ver que pasa... cualquier cosa que encuentre te digo..
Chau
Respuesta

<script language=javascript type=text/javascript>
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
</script>

Éste script desactiva el enter y puede pasar de un campo a otro con TAB

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas