在onblur函数中使用PHP和Javascript



所以我有这样的形式:

<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>

我用它来上传视频到我的网络托管服务器。我希望能够用一些使用PHP数组的javascript来更改series_id_check跨度中的文本。。。以下是我用于从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中的文本不会更改。你能告诉我如何修复它吗?

您应该考虑使用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;
    }
});

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

您的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 likley的属性意味着匿名函数有问题。

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

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

相关内容

  • 没有找到相关文章

最新更新