php pdo code to search and echo from textbox

419

my form has receipt_no textbox..

I Need to search data on textbox blur event or key down event of textbox.

from this receipt_no textbox.I need to search database and echo values inside another textboxes...

please help...

<?php
function  myFunction()
{
if(isset($_POST['receipt_no']))
{       
    $receipt_no = $_POST['receipt_no']; 
    $result = $database->getRow("SELECT receipt_no,scheme_name FROM scheme_master where receipt_no = :receipt_no",array(':receipt_no'=>$receipt_no));
    if($result!=0)
    {   
        while ($row = $result->fetchObject())
         {
          echo $row->scheme_name;
         }
    }
}
}
?>
<form action="" method="post">
    <input type="text"  name="receipt_no" onblur="myFunction()" />
</form>

echo value inside this textbox---

<input type="text" value="<?=$scheme_name;?>" name="scheme" class="field size2" />
474

Answer

Solution:

Right two methods, PHP only and AJAX javascript as some have mentioned.

First PHP only:

<? $db=new PDO();
if($_POST&&isset($_POST['submit'])){
    $result=$db->prepare('SELECT receipt_no,scheme_name FROM scheme_master WHERE receipt_no=:receipt_no');//not sure why you're entering an array?
    $result->bindParam(':receipt_no',$_POST['scheme'],PDO::PARAMETER);
    $result->execute();
    while($row=$result->fetch(PDO::FETCH_ASSOC)){
        echo$row['scheme_name'];
    }
}?>
<!--Form next-->
<form name='findReceipt' action='' method="POST">
    <input name='scheme' type='text' value='<?echo$scheme_name;?>'/>
    <input name='submit' type='submit' value='Get'/>
</form>

You don't need the attributeonblur attribute for this method. This is a 'self' operating page method.

The next method is javascript method where theonblur attribute can be used:

myFunction(){
    var xmlhttp=XMLHttpRequest();
    xmlhttp.open('POST','getResult.php',false);
    xmlhttp.send("receipt_no="+this.value);//if this doesn't work, use the following line
    //document.getElementByName('scheme').value
    document.getElementById('resultHere').innerHTML=xmlhttp.responseText;
}

The above is a VERY basic function, it will not work in IE5-6 and may require some header data etc. And! For those that love the silly library jQuery

$('input[name="scheme"]').blur(function(){
    var self=this;
    $.ajax({
        type:'POST',
        data:{receipt_no:$(self).value()},
        beforeSend:function(xhr){
            if($(self).value().length<5)xhr.abort();
        },
        success:function(result){
            $('#result').html(result);
        }
    });
}

Some may argue to use aGET method for this opposed toPOST but I prefer POST overall.

People are also looking for solutions to the problem: php - Codeigniter: Order by ascending date

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.