0

所以我有这个表格:

<form enctype="multipart/form-data" action="cgi-bin/upload.cgi?upload_id=" method="post" onSubmit="return StartUpload(this);" target="xupload">
<table>
    <tr>
        <td class="first"><span class="right">Upload File: </span></td>
        <td class="second"><input name="file_1" type="file" onChange="checkExt(this.value)" accept=".mp4, .avi, .wmv, .mov, .camrec, .flv, .zip, .rar"></td>
        <td class="third"><span class="left">(required)</span></td>
    </tr>
    <tr></tr>
    <tr><center><h2>Video Information</h2></center></tr>
    <tr></tr>
    <tr>
        <td class="first"><span class="right">Your Name: </span></td>
        <td class="second"><input type="text" name="name"></td>
        <td class="third"><span class="left">(preferred)</span></td>
    </tr>
    <tr>
        <td class="first"><span class="right">Series ID: </span></td>
        <td class="second"><input id="series_id" type="text" name="series_id" placeholder="You should have been given this by your admin"></td>
        <td class="third"><span class="left">(preferred)</span></td>
    </tr>
    <tr>
        <td class="first"></td>
        <td class="second"><span id="series_id_check">Enter You're series ID Above and this will change</span></td>
        <td class="third"></td>
    </tr>
    <tr>
        <td class="first"><span class="right">Title: </span></td>
        <td class="second"><input type="text" name="title"></td>
        <td class="third"><span class="left">(preferred)</span></td>
    </tr>
    <tr>
        <td class="first"></td>
        <td class="second center"><small>Please use "<span style="color:#9c0000">&lt;br&gt;</span>" to make a new line</small></td>
        <td class="third"></td>
    </tr>
    <tr>
        <td class="first"><span class="right">Description: </span></td>
        <td class="second"><textarea name="desc"></textarea></td>
        <td class="third"><span class="left">(preferred)</span></td>
    </tr>
    <tr>
        <td class="first"><span class="right">Guests: </span></td>
        <td class="second"><input type="text" name="guests"></td>
        <td class="third"><span class="left">(optional)</span></td>
    </tr>
    <tr>
        <td class="first"><span class="right">Additional Tags: </span></td>
        <td class="second"><input type="text" name="tags"></td>
        <td class="third"><span class="left">(comma separated - optional)</span></td>
    </tr>
</table>
<input type="submit" name="submit" value="Upload File">
</form>

我用来将视频上传到我的网络托管服务器。我希望能够series_id_check使用一些使用 PHP 数组的 javascript 更改跨度中的文本...这是我用于从 mysql 填充数组的 PHP(我知道它已被弃用,但对于我的 WEB 托管 PHP 5.3 已安装并且它仅从 5.4 开始贬值):

$sql1 = mysql_fetch_assoc(mysql_query("SELECT COUNT cnt FROM series"));
$count = $sql1['cnt'];

$gamearray = array($count - 1);
$namearray = array($count - 1);
$idarray = array($count - 1);

for ($i = 0; $i < $count; $i++)
{
    $sql2 = mysql_fetch_assoc(mysql_query("SELECT id,game,name FROM series WHERE id='{$i}'"));

    $gamearray[$i] = $sql2['game'];
    $namearray[$i] = $sql2['name'];
    $idarray[$i]   = $sql2['id'];
}

die我知道它可以很好地连接到数据库,因为当它尝试连接时我有一个声明

javascript是:

var id = "series_id";

document.getElementById("series_id").onblur = function()
{
    var value = document.getElementById(id).value;


    var gameArray = new Array(<? echo implode(',', $gamearray); ?>;
    var nameArray = new Array(<? echo implode(',', $namearray); ?>;
    var idArray = new Array(<? echo implode(',', $idarray); ?>;

    if (inArray(value,idArray)
    {
        var id = parceInt(value);
        var game = gameArray.indexOf(id);
        var name = nameArray.indexOf(id);
        var input= "You've inputted the id for the game: " + game + " for the series: " + name;

        document.getElementById("spawn_series_id_check").innerHTML = input;
    }
});

function inArray(var input, var array)
{
    var count = array.length;
    var returnVal = false;

    for (var i = 0; i < count; i++)
    {
        if (array[i] == input)
        {
            returnVal = true;
        }
    }

    return returnVal;
}

基本上 series_id_check 中的文本不会改变。你能告诉我如何解决它吗

4

2 回答 2

0

您应该考虑使用 JSON 来处理这些数据。

$result = mysql_query("SELECT id,game,name FROM series");
$count = count($result);

$series = array();

while ($row = mysql_fetch_assoc($result)) {
    $series[$row['id']] = array('game' => $row['game'], 'name' => $row['name']);
}

在你的 JS 中:

document.getElementById("series_id").onblur = function()
{
    var series = '<?php echo json_encode($series); ?>';
    var id = parseInt(this.value, 10);

    if (undefined !== series[id])
    {
        var game = series[id]['game'];
        var name = series[id]['name'];

        var message = "You've inputted the id for the game: " + game + " for the series: " + name;

        document.getElementById("spawn_series_id_check").innerHTML = message;
    }
});

我还没有测试过代码,但这是你应该走的路。

于 2013-08-07T20:31:51.703 回答
0

你的 javascript 数组是不完整的,会像这样出来:

var gameArray = new Array(-1;

尝试重写代码的数组构建部分来为每个数组执行此操作:

var gameArray = new Array(<? echo implode(',', $gamearray); ?>);

请注意,javascript 数组和行已关闭。

添加修复

if (inArray(value,idArray)
{

应该

if (inArray(value,idArray))
{

还有一个 - 尝试改变

function inArray(var input, var array)
{

function inArray(input, array)
{

另外 - 无法设置 null 属性意味着匿名函数存在问题。

函数的第二行var value = document.getElementById(id).value

需要以分号结尾var value = document.getElementById(id).value;

于 2013-08-07T20:33:34.467 回答