HTML/PHP form problems?
Can someone tell me why this PHP script won't detect when the form is submitted?
<div id='main'>
<h1>Register</h1>
<?php
if ( isset( $_POST['regusername'] ) && isset( $_POST['regpassword'] ) && ( $_POST['password'] == $_POST['password2'] ) )
{
if ( Register( $_POST['regusername'], $_POST['regpassword'] ) )
echo "<p>Registered successfully, try logging in now</p>";
else
echo "<p>Registration appeared to fail</p>";
}
else
{
?>
<form action='' method='post'>
<p>
<label for='regusername'>Desired Username (max 20 chars)</label>
<input type='text' id='regusername' maxlength='20' />
</p>
<p>
<label for='regpassword'>Desired Password (max 20 chars)</label>
<input type='password' id='regpassword' maxlength='20' />
</p>
<p>
<label for='regpassword2'>Repeat password</label>
<input type='password' id='regpassword2' maxlength='20' />
</p>
<p>
<input type='submit' id='regsubmit' value='Register'/>
</p>
</form>
<?php
}
?>
</div>
I've tried just used "if ( isset( $_POST['regsubmit'] ) )" and that doesn't work either… it just always displays he form and never the paragraphs for registering successfully/unsuccessfully
Well I just tested on my server and it works. Hopefully you changed the previous error mentioned by system_meltdown as well. If you want me to explain in further detail PM me.
P.S. - Here is the slightly adjusted code I used.
<div id='main'>
<h1>Register</h1>
<?php
if ( isset( $_POST['regusername'] ) && isset( $_POST['regpassword'] ) && ( $_POST['regpassword'] == $_POST['regpassword2'] ) )
{
//have to take out because I do not have your Register function
//if ( Register( $_POST['regusername'], $_POST['regpassword'] ) )
echo "<p>Registered successfully, try logging in now</p>";
//else
echo "<p>Registration appeared to fail</p>";
}
else
{
?>
<form action='' method='post'>
<p>
<label for='regusername'>Desired Username (max 20 chars)</label>
<input type='text' name='regusername' maxlength='20' />
</p>
<p>
<label for='regpassword'>Desired Password (max 20 chars)</label>
<input type='password' name='regpassword' maxlength='20' />
</p>
<p>
<label for='regpassword2'>Repeat password</label>
<input type='password' name='regpassword2' maxlength='20' />
</p>
<p>
<input type='submit' name='regsubmit' value='Register'/>
</p>
</form>
<?php
}
?>
</div>
-Nirucesis
All is well that is simple as shit…
<div id='main'>
<h1>Register</h1>
<?php
if ( isset($_POST['regsub'] && isset( $_POST['regun'] ) && isset( $_POST['regpwd'] ) && ( $_POST['regpwd'] == $_POST['regpwd2'] ) ){
//I take it you have set up a Register command to input this into your DB
if ( Register( $_POST['regun'], $_POST['regpwd'] ) ){
echo "<p>Registered successfully, try logging in now</p>";
}else{
echo "<p>Registration appeared to fail</p>";
}
}else{
?>
<form action='' method='post'>
<p />
<label for='regun'>Desired Username (max 20 chars)</label>
<input type='text' name='regun' maxlength='20' />
<p />
<label for='regpwd'>Desired Password (max 20 chars)</label>
<input type='password' name='regpwd' maxlength='20' />
<p />
<label for='regpwd2'>Repeat password</label>
<input type='password' name='regpwd2' maxlength='20' />
<p />
<input type='submit' name='regsub' value='Register'/>
</form>
<? } ?>
</div>
I know a lot of this change is nit pickey layout and simplifying shit but that helps trust me…when you have 5000 lines of code to go through and you have to type regpassword all the time instead of regpwd it makes a difference. you will thank me for short hand soon enough :evil:
I do have a CMS that I could send you a skeleton for if you like. Just let me know.
Got it to work eventually… but have no idea what I did… I didn't seem to actually change anything… I decided to echo the POST vars, all 3 of them… And then suddenly it worked… it now works without having to echo them
EDIT: @Aldar: Yeah If you can send it to me I'd be grateful, I can now get a shoutbox and blogging thingy for main page… But don't really know where to go from here, and how to do the harder stuff
mozzer wrote: Personally, when I was learning I'd do my forms like this
if ( isset ( $_POST ) )
{
print_r ( $_POST );
} else {
echo "<!-- FORM -->";
}
?>```
Personally I don't see the point in echoing html unnecessarily.
```markup
...
} else {
?>
<!-- form -->
<?php } ?>
.|Mals