Snowcat Jung Snowcat Jung - 2 years ago 74
PHP Question

button(submit) didn't work - but input(submit) work

I want to make Login page with PHP.

And want to use button but it didn't works.

and it looks like just refresh the page.

I tried button tag in the form.

How can I make this works??

include "login.php";

<form method="post" id="sign">

<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="email" name="user_mail" id="user_mail" value="<?php echo addslashes($_POST['user_mail'] ?? '') ?>">
<label class="mdl-textfield__label" for="user_mail">Email</label>

<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="password" name="passcode" id="passcode" value="<?php echo addslashes($_POST['passcode'] ?? '') ?>">
<label class="mdl-textfield__label" for="passcode">Password</label>

//this <input> works
<input type="submit" name="signup" form="sign"/>
//I want to use this <button>
<button type="submit" name"signup" form "sign" formmethod="post"></button>

if ($error ?? '') {

echo addslashes($error);



if ($_POST['signup'] ?? '') {

$error = '';

if (!$_POST['user_mail']) $error .= "<br />Please enter email";
else if (!filter_var($_POST['user_mail'], FILTER_VALIDATE_EMAIL)) $error .= "<br />Please enter valid email";

if (!$_POST['passcode']) $error .= "<br />Please enter password";
else {

if (strlen($_POST['passcode']) < 8) $error .= "<br />Please enter a password more than eight";
if (!preg_match('`[A-Z]`', $_POST['passcode'])) $error .= "<br />please least one capital letter";


if ($error) $error = "there were errors:" . $error;

Answer Source

<button> means put a button on the page and execute whatever is in the onclick method. <submit> submits a form.

If you want a button to sumbit your form, use the onclick method of the button to do it. Why can't you use submit, though?

See here, they use a link <a onclick='...' but you can do the same for your button: How to submit a form with JavaScript by clicking a link?

As @Arun says below, you can use:

<button type='submit'> inside your form, too. There are lots of ways to do things, so my only advice on top of this is to be consistent. I use input out of habit, and that way I don't have a bunch of mixed conventions throughout my code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download