当我测试我的表单时,它成功地将数据添加到我的数据库表中,但单选按钮选择始终是“未分组”,这是组中的第一个单选按钮。我尝试给他们单独的 id,但是在我的 ajax.js 文件中,我的函数使用了这个:
var group = encodeURI(document.getElementById('group').value);
这意味着现在我正在调用一个不存在的 id(我认为这就是发生的事情)。我怎样才能做到这一点,以便当我的 ajax 函数调用单选按钮选择时,为了将其发送到将其添加到数据库的我的 php 文件,它会选择正确的。
我希望这是有道理的,如果需要更多细节,我可以添加更多。
这是我的表单,它在一个 php 文件中,因为它必须按照我的分配(必须使用 ajax 调用):
<!-- Include AJAX Framework -->
<script src="ajax.js" language="javascript"></script>
<?php $addContact = $_GET['addContact']; //index which sends new contact form to the html page via ajax function.
//form which users can use to enter the details of a new contact to add to the database.
echo "Add A Contact:";
echo "<form action='javascript:insert()' method='get'>";
echo "<table>";
echo "<tr>";
echo "<td>First Name:</td><td><input name='newFname' type='text' id='newFname' value='' size'20'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Last Name:</td><td><input name='newLname' type='text' id='newLname' value='' size'20'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number:</td><td><input name='newPhone' type='text' id='newPhone' value='' size'20'></input></td>";
echo "</tr>";
echo "<td>Email Address:</td><td><input name='newEmail' type='text' id='newEmail' value='' size'20'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Postal Address:</td><td><input name='newAddress' type='text' id='newAddress' value='' size'20'></input></td>";
echo "</tr>";
echo "<td>Group:</td><td><input type='radio' id='Ungrouped' name='group' value='Ungrouped'>No Group <input type='radio' id='Friends' name='group' value='Friends'>Friend";
echo "<input type='radio' id='Colleagues' name='group' value='Colleagues'>Colleagues <input type='radio' id='Family' name='group' value='Family'>Family";
echo "</table>";
//submit button that submits the contact information to an ajax function.
echo "<input type='submit' name='Submit' value='Add Contact'/>";
echo "</FORM>";
这是我的 php 文件,它连接到我的数据库并使用插入语句将数据保存到其中:
<!-- Include Database connections info. -->
<?php include('config.php'); ?>
<?php
if(isset($_GET['newFname']) && isset($_GET['newLname']) && isset($_GET['newPhone']) && isset($_GET['newEmail']) && isset($_GET['newAddress']) && isset($_GET['group']))
{
$newFname = $_GET["newFname"] ;
$newLname = $_GET["newLname"] ;
$newPhone = $_GET["newPhone"] ;
$newEmail = $_GET["newEmail"] ;
$newAddress = $_GET["newAddress"] ;
$group = $_GET["group"] ;
$insertContact_sql = "INSERT INTO `test`.`contacts` (`newFname`, `newLname`, `newPhone`, `newEmail`, `newAddress`, `group`) VALUES ('{$newFname}' , '{$newLname}' , '{$newPhone}' , '{$newEmail}' , '{$newAddress}' , '{$group}')";
$insertContact= mysql_query($insertContact_sql) or die(mysql_error());
}
else
{
echo 'Error! Please fill all fileds!';
}
?>
这是我的 ajax(它的相关 ajax 代码,还有其他函数(即调用我的表单到我的 html 页面的函数),但它们不适用于解决这个问题):
function createObject()
{
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
//value solve an Internet Explorer cache issue
var nocache = 0;
function insert()
{
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('content02').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var newFname= encodeURI(document.getElementById('newFname').value);
var newLname = encodeURI(document.getElementById('newLname').value);
var newPhone = encodeURI(document.getElementById('newPhone').value);
var newEmail = encodeURI(document.getElementById('newEmail').value);
var newAddress = encodeURI(document.getElementById('newAddress').value);
var group = encodeURI(document.getElementById('group').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'newContact.php?newFname='+newFname+'&newLname=' +newLname+'&newPhone=' +newPhone+'& newEmail=' +newEmail+'&newAddress=' +newAddress+'&group=' +group+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4)
{
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('content02').innerHTML = 'Your contact was successfully added!'+response;
}
}