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.
}
Related
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');
<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>
I created this form with bootstrap 3:
<form role="form" method="get" target="_self" action="checkplayer.php">
<div class="input-group">
<input type="text" class="form-control input-lg" placeholder="Search player" name="player">
<span class="input-group-btn">
<button class="btn btn-primary btn-lg" type="submit">Search</button>
</span>
</div><br>
<div class="btn-group">
<button name="region" type="button" class="btn btn-primary" value="NA" autofocus="true">NA</button>
<button name="region" type="button" class="btn btn-primary" value="EUW">EUW</button>
<button name="region" type="button" class="btn btn-primary" value="EUNE">EUNE</button>
<button name="region" type="button" class="btn btn-primary" value="BR">BR</button>
<button name="region" type="button" class="btn btn-primary" value="TR">TR</button>
<button name="region" type="button" class="btn btn-primary" value="RU">RU</button>
<button name="region" type="button" class="btn btn-primary" value="LAN">LAN</button>
</div>
</form>
Im using the button group, because it looks better then a dropdwon menue or just checkboxes.
I tried to send the btn-group value by the _get methode but it does not work.
So how can in submit the value with my _get methode to my PHP file?
Thank you for helping
I found a solution
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" id="NA" name="region" value="NA" autofocus="true"/> NA
</label>
<label class="btn btn-primary">
<input type="radio" id="EUW" name="region" value="EUW" /> EUW
</label>
<label class="btn btn-primary">
<input type="radio" id="EUNE" name="region" value="EUNE" /> EUNE
</label>
<label class="btn btn-primary">
<input type="radio" id="BR" name="region" value="BR" /> BR
</label>
<label class="btn btn-primary">
<input type="radio" id="TR" name="region" value="TR" /> TR
</label>
<label class="btn btn-primary">
<input type="radio" id="RU" name="region" value="RU" /> RU
</label>
<label class="btn btn-primary">
<input type="radio" id="LAN" name="region" value="LAN" /> LAN
</label>
<label class="btn btn-primary">
<input type="radio" id="LAS" name="region" value="LAS" /> LAS
</label>
<label class="btn btn-primary">
<input type="radio" id="OCE" name="region" value="OCE" /> OCE
</label>
</div>
These buttons inside 'btn-group' div, aren't doing anything useful, apart from just displaying some buttons on the screen. They are not aware of any 'selected' property (in order for the selected value to be submitted).
Buttons are not meant to act like radio buttons, checkboxes etc. Their purpose is to perform an action when they are clicked.
So what i recomend is to forget about button group and just add checkboxes. If you are concerned about the styling, just add this very useful library (http://icheck.fronteed.com/). I think line skin will be great for you.
If you really want these buttons, you should write some js code (preferably jquery) and on the click event, store the selected value(s) in a hidden input that will get submitted. See here: Change a hidden input's value with jquery/javascript
I'm using Bootstrap theme and my form is this:
<form class="navbar-form" role="search" method="get" action="<?=$domain?>search" onsubmit="DoSubmit();">
<div class="input-group">
<input type="text" class="form-control input" placeholder="Search" name="q">
<div class="input-group-btn">
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
The original page where is placed is
http://www.domain.com/toys/search/Electronics for Kids/
when i search into form "test" and press enter, it sends me to
http://www.domain.com/toys/search/?q=test
With what should i replace the code to send me to:
http://www.domain.com/toys/search/test
Thanks in advance!
I assume you use JQuery if you use bootstrap.
So do following:
HTML:
<form class="navbar-form" role="search" method="get" action="<?=$domain?>search" id="searchFrom">
<div class="input-group">
<input type="text" id="searchField" class="form-control input" placeholder="Search" name="q">
<div class="input-group-btn">
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
JS:
$('body').on('submit','#searchFrom', function(e) {
e.preventDefault();
location.href = $(this).attr('action')+'/'+encodeURIComponent($('#searchField').val());
});
<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