I am trying to create a form that will allow a user to update data in a mysql database. I want to call up the record being updated by the use of a select-option list at the top of the form.
I have the code necessary to bring up the data in the form to be edited, however, I am struggling to get the select-option input to trigger the data to change. I'm certain that an onChange function on the select can accomplish this, but have no idea where to start.
I'll appreciate any guidance on which function to use in the javascript. Code follows:
<form name = "updatecontact" action="contact_update.php" method="post">
<table id = "boxes" align="center"; border="0">
<tr>
<td colspan="2">
<div id="errors">
<?php
if ($_POST['submit'])
{
$email=$_POST['email'];
$first=$_POST['first'];
$last=$_POST['last'];
$cell=$_POST['cell'];
$home=$_POST['home'];
$city=$_POST['city'];
$age=$_POST['age'];
$errorstring = "";
if (!$first)
$errorstring = $errorstring."First Name<br>";
elseif ($first=="First Name")
$errorstring = $errorstring."First Name<br>";
if (!$last)
$errorstring = $errorstring."Last Name<br>";
elseif ($last=="Last Name")
$errorstring = $errorstring."Last Name<br>";
if (!$email)
$errorstring = $errorstring."Email Address<br>";
elseif ($email=="Email Address")
$errorstring = $errorstring."Email Address<br>";
if ($errorstring!="")
echo "Please fill out the following fields:<br>$errorstring";
else {
if (isset($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
require('process.php');
} else {
echo "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
}
}
}
?>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<font size="+2"><h2 align="center">Update Contact Information</h2></font>
</td>
</tr>
<tr>
<td>
<?php
require("connect.php");
if (!$cntct_id)
$cntctset = mysql_query("SELECT * FROM `CONTACTS`");
echo "<select name = 'contact' class='request_table' style='background:330033';>";
while ($row = mysql_fetch_assoc($cntctset))
{
$cntcts = $row['CNTCT_ID'];
echo "<option ";
if($cntct_id==$cntcts) echo "SELECTED";
echo ">$cntcts</option>";
}
echo "</select>";
//attempting to set record to be edited here...
$cntct_id=$_POST['contact'];
?>
</td>
</tr>
<tr>
<td>
<h3>Personal Info:</h3>
</td>
</tr>
<tr>
<td>
<?php
echo "<input type='text' value='";
if (!$first)
{
require("connect.php");
$contact_id_sql = mysql_query("SELECT * FROM `CONTACTS` WHERE CNTCT_ID = $cntct_id");
while ($row = mysql_fetch_assoc($contact_id_sql))
{
$first= $row['FIRST'];
ECHO $first;
}
}
elseif ($first!="First Name")
{
echo $first;
}
else
{
echo "First Name";
}
echo "' class='request_table' name='first' id='first' size='30' maxlength='30'>";
?>
</td>
</tr>
<tr>
<td>
<?php
echo "<input type='text' value='";
if (!$last)
{
require("connect.php");
$contact_id_sql = mysql_query("SELECT * FROM `CONTACTS` WHERE CNTCT_ID = $cntct_id");
while ($row = mysql_fetch_assoc($contact_id_sql))
{
$last= $row['LAST'];
ECHO $last;
}
}
elseif ($last!="Last Name")
{
echo $last;
}
else
{
echo "Last Name";
}
echo "' class='request_table' name='last' id='last' size='30' maxlength='30'>";
?>
</td>
</tr>
The rest of the code is just more inputs for the form.....