0

i'm trying coding some ajax coding that when user enter their name then click submit, all the data that related with them will be display without refreshing page. But my problem is my code doesn't work(don't show output). can i know what the problem is and maybe give me some solution/example thanks.

below is my code:

<html>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

  <script type="text/javascript">

  $(document).ready(function() {

  $("#display").click(function() {                

     $.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "tesz2.php",             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
        $("#responsecontainer").html(response); 
        //alert(response);
                 }

            });
       });
    });

   </script>

   <body>
   <form method = Post onclick="display">
   <input type ="text" id='name'><br>
   <input type='submit'>
   </form>


   <h3 align="center">Manage Student Details</h3>

   <div id="responsecontainer" align="center"></div>
   </body>

Php File:

  <?php
   include("co.php");
   mysql_select_db("testing",$con);
   $result=mysql_query("select * from Login where user_name='".$_POST['name']."'",$con);

  echo "<table border='1' >
        <tr>
         <td align=center> <b>user Id No</b></td>
         <td align=center><b>Name</b></td>
         <td align=center><b>Password</b></td>
         <td align=center><b>responsibility</b></td></td>";

            while($data = mysql_fetch_row($result))
   {   
         echo "<tr>";
         echo "<td align=center>$data[0]</td>";
         echo "<td align=center>$data[1]</td>";
         echo "<td align=center>$data[2]</td>";
         echo "<td align=center>$data[3]</td>";
              echo "</tr>";
   }
        echo "</table>";
      ?>

co.php is my config.file

5
  • You should try debugging and check what you're getting in the Network tab when you Inspect element (press F12). commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art034 Commented Apr 4, 2017 at 3:57
  • 1
    mysql is obsolete use mysqli or pdo for connecting to database Commented Apr 4, 2017 at 3:59
  • @Rafiq are there any problem with my query, because the output only return blank table not the data. Commented Apr 4, 2017 at 4:16
  • to see that this $_POST['name'] has any value when you post your data Commented Apr 4, 2017 at 4:21
  • @Rafiq can you be more specific. I don't understand. Commented Apr 4, 2017 at 4:39

2 Answers 2

1

in you form you are use onclick method and when call ajax you can use #display as id but it is method so remove your form code and put this code

<form method = Post id="display">
   <input type ="text" id='name'><br>
   <input type='submit'>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

thank you. i will try that. and also are there any problem in my php file?
1

You need to send the name variable to the php page. Your ajax request should look like this:

$.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "tesz2.php",
        data: { name: $('#name').val() }, // set the naem variable
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response); 
            // alert(response);
        }

            });
       });
    });

Edit: You also need to use Dhaval Gohel's answer. There was more than one problem.

Edit: You also should change you .clcik to .submit. That's probably the behavior you're looking for.

Your javascript would look like this:

  <script type="text/javascript">

  $(document).ready(function() {

  $("#display").submit(function(e) {   
         e.preventDefault();

     $.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "tesz2.php",
        data: { name: $('#name').val() }, // set the naem variable             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
        $("#responsecontainer").html(response); 
        //alert(response);
                 }

            });
       });
    });

   </script>

2 Comments

thanks, but may i ask you some question. are there any problem with my php file. I mean at the query. because it only return blank table not the data.
I believe your query should look like this: mysql_query("SELECT * from Login WHERE user_name='$_POST[name]'",$con); However, I haven't tested it. Connecting the way you are though will leave you vulnerable to SQL Injection. You should read this question to fix that.

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.