How to list and consult contacts with PHP and Javascript? [closed]

0

Hello, I need a very practical PHP example that brings me the contact list from a database table, and click on a contact to be displayed the information of it. Follow the image below:

I'm a beginner in PHP, I tried to use the query for AJAX, but I did not succeed. I tried to search in many places for an example that could help me with the question, but I did not find anything either.

I would like to accomplish this without refreshing the screen or redirecting to another page (Javascript).

I'm counting on your help, thank you!

    
asked by anonymous 04.06.2015 / 18:43

2 answers

1

So try HTML FILE

<html>
<title>teste</title>
<head>
    <!-- CSS aqui -->
</head>
<body>

    <h1>Consultar</h1>

    <p>Clique no botao</p>
    <p><button class='doQuery' data-userid='1'>Marco</button></p>
    <p><button class='doQuery' data-userid='2'>Alvori</button></p>
    <p><button class='doQuery' data-userid='3'>Cintia</button></p>
    <p><button class='doQuery' data-userid='4'>Jair</button></p>

    <hr>
    <div id='resultadofinal'></div>

<!-- JS aqui no final da pagina para carregar mais rapido -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><script>$(function(){$.system={};$.system.path='/stackoverflow/exemplo1/';//oucolocarapastaraiz$.system.path='/';jQuery(".doQuery").click(function(){

        var id_usuario = $(this).attr('data-userid');

        jQuery.post( $.system.path + 'result.php' , { id_usuario:id_usuario,outraval:'valor_da_val' } , function(result,status){ 


            if( status == 'success' )
            {

                var json = jQuery.parseJSON(result);

                if( json.resultStatus == 'success' )
                {

                    jQuery("#resultadofinal").html(json.html);

                }
                else
                {
                    jQuery("#resultadofinal").html(json.resultMSG);

                }

            } 
            else
            {
                alert('Erro no requisicao jquery');
            }


        } );
    });
});
</script>

</body>
</html>

FILE result.php - make your sql as per your bank here I took xampp example

<?php 

$con   = mysql_connect('localhost','root','');
$db    = mysql_select_db('webauth',$con);


if( isset($_POST['id_usuario'])){

    // input    
    $id    = $_POST['id_usuario'];

    ####
    $html  = array();
    $table = 'user_pwd';

    // your SQL


    //Query
    $sql = mysql_query("SELECT * FROM {$table} WHERE id = '{$id}'",$con);

    if( mysql_error() )
    {
        $html['resultStatus'] = 'error';    
        $html['resultMSG']    = mysql_error();
    } 
    else
    {

        if( mysql_num_rows($sql) >= 1 )
        {

            $html['resultStatus'] = 'success';      

            $html['html'] = "
                <table>
                <tr>
                    <td>Nome</td>
                    <td>Senha</td>
                </tr>
            ";

            while( $row = mysql_fetch_assoc($sql) )
            {

                //aqui voce define os dados da sua table
                $html['html'] .= "
                    <tr>
                        <td>{$row['name']}</td>
                        <td>{$row['pass']}</td>
                    </tr>

                ";

            }

            $html['html'] .= "</table>";

        } 
        else
        {
            $html['resultStatus'] = 'error';        
            $html['resultMSG'] = 'Nenhum resultado';                    
        }

    }



    // output
    echo json_encode($html);


}
?>
    
05.06.2015 / 04:38
-1

I think with method get it's easy ...

Each contact of that list is a link that goes to the same page only with an extension like this (I'm based on the url that appears in the image):

  

test.php? contact = contact_name

<?php 

// se existir o contato na url...
if(isset($_GET['contato'])){

$contato = $_GET['contato'];

//seleciona todos os dados daquele contato
$selecionaContato = mysql_query("SELECT * FROM contato WHERE nomeContato='$contato'");
while($dadosContato = mysql_fetch_array(selecionaContato)){   
//mostre abaixo todos os dados do usuário selecionado
?>

 <p>Nome Completo: <?php echo $dadosContato['nome'];?></p>
 <p>telefone: <?php echo $dadosContato['tel'];?></p>
 <p>Endereço: <?php echo $dadosContato['endereco'];?></p>


  <?php }
  // fecha o while
  // se nao existir o contato na url mostra o texto abaixo..

   }else{ ?>


  <p>Selecione um contato no menu a esquerda</p>


  <?php } // fecha o else ?>

This is a simple method, if you need more security, I recommend the method post

If you did not create a slug on your table I advise you to check by the id of each contact .. type this ...

  

test.php? idContact = 1

$id = $_GET['idContato'];
// force para que a variavel id seja um numero para evitar hachers
$id = (int)$id;
//seleciona todos os dados daquele contato pelo id
$selecionaContato = mysql_query("SELECT * FROM contato WHERE     idContato='$id'");

then the rest is the same ...

    
04.06.2015 / 20:49