douna2633 2017-04-08 21:58
浏览 86
已采纳

使用Ajax将PHP中的Echo转换为Div

I have a simple Database on a Server (for Testing). This PHP File is on the Server and works when I open the URL. (http://**.com/search.php?id=abc) Echo gives back "30"

<?php
$pdo = new PDO('mysql:host=*com; dbname=*test1', '*', '*');
$idV = $_GET['id']; 
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = :idV");
$statement->bindParam(':idV', $idV);
$statement->execute();
 while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{ $posV = $row['position']; };
echo $posV;
?>

The HTML is just for Testing

<input type="text" id="txt1">
<button type="button" class="btn btn-info" id= "bt1">Info Button</button>
<div id= "div1"> </div>

I want that when i enter a Code in the Textfield and press the Button, the Echo from the PHP is Displayed in the Div. I know i should use Ajax GET, but i tried so many things and nothing worked. Could you help me pls?

Edit: Last try: https://jsfiddle.net/qz0yn5fx/

    <input type="text" id="txt1">
  <button type="button" class="btn btn-info" id="bt1">Info Button</button>
      <div id="div1">Here </div> 

      <script>
          $(document).ready(function() {

    $("#bt1").click(function() {                
      $.ajax({   
         //create an ajax request to load_page.php
        type: "GET",
        url: "http://**.com/search.php?id=a10 ",             
        dataType: "html",   //expect html to be returned      

        success: function(response){                    
            $("#div1").html(response); 
            alert(response);
        }

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

Better dont look at the next Fiddle i just copied all first Tries:

https://jsfiddle.net/jqv1ecpj/

  • 写回答

1条回答 默认 最新

  • douyoupingji7238 2017-04-09 13:17
    关注

    You could just use a simple POST request instead of a GET request.

      <form id="search" name="search" method="post">
        <input type="text" id="txt1" name="search_input">
      <button type="submit" class="btn btn-info" id="bt1">Info Button</button>
      </form>
        <div id="div1">Here </div>
    
    $(document).ready(function(){
        $("#search").on("submit", function(e){
        e.preventDefault();
        $.post("/search.php", $("#search").serialize(), function(d){
            $("#div1").empty().append(d);
        });
      });
    });
    

    And then in your PHP, (don't forget to use try{}catch{}):

        try {
        $pdo = new PDO('mysql:host=*com; dbname=*test1', '*', '*');
        $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $idV = (isset($_POST['search_input'])) ? $idV = $_POST['search_input'] : exit('The search was empty'); 
        $statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = ?");
        $statement->bindParam(1, $idV);
        $statement->execute();
        foreach($statement -> fetchAll() as $row){
        echo $row['position'];
        }
    
        $pdo = null;
        } catch (PDOException $e) {
        die($e -> getMessage());
        }
    

    I think this should work (haven't tested it). Let me know if it doesn't work and I'll test and correct it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址