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

Asked

Viewed 378 times

0

I’m trying to play a value redeemed by php and play in a input with JQuery and then perform a Ubmit on a certain form, what I tried to do is this here:

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

    $Termo = $_POST['Termo'];

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

    ";  

}

The input is named after Termo but nothing happens when receiving the variable Termo I check and it has content.

  • Because there’s a ternary inside the if?

  • Why don’t you echo into the input HTML? instead of setting with Javascript an element that is generated in PHP as well

1 answer

1


@adventist, An echo command is kind of boring from 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 } ?>
  • echo was missing: Document.querySelector('[name='Term']'). value = '<? php echo $Term ?>';

  • No need to echo, and take your shaman test =)

  • 2

    No <php echo $var ? > what will be printed is exactly what is written: value = '" . $Term . " '; but you want it to look like this: value = 'abcdef'; where $_POST['Term'] = "abcdef"

  • 2

    That one $termo will generate a javascript error... You have to produce (print) on the server side, with php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.