Check if form submitted and do something

2019-08-26 20:29发布

I have got my validation working but I wanted to only output it, when it has been submitted but isset nor empty seems to work for me for some reason? I am using php 5.5.3 I believe

My code:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
    <input type="text" placeholder="Username" maxlength="15" name="username"/><br>
    <input type="password" maxlength="15" name="password1"/><br>
    <input type="password" maxlength="15" name="password2" /><br>
    <input type="text" placeholder="your@email.com" maxlength="25"  name="email"/><br>
    <input type="text" maxlength="20" name="county" /><br>
    <input type="submit" value="Register" name "register"/>
</form>
<?php
//Form Validation
if(empty($_POST['register']))
{
    echo "Option A <br>";
}
else
{
    echo "Option B <br>";
}

Also, I can't remember how I would enter in the same information for that user if it validates fine?

value="<?php $_POST['stuff'] ?>

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-26 20:57
<input type="submit" value="Register" name "register"/>
                                          ^

You're missing an = there.

It should be:

<input type="submit" value="Register" name="register"/>

It's betterto use isset() instead of empty(), in this case:

if(isset($_POST['register']))
{
    echo "Option A <br>";
}
else
{
    echo "Option B <br>";
}

I'm not sure what Option A and B are, but with this code, you'll always see Option B when you load the page. The condition in your if block will execute to FALSE and hence the else block will executed. If you want to avoid this, you can use an elseif statement instead of an else.

查看更多
登录 后发表回答