簡體   English   中英

使用javascript var用php mysql查詢數據

[英]using javascript var to query data with php mysql

我無法找到具有此條件的任何解決方案,當id用戶文本值更改時,名稱和地址將被填充。 但它在php查詢中返回一些錯誤。

對不起,如果這個帖子重復,因為我無法找到符合我的條件的解決方案。

這里是html表單

    <form id="frm_add_bill" name="frm_add_bill" >

<label >id user</label>                                          
<input type="text" onchange="getplg()"  id="kdp" name="kdp">                    
<label >user name</label>

<input type="text" maxlength="25" name="name" id="name"  readonly>

<label >user address</label>

<input type="text" name="address" id="address"  readonly>
</form>

這里有javascript和php代碼

<script type="text/javascript">  function getplg(){

                var kdpe = $('#kdp').value;

            $.ajax({
                type: 'post',
                url: '',
                data: kdpe,
                timeout: 50000
            });

            }</script>

            <?php
            if (isset($_POST['kdpe'])) {

                $kpde=htmlspecialchars($_POST['kdpe']);


            $amxz=mysql_query("SELECT name, address from tbl_user where id_user='$kpde'");
            $camqz=mysql_fetch_array($amxz);

            echo "<script>document.write(fillem());</script>";

            }
            ?>

            <script type="text/javascript">
            function fillem(){

            document.frm_add_bill.name.value=<?php echo $camqz['0'];?>;
            document.frm_add_bill.address.value=<?php echo $camqz['1'];?>;

            }
            </script>
function getplg(){
        try{
            var kdpe = $('#kdp').val();
            console.log("here");
            $.ajax({
                method: 'post',
                url: '',
                data: {val:kdpe}}).done(function(data){
                    console.log(data);
                    console.log("hello");
            });
            }catch(Exception){
                alert("error");
            }
        }
    </script>

嘗試這個。

首先將您的ajax功能更改為:

function getplg()
{ 
    var kdpe = $('#kdp').val(); 
    $.ajax({ 
    type: 'post', 
    url: '', 
    data: "kdpe="+kdpe, 
    timeout: 50000 
    }); 
}

在這里你需要使用param作為"kdpe="+kdpe傳遞。

並在if (isset($_POST['kdpe']))移動fillem()

<?php 
if (isset($_POST['kdpe'])) {
$kpde=htmlspecialchars($_POST['kdpe']);

$amxz=mysql_query("SELECT name, address from tbl_user where id_user='$kpde'");

$camqz=mysql_fetch_array($amxz);

echo "<script>document.write(fillem());</script>"; 
?>

<script>
function fillem(){
   document.frm_add_bill.name.value=<?php echo $camqz['0'];?>;
   document.frm_add_bill.address.value=<?php echo $camqz['1'];?>; 
}
</script>
<?php
} 
?>

邊注:

停止使用mysql_*因為它已棄用並在PHP 7中關閉。使用mysqli_*PDO

您應該傳遞這種格式的數據:

$.ajax({name:value, name:value, ... })

我找到了這個案子的答案

這個js代碼使用內部標記

<script type="text/javascript">

        $(document).ready(function()
        {

         $("#kdp").blur(function() {


          var idkategori = $(this).val();
          if (idkategori != "")
          {

           $.ajax({
            type:"post",
            url:"getsubkat.php",
            data:"id="+ idkategori,
            success: function(data){
             $("#name").html(data);
            }
           });
          }
         });
        });
        </script>

和這個HTML

<div class="form-group">
  <label for="inputEmail3" class="col-sm-2 control-label">
    User Code
  </label>
  <div class="col-sm-10">
    <input type="text" class="form-control" id="kpd" name="kdp"  >
  </div>
</div>
<div id="name">

</div>

和PHP代碼是不同的頁面與js和html(getsubkat.php)

    <?php
include ("components/inc/connection.php");
$id=$_POST['id'];
$query=mysql_query("SELECT name, address from tbl_user where user_code='".$id."'");

 $data=mysql_fetch_array($query);

echo"<div class='form-group'>";
    echo"<label for='inputEmail3' class='col-sm-2 control-label'>user name </label>";
echo"<div class='col-sm-10'>";
    echo"<input type='text' class='form-control' value='$data[user_name]'  readonly>";
echo"</div>";
echo"</div>";

echo"<div class='form-group'>";
    echo"<label for='inputEmail3' class='col-sm-2 control-label'>user address</label>";
echo"<div class='col-sm-10'>";
    echo"<input type='text' class='form-control' value='$data[user_address]'  readonly>";
echo"</div>";
echo"</div>";

?>

您必須使用onkeydownonkeyuponpasteoninput事件而不是onchange事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM