1

I'm making a form for a project that when the user insert their username and click submit their info will show in the respected area (with some ajax).The problem is the data show only the column name and the rest are empty. I have insert all the info needed in the table, but still data don't appear. Below is my PHP code:

<?php
require "co.php";
$link = mysqli_connect($h,$u,$p,$db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

$q = "select * from login where User_name ='".$_GET['name']."'";
$result = mysqli_query($link,$q);
if($result){
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>";
}

else{
echo ' Failed';
}
?>

And also i include html 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").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>

<body>
<form method =POST action=test2.php id="display">
<input type ="text" class='z' id='name'><br>
<input type='submit'>
</form>


<h3 align="center">Manage Student Details</h3>
<!--table border="1" align="center">
<tr>
   <td> <input type="button" id="display" value="Display All Data" /> </td>
</tr>
</table-->
<div id="responsecontainer" align="center">

</div>
</body>

Can anybody tell me what the error or maybe some solution. Thanks

7
  • Sidenote: You really quote these <form method =POST action=test2.php. Some browser don't like it. Commented Apr 6, 2017 at 1:33
  • @Fred-ii- so what should i use? Commented Apr 6, 2017 at 1:34
  • I don't understand what you're asking; "use" what? I only gave you a "sidenote", which isn't fully directed at solving your question. If you have errors, you should check for them. Commented Apr 6, 2017 at 1:40
  • 1
    From what I can see is that you're using class='z' yet you're using $('.name') which you may have confused yourself with the id id='name' - those need to match. Either you use the z class by renaming your $('.name') to $('.z') or change your class='z' to id='name' and $('.name') to $('#name'). But I think the best would be to change data: { name: $('.name').val() } to data: { name: $('#name').val() }; try that. My JS is very limited. Commented Apr 6, 2017 at 1:47
  • 1
    Yet here is a problem I know about for sure; being this mysql_fetch_row - That mysql_ function does not work with a mysqli_ connection. You need to use mysqli_fetch_row(). Commented Apr 6, 2017 at 1:49

1 Answer 1

1

Add an additional javascript function displayData() which can be used for displaying all data.

function displayData(){
  $.ajax({
    url: 'tesz2.php',
    type: 'POST',
    data:{
      'perform': true,
    },
    success: function(data){
      $('#responsecontainer').html(data);
    }
  });
}

In your php file you have to do:

if(isset($_POST['perform'])==true){
  //Query........
}

Now add call this function ajax function success.

$.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){
    displayData(); //All data will show                 
    $("#responsecontainer").html(response); 
           }

         });
    });
 });
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.