Why is the button not redirecting? - php

<form action="../includes/process_login.php" method="post" name="login_form">
<div class="form-group">
<label class="control-label">Username</label>
<input type="text" class="form-control" name="username" />
</div>
<div class="form-group">
<label class="control-label">Password</label>
<input type="password" class="form-control" name="password" />
</div>
<div class="text-center">
<input type="button"
value="Sign in"
class="btn btn-primary"
action="../includes/process_login.php"/>
</div>
</form>
Hello all, I have been following a tutorial and stumbled upon this error. Whenever I try to click the Sign-in button, it doesn't re-direct me. It is supposed to log me in.
Thank you in advance.

Remove the "action" from input and change its type to "submit".
<input type="submit" value="Sign in" class="btn btn-primary" />
This will submit the form to "../includes/process_login.php".

The correct syntax for button(submit) is as follows
<input type="submit" value="Submit">
Thus, your button should be changed as
<input type="submit" value="Sign in" class="btn btn-primary"/>
On clicking this submit button, the form is submitted to the page apecified in the attribute action of the <form>
Read more about it here

Related

tag <a onclick = file.php> and refresh page

I have a login form; this is a part of my code:
<form method="post" action="login.php" >
<label>Username</label>
<input type="text" name="username"/>
<br>
<label>Password</label>
<input type="password" name="password"/>
<br>
<div class="log"><a class="btn btn_red">Login</a></div>
</form>
What I expect is that this form redirect me to the "login.php" file, but it doesn't work. Where am I going wrong?
Thank u all.
You have an anchor (with no href attribute) and no button.
You need a submit button
<div class="log"><button class="btn btn_red">Login</button></div>
Try this
<form method="post" action="login.php" id="form1">
<label>Username</label>
<input type="text" name="username"/>
<br>
<label>Password</label>
<input type="password" name="password"/>
<br>
<div class="log"><button type="submit" class="btn btn_red" form="form1" value="Submit">Login</button></div>
</form>

PHP : Echo not working on POST method from form

I am a bit new to PHP and I'm having a bit of difficulties here and there. I am developing a form and wish to display an error box if any of the fields are empty when the 'Submit' Button is pressed. I've tried the following code but the echo is still not appearing. Any suggestions ?
Form Code:
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="$_POST" action="searchEmployee.php">
<br style="clear:both">
<h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
</div>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
</div>
</form>
</div>
</div>
</div>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$required = array('name', 'surname', 'ID', 'visitorCard');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
}
}
?>
You are using method as $_POST in your form attribute,
It should be only POST.
So, replace your form line with,
<form role="form" method="POST" action="searchEmployee.php">
and also, change Submit button line to,
<input type="submit" name="submit" value="Sign In Visitor" class="btn btn-primary pull-right" />
Here are few mistakes:
You are using method as $_POST in your form attribute, It should be only POST.
Your form will not submit because your button is not a submit type. it should
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
Or
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
You need to make 2 change as below
Change button type like this
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
Change Form method
<form role="form" method="POST" action="searchEmployee.php">
Change this
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
To
<input type="button" id="submit" name="submit" value="submit" class="btn btn-primary pull-right" />Sign In Visitor
and Also
this
<form role="form" method="$_POST" action="searchEmployee.php">
To
<form role="form" method="POST" action="searchEmployee.php">
How will you get POST['submit'] value when you have not even set, that means your if code won't execute it and so it won't echo or alert it.
For Best practice of debugging always try to do this whenever such instances occurs while coding.
print_r($_POST);
This will show you an array of POST variables
change $_POST to POST and put <input type="submit" instead of <input type = "button"
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="post" action="searchEmployee.php"> <br style="clear:both"> <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3> <div class="form-group"> <label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div>
<div class="form-group"> <label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required> </div> <div class="form-group"> <label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label> <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div> <div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
</div> </form> </div>
</div> </div>
Php Code:::searchEmployee.php
<?php if (isset($_POST['submit'])) { $required = array('name', 'surname', 'idCard', 'cardNumber');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) { if (!isset($_POST[$field])) {
$error = true;
} }
if ($error) {
echo "All fields are required."; } }
?>
First think change button type="submit".
Second think change PHP array
$required = array('name', 'surname', 'idCard', 'cardNumber');

Checking which button is pressed with two forms

I have two forms in my webpage, one with a field and a button and the other with two fields and a button.
I have a hard time checking which form's button is pressed, any advice is welcome on how to handle this.
<form class="form-inline" action="somewhere.php" method="post">
<div class="form-group">
<label for="date">Date:</label>
<input type="text" class="form-control" name="date" id="date" placeholder="abc">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<form class="form-inline" action="somewhere.php" method="post">
<div class="form-group">
<label for="date1">Date 1:</label>
<input type="text" class="form-control" name="date1" id="date1" placeholder="abc>
</div>
<div class="form-group">
<label for="date2">Date 2:</label>
<input type="text" class="form-control" name="date2" id="date2" placeholder="abc">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
And what is supposed to check what button is pressed.
$date = $_POST['date'];
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
if (!empty(date)) {
//do something
}
if (!empty($date1) && !empty($date2)) {
//do something else
}
First give name attribute in button.
<button type="submit" name="button1" class="btn btn-default">Submit</button>
<button type="submit" name="button2" class="btn btn-default">Submit</button>
now php side check which button is pressed:
if(isset($_POST['button1']){
//Do something.
}
else if(isset($_POST['button2'])
{
//Do something.
}
HTML
<button type="submit" name="btn1" class="btn btn-default">Submit</button>
<button type="submit" name="btn2" class="btn btn-default">Submit</button>
PHP
if(isset($_POST['btn1']){
//put code here.
}else{
//put code here.
}

how to clear the form data when we already set the value as given in program. By using the clear button

<div class="form-group">
<label for="country">Country</label>
<input type="text" class="form-control" id="country" placeholder="Enter Country" name="country" value="XXXXX" >
</div>
<div class="form-group">
<label for="social">Social media</label>
<input type="text" class="form-control" id="linkedin" placeholder="Enter url" value="http:\\www.google.co.in" name="url">
</div>
<button type="reset" class="btn btn-primary" name="reset" onclick="resetform(form); return false;">Clear</button>
you can use jQuery to clear all input from form/div tag like
$('.form-group').find('input:text').val('');
Just the reset button should do the magic:
<input type="reset" value="Clear">
Update 1:
<button type="reset" class="btn btn-primary" name="reset">Clear</button>

PHP: Bootstrap Radio button value not showing

I have a bootstrap form with 3 radio buttons. I am saving the value into a variable so that I can use to save into database and email.
HTML:
<form class="form-horizontal form-validate" id="signup-form" method="post">
<input type="hidden" name="signupForm" />
<div class="control-group">
<label class="control-label">Seed program</label>
<div class="controls">
<input type="radio" name="signup" value="Seed Program" checked="checked"/>
</div>
</div>
<div class="control-group">
<label class="control-label">Gift Wrap Program</label>
<div class="controls">
<input type="radio" name="signup" value="Gift Wrap" />
</div>
</div>
<div class="control-group">
<label class="control-label">Sign up for both</label>
<div class="controls">
<input type="radio" name="signup" value="Both" />
</div>
</div>
<input type="submit" class="btn btn-large btn-block btn-success" value="Submit" name="submit">
</form>
PHP:
if(isset($_POST['signupForm'])){
if(isset($_POST['signup'])) {
$signup = $_POST["signup"];
}
else{
$signup = "Nothing was selected";
}
}
The Problem:
The Problem is that I can only get the value of the first radio button which has the "checked" attribute. If I select any of the other two, I wont get anything and the value shows empty.
Any help is appreciated.
Thanks,
try using
<input type="radio" name="signup" value="Seed Program" checked />
and not
<input type="radio" name="signup" value="Seed Program" checked="checked"/>

Categories