How to take the value of a php variable and pass in a java script input

Asked

Viewed 971 times

0

I have a form where the credit card data is filled out and displayed on the card!

            <div class="row">
                <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">


                    <?= $form->field($formPagamento, 'name')->textInput(); ?>


                </div>
            </div>


 //Name Input
            $(".inputname").keyup(function(){
                $(".fullname").text($(this).val());
                if($(".inputname").val().length === 0){
                    $(".fullname").text("Nome completo");
                }
                return event.charCode;
            }).focus(function(){
                $(".fullname").css("color", "white");
            });

2 answers

1

If you’re already getting the value of the PHP variable by loading the page, and you want to put its value in an HTML input, you can do something like:

<input type="text" value="<?=$valor?>" />

Now, if you want to take the value of a PHP variable and pass it to the javascript code recommend that you use a request for the PHP script, using Axios, jquery, or something like.

Then you would have a PHP file.

pegaValorDaVariable.php

<?php 
//Faz algum calculo ou consulta no BD
echo $minhaVariavel;

And with javascript you would:

axios.get("pegaValorDaVariavel.php").then(function(response) {
    variavelJS = response.data
    //Faz outras coisas com a variavel...
})

With jQuery use ajax https://api.jquery.com/jquery.ajax/

  • Then the value shown in the input I want to appear in the card image

  • Take a look here, would it be like this? https://jsfiddle.net/sngbrdtk/

  • that’s right, so that’s the following hi form ja and auto generated by Yii, that part <?= $form->field($formPagamento, 'name')->textInput(); ? > dai as put to ?

  • Well, it generates an HTML with input from that command, right? You need to get the value of that input from jQuery, right? Then you can take this value by the input name, in case you are passing 'name', then it would be $("input[name='name']"). val()?

0

You can do it like this:

<script> var meusdados = '<?php echo $minhavariavle; ?>'; </script>

Browser other questions tagged

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