doutangguan2460 2014-03-20 20:27
浏览 23

Ajax调用PHP

I'm working in a project which I have to use several inputs which have to be completed by the client inserting a number. If the number is equal or more than 6, 'aprobada' should appear in the input text, else 'reprobada'. Is it possible to pass variables from php to jquery?

All comments and feedback are welcome :)

Thank you!

CODE JQUERY ,HTML


<script src="jquery-2.1.0.js" type="text/javascript"></script>   
<script>
function submitForm() {
$.ajax({type:'POST', url: 'formtronco.php', data:$('#troncocomun').serialize(),
success:    function(response) {


$('#troncocomun').find('#aqui').html(response);  /*here would be this variable  
'$acreditacion[0]' from php to respond in '#aqui'*/   
$('#troncocomun').find('#alla').html(response); /*here would be this variable    
'$acreditacion[1]' from php to respond in '#alla'*/ 
}});


return false;
}

</script>



....
<!-- PRIMER SEMESTRE -->
<div class="nivel">NIVEL I</div>
<form class="creditos" onsubmit="return submitForm();"  name="troncocomun" 
id="troncocomun" method="post" >
<div class="semestre1" >
<table >
<tr>
<td >
UNIDADES DE APRENDIZAJE
</td>
<td>
VALOR EN CRÉDITOS
</td>
<td >
CALIFICACIÓN 
</td>
<td >
ACREDITACIÓN
</td>
<td >
CARGA ACADÉMICA
</td>
</tr>
<tr>
<td  nowrap id="materia">
ALGEBRA
</td>
<td>
5.62
</td>
<td>
<input type="text" id="1" name="1" size="3px">

</td>
<td id="aqui" name="aqui">

</td>
<td>
Row 1
</td>
</tr>
<tr>
<td nowrap id="materia">
COMPUTACION BASICA I
</td>
<td>
4.5
</td>
<td>
<input type="text" id="2" name="2" size="3px">
</td>
<td id="alla" name="alla">   
</td>
<td >
Row 2
</td>
</tr>
<tr>
</table>
<input type="submit" name="enviar" id="enviar" value="Procesar" />          

</form>



CODE PHP:


<?php

if(isset($_POST['1'])) 
{
$acreditacion=array($_POST['1'],$_POST['2']);
if ($acreditacion[0] >=6 ) {
echo "Aprobada";}
else{
echo "Reprobada";
}

if ($acreditacion[1] >=6 ) {
echo "Aprobada";}
else{
echo "Reprobada";
}


} 
?>
  • 写回答

3条回答 默认 最新

  • dongqiangteng7319 2014-03-20 20:31
    关注

    Not the tidiest thing, really. But this answer your question:

    <script>
    var someVariable = <?php echo $phpVar; ?>;
    </script>
    

    Assuming $phpVar = 1, this'd output:

    <script>
    var someVariable = 1;
    </script>
    

    Make sure you include this BEFORE other javascript that references this variable.

    You shouldn't be doing this though, since it's very untidy - you should't actually be needing to "pass variables" like that.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值