【发布时间】:2011-10-29 05:44:34
【问题描述】:
好吧,让我先解释一下代码:
首先用户输入 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是否被转移,例如在 script_1.php 中使用print_r($_POST); -
创建代码的简化版本。运行。进一步简化它。运行。进一步简化它。运行。重复直到错误停止发生。重新添加复杂性。找出错误发生的确切位置。然后要么 [1] 修复它,要么 [2] 将生成的代码发布到 StackOverflow,准确解释错误似乎发生的位置。 -1.
-
@pekka:我知道它的大部分代码......但如果你观察它......它不会提交信息,而是获取信息......我认为它不可能使用print_r($_POST)..
-
@John:你提交的代码越多,阅读它的人就越少。就像 Chris 建议的那样,您需要减少问题。
标签: php javascript jquery mysql html