Hello Everyone,
I am a bit new at programming so please go lightly on me I am trying to make an auto complete textbox from a mysql table called form test with two fields, id and name, and have had success. Herein lies the problem. I would like to be able to edit that field with the autocomplete information as well. The edit form works however, it does not show any autocomplete suggestions like I would have hoped for.
I will post the code and files below. Any help would be greatly appreciated. Also before I get flamed on no injection protection I understand that, I am just trying to get the basics working before adding protection.
Thank you all very much!
Here is my testadd.php form
<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function(){
$("#name").autocomplete({
source:'testgetautocomplete.php',
minLength:1
});
});
</script>
</head>
<!----- Start Add Form ------>
<?php
include_once('db.php');
if(isset($_POST['name'])
)
{
$name = $_POST['name'];
?>
<?php
if(mysql_query("INSERT INTO formtest VALUES('','$name')"))
echo "Boat type $name Created and Added Successfully!";
else
echo "Oops! There was a problem. Please try again. If you have seen this error more than once please call UYS Support and tell them: ".mysql_error();
}
?>
<?php
$res = mysql_query("SELECT * FROM formtest ORDER BY name");
?>
<form action="testadd.php" method="POST">
<br />
<br />
<br />
<br />
<h1>Add a New User</h1>
<table class="center">
<tr>
<td>Enter a New Name: </td><td><input type="text" id="name" name="name" /></td>
</tr>
<tr>
<td></td><td><input type="submit" value=" Add >>> "/></td>
</tr>
</table>
</form>
<!----- End Add Form ------>
<!----- Start Table List ------>
<h1>Existing Names</h1>
<table class="results">
<?php
/* ********* Table Headers ********** */
echo "<tr>";
echo "<td>" . " " . "</td>";
echo "<td>" . "ID" . "</td>";
echo "<td>" . "Name" . "</td>";
echo "<td>" . " " . "</td>";
echo "</tr>";
/* ********* Table data loop ********* */
while( $row = mysql_fetch_array($res)) {
echo "<tr>";
echo "<td>" . "<a href='testedit.php?edit=$row[id]'>Edit</a>" . "</td>";
echo "<td>" . $row[id] . "</td>";
echo "<td>" . $row[name] . "</td>";
echo "</tr>";
}
?>
</table>
<!----- End Table List ------>
</body>
</html>
testgetautocomplete.php form code is: (password and db info left out obviously
<?php
mysql_connect('localhost', ' ', ' ');
mysql_select_db(" ");
$term=$_GET["term"];
$query=mysql_query("SELECT * FROM formtest where name like '%".$term."%' order by name ");
$json=array();
while($student=mysql_fetch_array($query)){
$json[]=array(
'value'=> $student["name"],
'label'=>$student["name"]." - ".$student["id"]
);
}
echo json_encode($json);
?>
testedit.php form code is as follows:
<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function(){
$("#newname").autocomplete({
source:'testgetautocomplete.php',
minLength:1
});
});
</script>
<title>UYS Edit Page</title>
</head>
<body>
<!----- Start Edit Form ------>
<?php
include_once('db.php');
if( isset($_GET['edit']) )
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM formtest WHERE id='$id'");
$row= mysql_fetch_array($res);
}
if( isset($_POST['newname']))
{
$newname = $_POST['newname'];
$id = $_POST['id'];
$sql = "UPDATE formtest SET name='$newname' WHERE id='$id'";
$res = mysql_query($sql)
or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=testadd.php'>";
}
?>
<table class="centerwithroom">
<form action="testedit.php" method="POST">
<tr>
<td>New Name </td><td><input type="text" name="newname" value="<?php echo $row[1]; ?>"/></td>
</tr>
<td></td><td><input type="submit" value=" Update "/></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row[0]; ?>"/>
</form>
</table>
<!----- End Edit Form ------>
</body>
</html>