How can I assign value of a php variable to an input with JQuery

0

I'm trying to play a value retrieved by php and play on a input with JQuery and then make a submit on a certain form, what I tried to do is here:

if (!empty($_POST['Termo'])) {

    $Termo = $_POST['Termo'];

    echo "
    
    jQuery(function($) {
        document.querySelector('[name='Termo']').value = '" . $Termo . "';  
        document.forms['frmBusca'].submit();
    }); 
    

    ";  

}

The input has the name of Termo but nothing happens, when I get the variable Termo I check it and it has content.

    
asked by anonymous 02.08.2016 / 21:20

1 answer

1

@adventistapr , An echo command is a bit of annoying php. Try different and follow below:

<?php
if (isset($_POST['Termo'])) {
    $Termo = $_POST['Termo'];
?>
jQuery(function($) {
        document.querySelector('[name='Termo']').value = '" . $Termo . "';  
        document.forms['frmBusca'].submit();
});
<?php } ?>
    
02.08.2016 / 21:42