1

I'm trying to find out what's going on with the following code but i can't figure out. I have made an html page, which communicates with a php and a js file. The purpose of my example is to get the data from the server side and display them at the client side. I've made also 2 buttons. One button in order to display me all the entries, and the second button in order to display me a specific entry when a user gives a specific ID.

The problem is that syntactic is OK. It passed all the necessary checks. But it doesn't display me something and unfortunately the console does not have any problem messages. I just want to inform you that i have put these 3 files in this path C:\wamp64\www\PHP and i'm running the selectfrm.html from here: http://127.0.0.1/PHP/selectfrm.html

So, can you help me with this?

selectfrm.html:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
<script type="text/javascript" src="select.js">
</script>
</head>
<body>
<button id="button">Select All </button>
<br>
<input type="text" id="id" />
<button id="button2">Select by ID</button>
<div id="content"></div>
</body>
</html>

select.js

$(document).ready(function(){
    $("#button").click(function(){

        function show_all(){
            $.ajax({
                type: "POST",
                url: "select.php",
                data:{action:"showroom"},
                success: function (data) {
                    $("#id").hide();
                    $("#button2").hide();
                    $("#content").html(data);
                }
            });
        }

        show_all();
    });
});
$(document).ready(function(){
    $("#button2").click(function() {

        function show_selected() {
            var id=$("#id").val();
            $.ajax({
                type: "POST",
                url: "select.php",
                data:{action:id},
                success: function (data) {
                    $("#content").html(data);
                    $("#button").hide();
                }
            });
        }

        show_selected();
    });
});

select.php:

<?php
$link=mysqli_connect("localhost", "root", "", "connection");

if (mysqli_connect_errno())
    echo "Failed to connect to MySQL:" .mysqli_connect_error();

$action=$_POST["action"];
if($action=="showroom") {
    $query = "SELECT * FROM names";
    $show = mysqli_query($link, $query) or die ("Error");
    echo "<table border='2px'><tr><td>name_id</td><td>age</td></tr>";
    while ($row = mysqli_fetch_array($show)) {
        echo "<tr><td>" .$row['name_id'] ."</td><td>" .$row['name'] ."</td><td>" .$row['age'] ."</td></tr>";
    }
    echo "</table>";
}
else{
    $query = "SELECT * FROM names WHERE name_id = '$action'";
    $show = mysqli_query($link, $query) or die ("Error");
    echo "<table border='2px'><tr><td>name_id</td><td>age</td></tr>";
    while ($row = mysqli_fetch_array($show)) {
        echo "<tr><td>" .$row['name_id'] ."</td><td>" .$row['name'] ."</td><td>" .$row['age'] ."</td></tr>";
    }
    echo "</table>";
}
?>
1
  • sidenote: you don't really need that additional wrapper function show_all(){ Commented Aug 24, 2018 at 9:19

1 Answer 1

1

You're missing a </script> closing tag after the following:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">

Some unrelated suggestions:

  • You may want to learn about SQL injection attacks, which your PHP script is prone to. I'd suggest exploring prepared statements,
  • It's always recommended to separate your view code from your logic, so try putting all the resulting HTML from your PHP script to a separate view file, and pass all the necessary data to it. While you're at it, you can make use of PHP's alternate syntax for the view file, which is a bit cleaner.

Something like that:

<table border="2px">
    <tr>
        <td>name_id</td>
        <td>name</td>
        <td>age</td>
    </tr>
    <?php foreach ($names as $name): ?>
    <tr>
        <td><?= $name['name_id'] ?></td>
        <td><?= $name['name'] ?></td>
        <td><?= $name['age'] ?></td>
    </tr>
    <?php endforeach ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.