简体   繁体   中英

How can I have MySQL and JS add numerous users into a html div and be able to delete them?

在此处输入图片说明 The program below lets the user click on a name in the drop down list and once the user clicks a name AJAX uses a PHP script to grab the user info from MySQL DB and displays it under the drop down...

However info about one person only can be displayed at once...How can I let the user add as many people as they want by clicking the names in drop down? I dont want the users that have been chosen already to show up in the list but I want the user to be able to delete people they have chosen and then the deleted user should appear in drop down list...There is a picture of how it works, but what I am trying to do is when a user clicks a name it adds that person's name and info into a div, then when I choose another person it adds that person into the div as well. People in the div should not show up in drop down list unless they are deleted..

HTML part

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html> 

PHP FILE:

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

JQuery may help you. Once you got it, you will be able to do something like this:



var removed_option;

$("select option").click(function(){
    removed_option = $(this); // saves option in var, will use it later to put it back to select
    $(this).remove(); // removes option from select
    var data = {};
    data.selected_person = $(this).val();
    $.post("ajax.php", data, function(returned_data){ // make ajax call
        $("#txtHint").html(returned_data); // put data from ajax into #txtHint
    });
});

You can add a column with button, which will remove the table and put back removed option:


$("#remove_table").click(function(){
    $(this).parents("table").remove(); // searches the DOM for button's parents which are tables and removes them
    $("select").append(removed_option); // adds to your select previously removed, but saved, option
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM