1

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/

Erik S.
  • 51
  • 1
  • 6

1 Answers1

0

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.

  • when i open the html Document on a server and enter a number then press the button it just refresh the page. Its the same as just pressing the button. Nothing happens. When i open the php direct with ?id=abc it always says "Search empty" my other php file works this way – Erik S. Apr 09 '17 at 17:23
  • Yes. If you use my example, it will only listen for a post request. If you want it to function even if you go to the file directly and append ?id=abc to the address you can just change the short-handed if statement to this: `$idV = (isset($_POST['search_input'])) ? $idV = $_POST['search_input'] : $idV = $_GET['id'];` However, I would not recommend doing this for security reasons. –  Apr 09 '17 at 18:13
  • My fault, i tried it with a local html file and php on server. After uloading the same html file to the server it works. So there is no way to test it local i guess? – Erik S. Apr 09 '17 at 18:59
  • You can test PHP code localy using something called WAMP -> http://www.wampserver.com/en/ . With WAMP you can run a "local server" and test your PHP on there, which also let's you play around in phpmyadmin (MySQL) –  Apr 09 '17 at 19:03
  • I know that but i thought about having the DB and php on the server and the html file local which uses the full url to the php file – Erik S. Apr 09 '17 at 19:22
  • You should be able to that. Are you using WAMP correctly? In your wamp folder there should be a folder called "www". In there you create seperate folders to your projects. Example: "wamp/www/myproject" and in there you add your files (such as index.html and other files). Then in your browser you point to: "http://localhost/myproject". Make sure you have wamp turned on first. Then you should be able to run everything as if it was a live apache-server. –  Apr 09 '17 at 19:29
  • I mean other things, but thats not the Question i was asking at first place so i stop spamming :-) If i have more Questions i will open another Thread or reply here. At this time your Code works but i dont know why *g* – Erik S. Apr 09 '17 at 20:06