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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀