开发者

Validating input text in php when moving pages

I do know how to validate a basic input(text) when submitting a form. However, I am lost as to how I am going to validate an input(text) when leaving a web page. Even with JS I couldn't get it to stay on the same page due to the "form action" attribute.

HTML Code for the input and submit

<form name="form1" action="second.php" **onsubmit="return error()"** method="post">
<input style="" name="hall" type="text"><br>
<input name="Move" style="height: 23px" type="submit" value="Move">
</form>开发者_开发技巧

PHP CODE for validating

<?php 
if (isset($_POST['Move'])) {
if(($_POST['hall']) != "Hallway")
{
  echo "Not among available rooms";
}
?>

Also this is the JS code

<script type="text/javascript">
function error()
{
    var x=document.forms["form1"]["hall"].value
    if (x==null || x=="" || x!="next")
    {
        alert("Wrong entry. Try again!!!");
        return false;
    }
}
</script>


The following code is working on my server, provided the second.php page exists. (BTW I added a ; after value, but it seems to work without it).

Did you put the js script after the form? Maybe it could impact.

<html>

<script type="text/javascript">
function error()
{
    var x=document.forms["form1"]["hall"].value;
    if (x==null || x=="" || x!="next")
    {
        alert("Wrong entry. Try again!!!");
        return false;
    }
}
</script>


<body>

<form name="form1" action="second.php" onsubmit="return error()" method="post">
<input style="" name="hall" type="text"><br>
<input name="Move" style="height: 23px" type="submit" value="Move">
</form>


</body>
</html>


Other things I can think of:

-Do you have any JS errors on the page that would prevent this script from running? It worked on my server, as well.

-Do you have another HTML element with an identical name?

-I am assuming that the **'s were for emphasizing that area in your code. If not, of course, they would need to be removed.

The only thing that could prevent your "return false" from working is if you have some kind of JavaScript error that kills the script from running.

If you have additional code on your page, please post it and we'll take a look!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜