-1

我正在使用 PHP、ajax JS 和 MySQL (phpMyAdmin) 进行个人项目。我正在尝试实现一个“添加 URL”功能,该功能从 HTML 表单中获取输入数据并通过 ajax 功能提交到我的数据库,因此不需要刷新整个页面。

我的问题是:如何编写从“时间”输入字段和“组合框”输入字段中选择值的 ajax?

代码:

插入函数(ajax):

function insert() 
{
    var name= encodeURI(document.getElementById('name').value);
    var url= encodeURI(document.getElementById('name').value);

    //What code goes here so I can retrieve the data from the combo boxes and the time field from the input form???


    xmlhttp.open('get', 'addUrl.php?name='+name+'& releaseTime=' +releaseTime+'& releaseDay=' +releaseDay+'& category=' +category+'& genre=' +genre+'& url=' +url);
    xmlhttp.onreadystatechange = contactAdded;
    document.getElementById("content02").innerHTML = "<center><img src='../Images/ajaxloading.gif' alt='Loading: Please Wait...'/></center>";
    xmlhttp.send(null);
    return;
}

addUrlForm.php:

    <!-- Include AJAX Framework -->
<script src="js/ajax.js" language="javascript"></script>

<?php

$addURL = $_GET['addURL'];

echo "Add URL:";
echo "<form action='javascript:insert()' method='get'>";
echo "<table>";
echo "<tr>";
echo "<td>Name:</td><td><input name='name' type='text' id='name' value='' size'30'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Release Time:</td><td><input name='releaseTime' type='time' id='releaseTime' value=''></input></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Release Day:</td><td><select name='releaseDay' id='releaseDay'> <option value='monday'>Monday</option> <option value='tuesday'>Tuesday</option> <option value='wednesday'>Wednesday</option> <option value='thursday'>Thursday</option> <option value='friday'>Friday</option> <option value='saturday'>Saturday</option> <option value='sunday'>Sunday</option> </select></td>";
echo "</tr>";
echo "<td>Category:</td><td><select name='category' id='category'> <option value='television'>Television</option> <option value='movie'>Movie</option> <option value='music'>Music</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Genre:</td><td><select name='genre' id='genre'> <option value='action'>Action</option> <option value='drama'>Drama</option> <option value='comedy'>Comedy</option> <option value='thriller'>Thriller</option> <option value='horror'>Horror</option> <option value='childrens'>Childrens</option> <option value='romantic'>Romantic</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<td>URL:</td><td><input name='url' type='text' id='url' value='' size'250'></input></td>";
echo "</tr>";
echo "</table><br />";
echo "<input type='submit' name='Submit' value='Add URL'/>";
echo "</FORM>";

?>

addUrl.php,将数据发送到数据库:

    <?php include('config.php'); ?>

<?php

    $name = $_GET["name"];
    $releaseTime = $_GET["releaseTime"];
    $releaseDay = $_GET["releaseDay"];
    $category = $_GET["category"];
    $genre = $_GET["genre"];
    $url = $_GET["url"];

$insertContact_sql = "INSERT INTO `links` (`name`, `releaseTime`, `releaseDay`, `category`, `genre`, `url`) VALUES ('{$name}' , '{$releaseTime}' , '{$releaseDay}' , '{$category}' , '{$genre}' , '{$url}')";

$insertContact= mysql_query($insertContact_sql) or die(mysql_error());

?>
4

1 回答 1

1
var releaseTime = encodeURIComponent(document.getElementById('releaseTime').value);
var releaseDay = encodeURIComponent(document.getElementById('releaseDay').value);

等等其他领域。

如果您需要支持旧浏览器,您可以通过以下方式获得 SELECT 的值:

var releaseDaySel = document.getElementById('releaseDay');
var releaseDay = encodeURIComponent(releaseDaySel.options[releaseDaySel.selectedIndex].value);

顺便说一句,您应该使用encodeURIComponent(),而不是encodeURI(). 而且您在分配给url.

于 2013-11-08T23:24:20.903 回答