How to send php variables to the html page?

Asked

Viewed 663 times

-1

I’m creating a testing page to see if I can use, learn and then pass this on to a larger project.

My problem is this: create the submission form in HTML, do the BD search with PHP, but I can’t return the result of the PHP search to HTML

HTML:

<html>
<head>
  <meta charset="utf-8" />
  <title>Teste</title>
</head>

<body>
    <form action="teste.php ">
        <input type="submit" name="Pesquisar"/>
    </form>
</body>
</html>

PHP

<?php 
    $conexao = mysqli_connect('localhost','root','','login_sessao');

    $consulta = mysqli_query($conexao, "SELECT usu_Senha FROM tb_usuario where usu_Email = 'emailQualquer'");

    $consultaQ = mysqli_fetch_assoc($consulta);
    echo "Sua senha é: " + $consulta['usu_Senha'];
?>

I’m sorry if I’m formatted wrong, first time asking a question, but thanks in advance.

2 answers

1


The concatenation operator of strings in the PHP is the dot: . !

Take an example:

<?php
$string = 'texto qualquer';

echo 'Este é um ' + $string; // Exibe "0"
echo 'Este é um ' . $string; // Exibe "Este é um texto qualquer"

Recommended reading: PHP - String Operators

If this doesn’t solve your problem, post the output of your query (warnings, errors, etc from PHP).

  • got it, and now to be able to send this variable $string to html page? , I tried to put in the value of a <tag> the following: value = <?php echo $string; ?> but it wasn’t

  • value="<?php echo $string; ?>" - correcting

  • The second comment should work. Remember that php is usually interpreted only on pages with the .php. extension Save configuration cases for other extensions...

  • this second comment is inside an html page, it may be that the reason is not returning any results?

  • Just change and test, Uai. It’ll be on the fly.

  • Damn, what a silly mistake, I managed to solve now, thank you

Show 1 more comment

1

all good! Next, there are many ways you can get PHP data into HTML! The simplest way is to create the query in the file itself and then "draw" the table receiving the array that comes from the Database! Another way would be to use query by ajax method. In the ajax method you trigger the query for a link and this link you return already the table mounted and points to a DIV.

In the code below I use Javascript with Ajax, I point out what is my variable of the value type, I point out what would be my php page and return the data in div #data. Within the search.php file I create the query in php with the mysql database and then create a table in html showing the query data

function buscaTipo(){

    var valorTipo = document.getElementById('tipo').value;
    var page = "buscateste.php";

    if(valorTipo !=''){
      $.ajax ({
        type: 'POST',
        dataType: 'html',
        url: page, beforeSend: function(){
          $("#dados").html("Carregando...");
        },
        data: {valorTipo: valorTipo,
          opcao: 1},
        success: function(msg)
        {
          $("#dados").html(msg);
        }
      })
    }else{
      $.post('buscateste.php', function(retorna){
        $("#dados").html(retorna);
    });

}}

Browser other questions tagged

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