简体   繁体   中英

Get the Echo from PHP into a Div with Ajax

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM