0

当我测试我的表单时,它成功地将数据添加到我的数据库表中,但单选按钮选择始终是“未分组”,这是组中的第一个单选按钮。我尝试给他们单独的 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;
    }
}
4

2 回答 2

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);
于 2013-03-08T00:30:21.113 回答
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阅读。

请告诉我们是否有帮助。

于 2013-03-08T00:40:26.480 回答