Check if form submitted and do something

给你一囗甜甜゛ 提交于 2019-12-13 03:33:27

问题


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:


<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.



来源:https://stackoverflow.com/questions/18837352/check-if-form-submitted-and-do-something

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!