当我测试我的表单时,它将数据成功添加到我的数据库表中,但单选按钮选择始终是“Ungrouped”,这是组中的第一个单选按钮。我尝试给他们单独的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文件连接到我的数据库并使用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;
}
}
答案 0 :(得分:0)
您需要做的是循环浏览所有单选按钮并选择已选中的单选按钮。然后,您可以在AJAX请求中仅发送该单选按钮的值。像这样:
var radios = document.getElementsByName('group');
var selectedRadio = null;
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked === true) {
selectedRadio = radios[i];
break;
}
}
var group = encodeURI(selectedRadio.value);
答案 1 :(得分:0)
你可以试试jQuery来解决这个问题。
将此行添加到您的<head>
部分,在您的HTML中加入jQuery:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
然后使用jquery选择器通过替换AJAX文件中的以下行来获取所需的值
var group = encodeURI(document.getElementById('group').value);
通过这些:
var groupValue = ($("input[name='group']:checked") != null) ? $("input[name='group']:checked").value : "NOT_CHECKED";
var group = encodeURI(groupValue);
确保无线电输入具有属性name
。忽略id
,因为它对于手头的目的没用。此外,如果未选中任何按钮,则设置您自己的值(如果您没有为其保留默认值)。
您也可以阅读https://stackoverflow.com/a/2564019/772020。
请告诉我们是否有帮助。