以下代码是否有效?
Is the following code valid?
让我先解释一下代码:
首先,用户输入id, jquery检查数据库的id,如果id存在,它获取结果并显示在其余的表单框中,如果不存在,它不显示一些样式的id。
下面是代码: Javascript和html <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
//checks if the id exists
$.post("script_3.php",{ id:$('#id').val(),rand:Math.random() } ,function(data)
{
if(data=='yes')//correct id detail
{
$$("#msgbox").fadeTo(200,0.1,function()
{
$(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
function()
{
$.post('script_1.php', { id: $('input[name="id"]', '#myForm').val()
},
function(json) {
$("input[name='title']").val(json.title);
$("input[name='name']").val(json.rno);
}, "json");
});
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function()
{
//add message and change the class of the box and start fading
$(this).html('NO ID...').addClass('messageboxerror').fadeTo(900,1);
});
}
});
return false; //not to post the form physically
});
Html: <style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
.top {
margin-bottom: 15px;
}
.buttondiv {
margin-top: 10px;
}
.messagebox{
position:absolute;
width:100px;
margin-left:30px;
border:1px solid #c93;
background:#ffc;
padding:3px;
}
.messageboxok{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #349534;
background:#C9FFCA;
padding:3px;
font-weight:bold;
color:#008000;
}
.messageboxerror{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #CC0000;
background:#F7CBCA;
padding:3px;
font-weight:bold;
color:#CC0000;
}
</style>
<body>
<form id="myForm" method="post">
id: <input type="text" name="id"/>
<div id="hidden" style="display: none;">
<p>Title:<input type="text" name="title"/></p>
<p>Name:<input type="text" name="rno"/>
</div>
<br/>
<input type="button" id="button1" value ="Get Info"
onclick="document.getElementById('hidden').style.display = '';"/>
<span id="msgbox" style="display:none"></span>
</form>
<div id="age"></div>
</body>
script_3.php
<?php
//connect to DB removed
$id= mysql_real_escape_string($_POST['id']);
$sql="SELECT id FROM parentid WHERE id='".$id."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
if(mysql_num_rows($result)>0)
{
echo "yes";
//now set the session from here if needed
$_SESSION['u_id']=$id;
}
else
echo "no"; //Invalid Login
?>
好吧,我的问题是它不会去script_1.php部分,它总是显示没有id错误,即使我输入正确的id。
我有任何语法错误的代码??
我想你的问题可能是
$('#id').val()
没有id='id'
您可以在firefox浏览器中打开firebug或在google chrome浏览器中检查元素,以查看是否有JavaScript错误或查看从调用script_3的服务器响应,还使用'var_dump'。或者print_r函数检查JavaScript参数是否正确到达服务器
1/
从这行我可以看到:
& lt;p>名称:& lt;输入类型="text" name="rno"/>
从HTML的一边,你没有关闭<p>
标签关闭它,把</p>
。
2/
在script_3
你忘记加括号了。必须是:
其他{回声"不";}
3
在script_1 你有:
?php
必须是:
& lt;php
?
希望能有所帮助
相关文章:
- 如何在AngularJS中有效地检查日期是否有效
- 检查蒸汽配置文件url是否有效
- 此multi-onChange事件是否有效
- 如果标记是使用 JavaScript 动态构建的,Schema.org 标记是否有效?
- 当用户在移动网络上时,GeoIP是否有效
- 如果系统时间发生更改,setTimeout是否有效
- JQuery-检查IMG src是否有效
- Regex以确定该值在JS中是否有效,返回null
- 在不重新运行jquery验证的情况下检查表单是否有效
- 使用JSONP内容压缩web服务响应是否有效
- 检查字符串是否为有效的html标记
- Javascript正则表达式,用于检查字符串是否为有效数字
- 是否可以在字段有效时触发事件(html5)
- Angular 2-检查图像url是否有效或已损坏
- Jquery/Javascript检查我的单词是否有效
- 在条件(if)结构的条件语句中是否有定义变量的有效方法
- 我如何在Capybara中测试页面是否未重新加载(JavaScript onClick拦截有效)
- 一种确定网站是否有效的方法
- 在 Zurb Foundation 5 中没有 JavaScript 的顶栏是否有效
- 使用 javascript 确定电子邮件地址是否有效