我很难将 JavaScript 实现到我的 html 表单中。我在整个过程中都使用了很多 PHP。我即将发布的代码包含在用户访问的单独页面中。这是一个php页面。
该表单当前不包含 action=,它会将其提交到 formposts.php。我把它拿出来看看我的 JavaScript 是否被忽略了,并且表单是在检查 JavaScript 之前发送的。但即使不采取行动,我的表单也不会响应 JavaScript。
在深入细节之前,我使用了简单形式的 JS,只是为了检查它是否有效。我真的需要一些关于如何使这项工作的帮助,我不知道问题出在哪里!
我在打开表单和关闭时包含了脚本调用。onsubmit // onclick 我也尝试过包含一个标签来显示文本。JS 在代码的末尾。你可以忽略一些文本/脚本。我猜是表单的开启,前三种输入类型,表单的关闭和JS都包括在内。我已经取出了一些,因为它是一个长表格。
<p><?php echo $session->getText("profileInfo",$language); ?></p>
<table width="530" border="0">
<tr>
<th scope="col" width="40%"><form enctype="multipart/form-data" method="POST" onsubmit="return checkForm()" name="registration">
<img src="<?php echo $userDetails['picUrl']; ?>" border="none"> ;</th>
<th scope="col" width="60%"><h3><?php echo $session->getText("profileMenuPersonal",$language); ?></h3></th>
</tr>
<tr>
<td><h4><?php echo $session->getText("regPic",$language); ?></h4></td>
<td><input type="file" name="uploaded" id="uploaded" onblur="checkType()" /> <label id="picMSG"></label> </td>
</tr>
<tr>
<td><h4><?php echo $session->getText("regFname",$language); ?></h4></td>
<td><input type="text" id="firstName" name="firstName" value="<?php echo $userDetails['firstName']; ?>" /></td>
</tr>
<tr>
<td><h4><?php echo $session->getText("regLname",$language); ?></h4></td>
<td><input type="text" id="lastName" name="lastName" value="<?php echo $userDetails['lastName']; ?>" /></td>
</tr>
<tr> <!-- I have taken some code out for readability of this post -->
<td><input type="hidden" id="reqtype" name="reqtype" value="personalDetails" />
<input type="hidden" id="realUserId" name="realUserId" value="<?php echo $realUserId; ?>" /></td>
<td><input type="submit" name="submit" value="update / save" class="button" onclick="return checkForm()" />
</form>
</td>
</tr>
</table>
<script type="text/javascript">
function checkForm()
{
var picture = document.getElementById('uploaded').value;
var firstName = document.getElementById('firstName').value;
var lastName = document.getElementById('lastName').value;
var email = document.getElementById('email').value;
var address1 = document.getElementById('address1').value;
var address2 = document.getElementById('address2').value;
var postCode = document.getElementById('postCode').value;
var email = document.getElementById('email').value;
// files types that r not allowed
// And are considered harming -
// someone purposfully trying to harm the system
var str1 = ".php";
var str2 = ".js";
var str3 = ".html";
if(!document.registration.firstName.value)
{
alert('please enter name');
return false;
}
if(picture.indexOf(str1) != -1 || picture.indexOf(str2) != -1 || picture.indexOf(str3) != -1)
{
alert("You have tried to upload content that may harm our system. The admin has been contacted and your account flagged. You may be deleted.");
return false;
}
// allowed file types
var str1 = ".jpeg";
var str2 = ".gif";
var str3 = ".png";
if(picture.indexOf(str1) != -1 || picture.indexOf(str1) != -1 || picture.indexOf(str1) != -1 )
{
return true;
}else{
alert("We only allow jpeg, gif and png files to be uploaded.");
return false;
}
}
function checkType()
{
var picture = document.getElementById('uploaded').value;
var element = document.getElementById('picMSG').value
// files types that r not allowed
// And are considered harming -
// someone purposfully trying to harm the system
var str1 = ".php";
var str2 = ".js";
var str3 = ".html";
if(picture.indexOf(str1) != -1 || picture.indexOf(str2) != -1 || picture.indexOf(str3) != -1)
{
element.innerHTML = "invalid type";
element.style.color = "red";
}
// allowed file types
var str1 = ".jpeg";
var str2 = ".gif";
var str3 = ".png";
if(picture.indexOf(str1) != -1 || picture.indexOf(str1) != -1 || picture.indexOf(str1) != -1 )
{
return true;
}else{
}
}
</script>
有谁知道为什么我的“简单” JS 被忽略了?
我是 PHP 和 JS 的新手。但我会认为这是一项简单的任务。我在做什么?