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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据