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";
}


} 
?>

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.

In your case, you're not passing a variable from PHP to JS, you're making an AJAX call to a PHP file asking for JSON data.

function submitForm() {
    $.ajax({
        type:'POST', 
        url: 'formtronco.php', 
        data:$('#troncocomun').serialize(),
        success: function(data) {
            // If you're querying by ID, no need to search within another node
            $('#aqui').html(data.val1);
            $('#alla').html(data.val2); 
        }
    });
});

And in formtronco.php

<?php
echo json_encode(array(
    'val1' => 'Some string',
    'val2' => 'Another string'
));
?>

To pass a variable to JS from PHP (what you actually asked) do the following

<script>
var errorMessage = <?php echo json_encode($errorMsgPhpVar); ?>;
alert(errorMessage);
</script>

json_encode will make it work even for strings with quotes and embedded new lines, besides working for PHP objects.

Another way to do this is to create an element that holds your variable when the page loads, for example, make a hidden input field

<input class="myVar" type="hidden" value="<?= $phpMyVar ?>">

Then using Javascript, you can select that hidden input by the ID and get the value from it.

(this is jquery)

var myVal = $('.myVar').val();

This way you can access the variable at any time and even place your javascript wherever you want.