Click here to Skip to main content
15,905,616 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's the problem, I want to search the DB based on user inputs.
so i am not using POST or GET, instead i used on click...
i posted below my code.
can you please guide me on how to do it in correct way.
thanks in advance.
the use of this form is for client information for edit purposes.
I put a value 2 to the search criteria just to show you that it is a running program. but i want the system to search the value based on user inputs.

your help is highly appreciated.

i don't know how to use the "p1.val()" value in sql search.

What I have tried:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
	    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
        <title>sample b</title>
    </head>
    <body>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
        <div class="container">
            <div class="form-row">
                <div class="form-group col-md-3">
                    <input type="text" class="form-control" placeholder="Last Name" name="last_name" id ="last_name" value=''>
                </div> 
                <div class="form-group col-md-3">
                    <input type="text" class="form-control" placeholder="Last Name" name="first_name" id ="first_name" value=''>
                </div>
                <div class="form-group col-md-3">
                    <input type="text" class="form-control" placeholder="Middle Name" name="middle_name" id="middle_name">
                </div>
            </div>
           
            <div class="form-group col-md-3" id="rslt">
                <input type="text" name="id" placeholder="Enter ID" value = '' />
                <button id='go' name="search" class=''>GO</button>
            </div>
           
        </div>
       
        <script>
            document.getElementById('go').onclick = function () {

                var p1 = $('#id'); 
                p1.val();
                
              <?php
                $connection = mysqli_connect("localhost","root","admin");
                $db = mysqli_select_db($connection,'test');

                $query = "Select * from tblnames where id = '2' ";
                $query_run = mysqli_query($connection,$query);
                $ln = "";
                $fn = "";
                $mi = "";
                while($row = mysqli_fetch_array($query_run))
                {
                    $ln=$row['lastname'];
                    $fn=$row['firstname'];
                    $mi=$row['miname'];
                }
               ?>
                var last_name=document.getElementById("last_name").value = "<?php echo $ln; ?>";
                var first_name=document.getElementById("first_name").value = "<?php echo $fn; ?>";
                var first_name=document.getElementById("middle_name").value = "<?php echo $mi; ?>";
            };
        </script>
    </body>
</html>
Posted
Updated 23-Jan-20 3:25am

The key to your problem is to understand the difference between client and server side scripting.

Client side is the web page the end user sees and interacts with. It contains your rendered HTML, JavaScript, and CSS.
PHP does not exist on the client side per se, only has affects of it.
PHP runs server side, where for the most part HTML, JS, & CSS do not exist.

So what we need to do is to get content entered into the client (HTML) and get that to the server (PHP).
It sounds like you want to do this via JavaScript; so the way to do that is going to be via AJAX; which can take the data from your web page, send it to the server, and get the response.

The best thing that you can do for yourself is to go through some tutorials on the subject; I googled "php ajax tutorial" and found this example on W3 Schools:
PHP AJAX and MySQL[^]
 
Share this answer
 
PHP (scripts) are run on the server. When it finally gets to your client (user) the PHP is gone from the page. What's left is the results of the php being run on the server. Thus, you CAN NOT set a php data value on the client since there is no mechanism on the client to do anything with it. Or PHP function, etc. PHP is also your gateway into SQL, MySQL, etc. - they reside on the server side.

JAVASCRIPT is run on the client (optionally - it can be disabled!). Simply put, it gives your page, as rendered on the client, functionality. In particular, it's useful for handling events (such as content of a drop-list being changed). It can also be involved in your page setup and operation - such as a refresh timer or data validation.

So, the PHP never gets to the client and the client doesn't understand PHP. Quite the dilemma for you and your problem.

There are two common ways to get your data from the client to the server.
1) Use web forms[^]. They can call scripts, and ultimately target a server-side page that can be sent the data in your form. Note that your page will be refreshed.
2) Use AJAX[^]. This is a javaScript that has functionality that sends information to the server - to a PHP file, which can then handle it and do what you wish with it. It does not refresh the current page - but better yet - it can update content on the current page without the refresh.

Sending data between the two takes some getting used to.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900