当我测试我的表单时,它会将数据成功添加到我的数据库表中,但单选按钮选择始终为“未分组”,这是该组中的第一个单选按钮。我试图给他们独立的ID,但在我的ajax.js文件我的函数使用此:通过ajax添加单选按钮到数据库?
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文件数据,将其与INSERT语句:
<!-- 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;
}
}
为什么不使用jQuery?有些事情变得更简单,也是Ajax。 – jtheman 2013-03-07 23:29:08
这看起来相关:http://stackoverflow.com/questions/8233308/getting-the-value-from-a-radio-button-using-javascript – 2013-03-07 23:29:58
这是一个任务,所以我必须坚持这样做。 – Corey 2013-03-07 23:30:15