<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Campo de busca com php e javascript</title>
        <description>Pessoal tenho o seguinte código abaixo:

Preciso que ao selecionar o produto( depois da procura no banco), ele preenchesse os demais campos com os dados vindos da consulta do banco.


&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
$(document).ready(function(){
    $('.search-cod input[type=&amp;quot;text&amp;quot;]').on(&amp;quot;keyup input&amp;quot;, function(){
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(&amp;quot;.resultcod&amp;quot;);
        if(inputVal.length){
            $.get(&amp;quot;codigo-search.php&amp;quot;, {term: inputVal}).done(function(data){
                // Display the returned data in browser
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });
    
    // Set search input value on click of result item
    $(document).on(&amp;quot;click&amp;quot;, &amp;quot;.resultcod p&amp;quot;, function(){
        $(this).parents(&amp;quot;.search-cod&amp;quot;).find('input[type=&amp;quot;text&amp;quot;]').val($(this).text());
        $(this).parent(&amp;quot;.resultcod&amp;quot;).empty();
    });
});

&amp;lt;/script&amp;gt;

 

&amp;lt;?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect(&amp;quot;localhost&amp;quot;, &amp;quot;root&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;table&amp;quot;);
 
// Check connection
if($link === false){
    die(&amp;quot;ERROR: Could not connect. &amp;quot; . mysqli_connect_error());
}
 
if(isset($_REQUEST[&amp;quot;term&amp;quot;])){
    // Prepare a select statement
    $sql = &amp;quot;SELECT * FROM produtos WHERE ean LIKE ?&amp;quot;; 
    
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, &amp;quot;s&amp;quot;, $param_term);
        
        // Set parameters
        $param_term = $_REQUEST[&amp;quot;term&amp;quot;] . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(mysqli_num_rows($result) &amp;gt; 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo &amp;quot;&amp;lt;p&amp;gt;&amp;quot; . $row[&amp;quot;ean&amp;quot;] . &amp;quot;&amp;lt;/p&amp;gt;&amp;quot;;
                    
                }
            } else{
                echo &amp;quot;&amp;lt;p&amp;gt;No matches found&amp;lt;/p&amp;gt;&amp;quot;;
            }
        } else{
            echo &amp;quot;ERROR: Could not able to execute $sql. &amp;quot; . mysqli_error($link);
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}
 
// close connection
mysqli_close($link);
?&amp;gt;

 

Formulario Html

&amp;lt;div class=&amp;quot;search-cod&amp;quot;&amp;gt;
            &amp;lt;label for=&amp;quot;nome&amp;quot; &amp;gt;Codigo&amp;lt;/label&amp;gt;
            &amp;lt;input class=&amp;quot;search-cod&amp;quot; type=&amp;quot;text&amp;quot; for=&amp;quot;nome&amp;quot; size=&amp;quot;10&amp;quot; autocomplete=&amp;quot;off&amp;quot; placeholder=&amp;quot;Procura Codigo...&amp;quot; /&amp;gt;
            &amp;lt;div class=&amp;quot;resultcod&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;br&amp;gt;

 

Aqui precisava o input do nome e abaixo o input da referencia,
estes dois (&amp;quot;nome&amp;quot; e &amp;quot;fererencia&amp;quot; veem do banco.

 

Desde já, agradeço a todos.</description>
        <link>/phorum/read.php?3,194669,194669#msg-194669</link>
        <lastBuildDate>Mon, 07 Sep 2026 18:06:08 +0000</lastBuildDate>
        <generator>Phorum 5.2.10</generator>
        <item>
            <guid>/phorum/read.php?3,194669,194669#msg-194669</guid>
            <title>Campo de busca com php e javascript</title>
            <link>/phorum/read.php?3,194669,194669#msg-194669</link>
            <description><![CDATA[Pessoal tenho o seguinte código abaixo:<br />
<br />
Preciso que ao selecionar o produto( depois da procura no banco), ele preenchesse os demais campos com os dados vindos da consulta do banco.<br />
<br />
<br />
&lt;script type=&quot;text/javascript&quot;&gt;<br />
$(document).ready(function(){<br />
    $('.search-cod input[type=&quot;text&quot;]').on(&quot;keyup input&quot;, function(){<br />
        /* Get input value on change */<br />
        var inputVal = $(this).val();<br />
        var resultDropdown = $(this).siblings(&quot;.resultcod&quot;);<br />
        if(inputVal.length){<br />
            $.get(&quot;codigo-search.php&quot;, {term: inputVal}).done(function(data){<br />
                // Display the returned data in browser<br />
                resultDropdown.html(data);<br />
            });<br />
        } else{<br />
            resultDropdown.empty();<br />
        }<br />
    });<br />
    <br />
    // Set search input value on click of result item<br />
    $(document).on(&quot;click&quot;, &quot;.resultcod p&quot;, function(){<br />
        $(this).parents(&quot;.search-cod&quot;).find('input[type=&quot;text&quot;]').val($(this).text());<br />
        $(this).parent(&quot;.resultcod&quot;).empty();<br />
    });<br />
});<br />
<br />
&lt;/script&gt;<br />
<br />
 <br />
<br />
&lt;?php<br />
/* Attempt MySQL server connection. Assuming you are running MySQL<br />
server with default setting (user 'root' with no password) */<br />
$link = mysqli_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;table&quot;);<br />
 <br />
// Check connection<br />
if($link === false){<br />
    die(&quot;ERROR: Could not connect. &quot; . mysqli_connect_error());<br />
}<br />
 <br />
if(isset($_REQUEST[&quot;term&quot;])){<br />
    // Prepare a select statement<br />
    $sql = &quot;SELECT * FROM produtos WHERE ean LIKE ?&quot;; <br />
    <br />
    if($stmt = mysqli_prepare($link, $sql)){<br />
        // Bind variables to the prepared statement as parameters<br />
        mysqli_stmt_bind_param($stmt, &quot;s&quot;, $param_term);<br />
        <br />
        // Set parameters<br />
        $param_term = $_REQUEST[&quot;term&quot;] . '%';<br />
        <br />
        // Attempt to execute the prepared statement<br />
        if(mysqli_stmt_execute($stmt)){<br />
            $result = mysqli_stmt_get_result($stmt);<br />
            <br />
            // Check number of rows in the result set<br />
            if(mysqli_num_rows($result) &gt; 0){<br />
                // Fetch result rows as an associative array<br />
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){<br />
                    echo &quot;&lt;p&gt;&quot; . $row[&quot;ean&quot;] . &quot;&lt;/p&gt;&quot;;<br />
                    <br />
                }<br />
            } else{<br />
                echo &quot;&lt;p&gt;No matches found&lt;/p&gt;&quot;;<br />
            }<br />
        } else{<br />
            echo &quot;ERROR: Could not able to execute $sql. &quot; . mysqli_error($link);<br />
        }<br />
    }<br />
     <br />
    // Close statement<br />
    mysqli_stmt_close($stmt);<br />
}<br />
 <br />
// close connection<br />
mysqli_close($link);<br />
?&gt;<br />
<br />
 <br />
<br />
Formulario Html<br />
<br />
&lt;div class=&quot;search-cod&quot;&gt;<br />
            &lt;label for=&quot;nome&quot; &gt;Codigo&lt;/label&gt;<br />
            &lt;input class=&quot;search-cod&quot; type=&quot;text&quot; for=&quot;nome&quot; size=&quot;10&quot; autocomplete=&quot;off&quot; placeholder=&quot;Procura Codigo...&quot; /&gt;<br />
            &lt;div class=&quot;resultcod&quot;&gt;&lt;/div&gt;<br />
            &lt;/div&gt;<br />
            &lt;br&gt;<br />
<br />
 <br />
<br />
Aqui precisava o input do nome e abaixo o input da referencia,<br />
estes dois (&quot;nome&quot; e &quot;fererencia&quot; veem do banco.<br />
<br />
 <br />
<br />
Desde já, agradeço a todos.]]></description>
            <dc:creator>Fernando</dc:creator>
            <category>Perguntas Variadas / Não Relacionadas ao PHP</category>
            <pubDate>Thu, 04 Oct 2018 18:53:46 +0000</pubDate>
        </item>
    </channel>
</rss>
